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

Commit514f9a6

Browse files
author
okan.cetin
committed
#000 okan remove lodash dependency and fix unit tests
1 parentbc6627c commit514f9a6

File tree

7 files changed

+34
-23
lines changed

7 files changed

+34
-23
lines changed

‎README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import { StoreBuilder } from 'redux-ts'
2020

2121
var store:Redux.Store<StoreState>=newStoreBuilder<StoreState>()
2222
.withInitialState({test:true})
23-
.withReducersMap(reducers)
23+
.withReducer("reducer", reducer)
2424
.build();
2525
}
2626
```

‎package.json‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name":"redux-ts",
3-
"version":"2.0.0",
3+
"version":"2.1.0",
44
"description":"Utils to define redux reducer/action in typescript",
55
"main":"lib/index.js",
66
"typings":"lib/src/index.d.ts",
@@ -45,7 +45,6 @@
4545
"decorator"
4646
],
4747
"dependencies": {
48-
"lodash":"^4.15.0",
4948
"react":"^15.3.1",
5049
"react-dom":"^15.3.1",
5150
"react-redux":"^4.4.5",
@@ -68,4 +67,4 @@
6867
"react":"^0.14.0 || ^15.0.0-0",
6968
"redux":"^2.0.0 || ^3.0.0"
7069
}
71-
}
70+
}

‎src/utils/asyncMiddleware.ts‎

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,29 @@ import { AsyncAction, ShowLoading, HideLoading } from './actionHelpers'
22

33

44
constisAsyncAction=(action:AsyncAction|any):action isAsyncAction=>{
5-
returnaction.resolve!==undefined&&typeofaction.resolve==="function";
5+
returnactioninstanceofAsyncAction;
6+
}
7+
8+
constmergeObject=(action:any):any=>{
9+
letmerged:any={};
10+
for(varkeyinaction){
11+
if(key!=='constructor'){
12+
merged[key]=(<any>action)[key];
13+
}
14+
}
15+
returnmerged;
616
}
717

818
exportconstasyncMiddleware=<S>(store:Redux.MiddlewareAPI<S>)=>(next:Redux.Dispatch<S>):Redux.Dispatch<S>=>(action:Redux.Action)=>{
919
//Fix: Actions must be plain objects.
10-
action=_.merge({},action);
20+
letmerged=mergeObject(action);
1121

1222
if(isAsyncAction(action)){
13-
1423
//First dispatch show loading action synchronously
1524
store.dispatch(newShowLoading());
1625

1726
//Change state immediately and register async operations
18-
varnextState=next(action);
27+
varnextState=next(merged);
1928

2029
//Lastly dispatch hide loading action asynchronously
2130
action.then(dispatch=>{
@@ -24,9 +33,11 @@ export const asyncMiddleware = <S>(store: Redux.MiddlewareAPI<S>) => (next: Redu
2433

2534
//After original dispatch lifecycle, resolve dispatch in order to handle async operations
2635
setTimeout(()=>{
27-
(<any>action).resolve(store.dispatch);
36+
merged.resolve(store.dispatch);
2837
});
38+
2939
returnnextState;
3040
}
31-
returnnext(action);
41+
42+
returnnext(merged);
3243
};

‎src/utils/reducerBuilder.ts‎

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,13 @@ export class ReducerBuilder<State> {
1818
returnthis;
1919
}
2020

21-
publicbuild(mergeToState:boolean=true){
22-
return(state:State=this.initState,action:SyncAction)=>{
21+
publicbuild(){
22+
return(state:State=this.initState,action:SyncAction):State|{}=>{
2323
lettype=action.type;
2424
letactionBody=this.actions[type];
2525

2626
if(!!actionBody){
27-
letnextState=actionBody(state,action);
28-
if(!mergeToState){
29-
returnnextState;
30-
}
31-
return_.merge({},state,nextState);
27+
returnactionBody(state,action);
3228
}
3329

3430
returnstate||{};

‎src/utils/storeBuilder.spec.ts‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,18 @@ import { StoreBuilder } from './storeBuilder'
66
describe("Store",()=>{
77

88
varTestAction=<Redux.Action>{type:"test"};
9+
varreducer=(state:any={},action:any)=>{returnstate};
10+
varinitState={reducer:{test:true}};
911

1012
describe("with inital state",()=>{
11-
varstate={test:true}
13+
1214
varstore=newStoreBuilder()
13-
.withInitialState(state)
15+
.withInitialState(initState)
16+
.withReducersMap({ reducer})
1417
.build();
1518

1619
it("should have correct value",()=>{
17-
expect(store.getState()).equal(state);
20+
expect(store.getState()).equal(initState);
1821
});
1922
});
2023

@@ -24,6 +27,7 @@ describe("Store", () => {
2427
vartestMiddleware=(store:any)=>(next:any)=>(action:any)=>{isSet=true;}
2528
varstore=newStoreBuilder()
2629
.withMiddleware(testMiddleware)
30+
.withReducersMap({ reducer})
2731
.build();
2832

2933
store.dispatch(TestAction);
@@ -77,6 +81,7 @@ describe("Store", () => {
7781
returnf;
7882
};
7983
varstore=newStoreBuilder()
84+
.withReducersMap({ reducer})
8085
.withEnhancer(enhancer)
8186
.build();
8287

‎src/utils/storeBuilder.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import*as_from'lodash'
21
import{combineReducers,createStore,applyMiddleware,compose}from'redux';
32
import{asyncMiddleware}from'../utils/asyncMiddleware'
43

@@ -33,7 +32,9 @@ export class StoreBuilder<StoreType> {
3332
}
3433

3534
publicwithReducersMap(reducers:Redux.ReducersMapObject){
36-
this.reducers=_.merge({},this.reducers,reducers)asRedux.ReducersMapObject;
35+
for(varreducerinreducers){
36+
this.reducers[reducer]=reducers[reducer];
37+
}
3738
returnthis;
3839
}
3940

‎typings.json‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
"dependencies": {},
44
"globalDependencies": {
55
"es6-shim":"registry:dt/es6-shim#0.31.2+20160602141504",
6-
"lodash":"registry:dt/lodash#4.14.0+20160829143836",
76
"react":"registry:dt/react#0.14.0+20160817201227",
87
"react-redux":"registry:dt/react-redux#4.4.0+20160724070751",
98
"redux":"registry:dt/redux#3.5.2+20160703092728"

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp