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

Commit3bec4d8

Browse files
author
okan.cetin
committed
#000 okan remove semicolon; change promise resolve logic
1 parent75f2ab8 commit3bec4d8

File tree

8 files changed

+153
-144
lines changed

8 files changed

+153
-144
lines changed

‎package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name":"redux-ts",
3-
"version":"2.4.2",
3+
"version":"2.5.0",
44
"description":"Utils to define redux reducer/action in typescript",
55
"main":"lib/index.js",
66
"typings":"lib/src/index.d.ts",

‎src/utils/actionHelpers.ts‎

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,27 @@
11
import"./promiseHelpers"
22
import{Dispatch,Action}from'redux'
33

4-
exporttypeNullableDispatch=Dispatch<any>|void;
4+
exporttypeNullableDispatch=Dispatch<any>|void
55

66
exportabstractclassSyncActionimplementsAction{
7-
type:string;
7+
type:string
88
}
99

1010
exportabstractclassAsyncActionextendsSyncActionimplementsPromise<NullableDispatch>{
11-
12-
privatepromise:Promise<Dispatch<any>>
11+
privateresolve:(value?:Dispatch<any>|PromiseLike<Dispatch<any>>)=>void
12+
privatepromise:Promise<Dispatch<any>>=newPromise<Dispatch<any>>((resolve,reject)=>{
13+
this.resolve=resolve
14+
})
1315

1416
then(onfulfilled?:(value:Dispatch<any>)=>NullableDispatch|PromiseLike<NullableDispatch>,onrejected?:(reason:any)=>void):Promise<NullableDispatch>{
15-
returnthis.promise.then(onfulfilled,onrejected);
17+
returnthis.promise.then(onfulfilled,onrejected)
1618
}
1719

1820
catch(onrejected?:(reason:any)=>NullableDispatch|PromiseLike<NullableDispatch>):Promise<NullableDispatch>{
19-
returnthis.promise.catch(onrejected);
21+
returnthis.promise.catch(onrejected)
2022
}
2123

2224
finally(onfulfilled?:(value?:NullableDispatch,isSuccess?:boolean)=>any|PromiseLike<NullableDispatch>):Promise<NullableDispatch>{
23-
returnthis.promise.finally(onfulfilled);
25+
returnthis.promise.finally(onfulfilled)
2426
}
2527
}

‎src/utils/asyncMiddleware.ts‎

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,42 +3,40 @@ import { SyncAction, AsyncAction } from './actionHelpers'
33

44

55
constisSyncAction=(action:Action):action isany=>{
6-
returnactioninstanceofSyncAction;
6+
returnactioninstanceofSyncAction
77
}
88

99
constisAsyncAction=(action:Action):action isany=>{
10-
returnactioninstanceofAsyncAction;
10+
returnactioninstanceofAsyncAction
1111
}
1212

1313
constmergeObject=(action:any):any=>{
14-
letmerged:any={};
14+
letmerged:any={}
1515
for(varkeyinaction){
1616
if(key!=='constructor'){
17-
merged[key]=(<any>action)[key];
17+
merged[key]=(<any>action)[key]
1818
}
1919
}
20-
returnmerged;
20+
returnmerged
2121
}
2222

2323
exportconstasyncMiddleware=<S>(store:MiddlewareAPI<S>)=>(next:Dispatch<S>):Dispatch<S>=>(action:Action)=>{
2424
if(isSyncAction(action)){
25-
action.type=(<any>action).constructor.name;
25+
action.type=(<any>action).constructor.name
2626

2727
if(isAsyncAction(action)){
28-
(<any>action).promise=newPromise<Dispatch<any>>((resolve,reject)=>{
29-
//After original dispatch lifecycle, resolve dispatch in order to handle async operations
30-
setTimeout(()=>{
31-
resolve(store.dispatch);
32-
});
33-
});
28+
//After original dispatch lifecycle, resolve dispatch in order to handle async operations
29+
setTimeout(()=>{
30+
(<any>action).resolve(store.dispatch)
31+
})
3432
}
3533

3634
//Fix: Actions must be plain objects.
37-
letmerged=mergeObject(action);
35+
letmerged=mergeObject(action)
3836

3937
//Change state immediately and register async operations
40-
returnnext(merged);
38+
returnnext(merged)
4139
}
4240

43-
returnnext(action);
44-
};
41+
returnnext(action)
42+
}

‎src/utils/promiseHelpers.ts‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
interfacePromise<T>{
2-
finally<TResult>(onfulfilled?:(value?:T,isSuccess?:boolean)=>TResult|PromiseLike<TResult>):Promise<TResult>;
2+
finally<TResult>(onfulfilled?:(value?:T,isSuccess?:boolean)=>TResult|PromiseLike<TResult>):Promise<TResult>
33
}
44

55
Promise.prototype['finally']=functionfinallyPolyfill<TResult>(callback:(value?:any,isSuccess?:boolean)=>TResult|PromiseLike<TResult>){
6-
varconstructor=this.constructor;
6+
varconstructor=this.constructor
77

88
returnthis.then(function(value:any){
99
returnconstructor.resolve(callback(value,true)).then(function(){
10-
returnvalue;
11-
});
10+
returnvalue
11+
})
1212
},function(reason:any){
1313
returnconstructor.resolve(callback(reason,false)).then(function(){
14-
throwreason;
15-
});
16-
});
17-
};
14+
throwreason
15+
})
16+
})
17+
}

‎src/utils/reducerBuilder.spec.ts‎

Lines changed: 45 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import 'mocha'
77

88

99
interfaceSampleState{
10-
isSyncActionCalled:boolean;
11-
isAsyncActionCalled?:boolean;
10+
isSyncActionCalled:boolean
11+
isAsyncActionCalled?:boolean
1212
}
1313

1414
interfaceSampleStore{
@@ -26,47 +26,48 @@ describe("Reducer", () => {
2626
describe("with inital state",()=>{
2727
varreducer=newReducerBuilder<SampleState>()
2828
.init({isSyncActionCalled:true})
29-
.build();
29+
.build()
3030

3131
varstore=newStoreBuilder<SampleStore>()
3232
.withReducersMap({ reducer})
33-
.build();
33+
.build()
3434

3535
it("should have correct value",()=>{
36-
expect(store.getState().reducer.isSyncActionCalled).equal(true);
37-
});
38-
});
36+
expect(store.getState().reducer.isSyncActionCalled).equal(true)
37+
})
38+
})
3939

4040
describe("with sync action handler",()=>{
4141
varreducer=newReducerBuilder<SampleState>()
4242
.init({isSyncActionCalled:false})
4343
.handle(SampleSyncAction,(state,action)=>{
44-
state.isSyncActionCalled=true;
45-
returnstate;
44+
state.isSyncActionCalled=true
45+
returnstate
4646
})
47-
.build();
47+
.build()
4848

4949
varstore=newStoreBuilder<SampleStore>()
5050
.withReducersMap({ reducer})
51-
.build();
51+
.build()
5252

53-
store.dispatch(newSampleSyncAction());
53+
store.dispatch(newSampleSyncAction())
5454

5555
it("should be called on dispatch sync action",()=>{
56-
expect(store.getState().reducer.isSyncActionCalled).equal(true);
57-
});
58-
});
56+
expect(store.getState().reducer.isSyncActionCalled).equal(true)
57+
})
58+
})
5959

6060
describe("with async action handler",()=>{
61-
vardispatchedEvents:any[]=[];
62-
varstore:Store<SampleStore>;
61+
vardispatchedEvents:any[]=[]
62+
varstore:Store<SampleStore>
63+
varbeforeDispatch=false
6364

6465
varhookReducer:Reducer<any>=(state:any={},action:Action)=>{
6566
if(!action.type.startsWith("@@")){
66-
dispatchedEvents.push(action.type);
67+
dispatchedEvents.push(action.type)
6768
}
68-
returnstate;
69-
};
69+
returnstate
70+
}
7071

7172
before(done=>{
7273
varreducer=newReducerBuilder<SampleState>()
@@ -76,37 +77,45 @@ describe("Reducer", () => {
7677
})
7778
.handle(SampleAsyncAction,(state,action)=>{
7879
action.then(dispatch=>{
79-
dispatch(newSampleSyncAction());
80-
});
81-
returnObject.assign({},state,{isAsyncActionCalled:true});
80+
dispatch(newSampleSyncAction())
81+
})
82+
returnObject.assign({},state,{isAsyncActionCalled:true})
8283
})
8384
.handle(SampleSyncAction,(state,action)=>{
84-
setTimeout(done);
85-
returnObject.assign({},state,{isSyncActionCalled:true});
85+
returnObject.assign({},state,{isSyncActionCalled:true})
8686
})
87-
.build();
87+
.build()
8888

8989
store=newStoreBuilder<SampleStore>()
9090
.withReducersMap({ hookReducer, reducer})
91-
.build();
91+
.build()
9292

93-
store.dispatch(newSampleAsyncAction());
94-
});
93+
varasyncAction=newSampleAsyncAction()
94+
asyncAction.then(x=>{
95+
beforeDispatch=true
96+
done()
97+
})
98+
store.dispatch(asyncAction)
99+
})
95100

96101
it("should dispatch actions in correct order",()=>{
97102
expect(dispatchedEvents).deep.equal([
98103
(<any>SampleAsyncAction).name,
99104
(<any>SampleSyncAction).name,
100-
]);
101-
});
105+
])
106+
})
102107

103108
it("should handle sync action",()=>{
104-
expect(store.getState().reducer.isSyncActionCalled).equal(true);
105-
});
109+
expect(store.getState().reducer.isSyncActionCalled).equal(true)
110+
})
106111

107112
it("should handle async action",()=>{
108-
expect(store.getState().reducer.isAsyncActionCalled).equal(true);
113+
expect(store.getState().reducer.isAsyncActionCalled).equal(true)
114+
})
115+
116+
it("should handle before dispatch promise",()=>{
117+
expect(beforeDispatch).equal(true)
109118
})
110-
});
119+
})
111120

112-
});
121+
})

‎src/utils/reducerBuilder.ts‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,36 @@ import { SyncAction, AsyncAction } from '../utils/actionHelpers'
33

44

55
exportinterfaceIAction<TextendsAction>{
6-
prototype:T;
6+
prototype:T
77
}
88

99
exporttypeReducer<State,ActionTypeextendsSyncAction>=(state:State,action:ActionType)=>State;
1010

1111
exportclassReducerBuilder<State>{
1212

1313
privateactions:{[type:string]:Reducer<State,SyncAction>}={};
14-
privateinitState:State;
14+
privateinitState:State
1515

1616
publicinit(state:State){
17-
this.initState=state;
18-
returnthis;
17+
this.initState=state
18+
returnthis
1919
}
2020

2121
publichandle<TextendsSyncAction>(actionType:IAction<T>,actionBody:Reducer<State,T>){
22-
this.actions[(<any>actionType).name]=actionBody;
23-
returnthis;
22+
this.actions[(<any>actionType).name]=actionBody
23+
returnthis
2424
}
2525

2626
publicbuild(){
2727
return(state:State=this.initState,action:SyncAction):State=>{
28-
lettype=action.type;
29-
letactionBody=this.actions[type];
28+
lettype=action.type
29+
letactionBody=this.actions[type]
3030

3131
if(!!actionBody){
32-
returnactionBody(state,action);
32+
returnactionBody(state,action)
3333
}
3434

35-
returnstate;
35+
returnstate
3636
}
3737
}
3838
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp