Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit2f5384b

Browse files
committed
refactor project structure and async action logic
1 parent6064095 commit2f5384b

16 files changed

+242
-317
lines changed

‎package.json‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name":"redux-ts",
3-
"version":"2.6.0",
3+
"version":"3.0.0",
44
"description":"Utils to define redux reducer/action in typescript",
55
"main":"dist/redux-ts.min.js",
66
"typings":"dist/src/index.d.ts",
@@ -20,9 +20,9 @@
2020
"build:dev":"webpack --config webpack.dev.js",
2121
"build:prod":"webpack --config webpack.prod.js",
2222
"clean":"rimraf dist lib",
23-
"build":"npm run build:dev && npm run build:prod",
24-
"prepublish":"npm runclean&&npm run build && npm run test",
25-
"test":"mocha --require source-map-support/register --require ts-node/register **/*.spec.ts"
23+
"build":"npm runclean && npm runbuild:dev&& npm run build:prod",
24+
"prepublish":"npm runtest&& npm run build",
25+
"test":"mocha --require source-map-support/register --require ts-node/registertests/**/*.spec.ts"
2626
},
2727
"tags": [
2828
"react",

‎src/action.model.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import{Action}from'redux'
2+
3+
exportabstractclassSyncActionimplementsAction<string>{
4+
type:string
5+
}
6+
7+
exportinterfaceActionClass<TextendsSyncAction>{
8+
prototype:T
9+
}
10+
11+
exporttypeActionBody<S,AextendsSyncAction>=(
12+
state:S,
13+
action:Action,
14+
dispatch:<DextendsSyncAction>(action:D)=>Promise<D>,
15+
)=>S

‎src/index.ts‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1-
export*from'./utils/actionHelpers'
2-
export*from'./utils/reducerBuilder'
3-
export*from'./utils/storeBuilder'
1+
import'./polyfills/object.polyfill'
2+
import'./polyfills/promise.polyfill'
3+
export*from'./action.model'
4+
export*from'./reducer.builder'
5+
export*from'./store.builder'

‎src/polyfills/object.polyfill.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* Hack in support for Function.name for browsers that don't support it.
3+
* https://stackoverflow.com/questions/25140723/constructor-name-is-undefined-in-internet-explorer
4+
**/
5+
if(
6+
Function.prototype.name===undefined&&
7+
Object.defineProperty!==undefined
8+
){
9+
Object.defineProperty(Function.prototype,'name',{
10+
get(){
11+
constfuncNameRegex=/function\s([^(]{1,})\(/
12+
constresults=funcNameRegex.exec(this.toString())
13+
returnresults&&results.length>1 ?results[1].trim() :''
14+
},
15+
set(value){},
16+
})
17+
}

‎src/polyfills/promise.polyfill.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
interfacePromiseConstructor{
2+
defer<T>():{
3+
resolve:(value?:T|PromiseLike<T>)=>void
4+
reject:(reason?:any)=>void
5+
promise:Promise<T>,
6+
}
7+
}
8+
9+
Promise['defer']=functiondeferPolyfill(){
10+
constdeferred={}asany
11+
constpromise=newPromise((resolve,reject)=>{
12+
deferred.resolve=resolve
13+
deferred.reject=reject
14+
})
15+
deferred.promise=promise
16+
returndeferred
17+
}

‎src/reducer.builder.ts‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import{ActionBody,ActionClass}from'.'
2+
import{Action,Dispatch,Reducer}from'redux'
3+
4+
exportclassReducerBuilder<State={}>{
5+
privateactions:{[type:string]:ActionBody<State,Action>}={}
6+
privateinitState:State
7+
8+
publicinit(state:State){
9+
this.initState=state
10+
returnthis
11+
}
12+
13+
publichandle<TextendsAction>(
14+
type:ActionClass<T>,
15+
action:ActionBody<State,T>,
16+
){
17+
this.actions[(<any>type).name]=action
18+
returnthis
19+
}
20+
21+
privatebuild(dispatch:Promise<Dispatch>):Reducer<State,Action>{
22+
return(state=this.initState,action)=>{
23+
consttype=action.type
24+
constactionBody=this.actions[type]
25+
26+
if(!!actionBody){
27+
returnactionBody(state,action,a=>dispatch.then(d=>d(a)))
28+
}
29+
30+
returnstate
31+
}
32+
}
33+
}

‎src/store.builder.ts‎

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
import{
2+
Middleware,
3+
DeepPartial,
4+
StoreEnhancer,
5+
Store,
6+
Dispatch,
7+
ReducersMapObject,
8+
applyMiddleware,
9+
combineReducers,
10+
compose,
11+
createStore,
12+
Reducer,
13+
}from'redux'
14+
import{ReducerBuilder,SyncAction}from'.'
15+
16+
constdevTool:StoreEnhancer=f=>
17+
(windowasany).__REDUX_DEVTOOLS_EXTENSION__
18+
?(windowasany).__REDUX_DEVTOOLS_EXTENSION__
19+
:f
20+
21+
constplainObjMiddleware:Middleware=store=>next=>action=>{
22+
action.type=action.type||action.constructor.name
23+
constplain:any={}
24+
for(constkeyinaction){
25+
plain[key]=action[key]
26+
}
27+
returnnext(plain)
28+
}
29+
30+
exportinterfaceReducerBuilderMap{
31+
[key:string]:ReducerBuilder
32+
}
33+
34+
exportclassStoreBuilder<StoreTypeextends{[key:string]:any}>{
35+
privatemiddlewares:Middleware[]
36+
privatereducers:ReducersMapObject<StoreType>
37+
privatereducerBuilders:ReducerBuilderMap
38+
privateinitialState:DeepPartial<StoreType>
39+
privateenhancer:StoreEnhancer
40+
41+
constructor(){
42+
this.middlewares=[plainObjMiddleware]
43+
this.reducers={}asReducersMapObject<StoreType>
44+
this.reducerBuilders={}
45+
this.initialState={}
46+
this.enhancer=f=>f
47+
}
48+
49+
publicwithMiddleware(middleware:Middleware){
50+
this.middlewares.push(middleware)
51+
returnthis
52+
}
53+
54+
publicwithInitialState(state:DeepPartial<StoreType>){
55+
this.initialState=state
56+
returnthis
57+
}
58+
59+
publicwithReducer(name:string,reducer:Reducer){
60+
this.reducers[name]=reducer
61+
returnthis
62+
}
63+
64+
publicwithReducerBuilder(name:string,reducerBuilder:ReducerBuilder){
65+
this.reducerBuilders[name]=reducerBuilder
66+
returnthis
67+
}
68+
69+
publicwithReducersMap(reducers:ReducersMapObject){
70+
this.reducers={
71+
...(this.reducersasany),
72+
...reducers,
73+
}
74+
returnthis
75+
}
76+
77+
publicwithReducerBuildersMap(reducerBuilders:ReducerBuilderMap){
78+
this.reducerBuilders={
79+
...this.reducerBuilders,
80+
...reducerBuilders,
81+
}
82+
returnthis
83+
}
84+
85+
publicwithEnhancer(enhancer:StoreEnhancer){
86+
constpreEnhancer=this.enhancer
87+
this.enhancer=f=>enhancer(preEnhancer(f))
88+
returnthis
89+
}
90+
91+
publicwithDevTools(){
92+
this.withEnhancer(devTool)
93+
returnthis
94+
}
95+
96+
publicbuild():Store<StoreType>{
97+
constdefer=Promise.defer<Dispatch<SyncAction>>()
98+
constreducerMap=Object.keys(this.reducerBuilders).reduce(
99+
(p:any,r)=>({
100+
...p,
101+
[r]:(this.reducerBuilders[r]asany).build(defer.promise),
102+
}),
103+
this.reducers,
104+
)
105+
constmiddlewares=applyMiddleware(...this.middlewares)
106+
constreducers=combineReducers<StoreType>(reducerMap)
107+
constcomposer=compose(middlewares,this.enhancer)(createStore)
108+
conststore=composer(reducers,this.initialState)
109+
110+
defer.resolve(store.dispatch)
111+
112+
returnstore
113+
}
114+
}

‎src/utils/actionHelpers.ts‎

Lines changed: 0 additions & 38 deletions
This file was deleted.

‎src/utils/asyncMiddleware.ts‎

Lines changed: 0 additions & 41 deletions
This file was deleted.

‎src/utils/browserPolyfill.ts‎

Lines changed: 0 additions & 43 deletions
This file was deleted.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp