Skip to content

Commit

Permalink
#000 okan remove lodash dependency and fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
okan.cetin committed Sep 23, 2016
1 parent bc6627c commit 514f9a6
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 23 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { StoreBuilder } from 'redux-ts'

var store: Redux.Store<StoreState> = new StoreBuilder<StoreState>()
.withInitialState({test:true})
.withReducersMap(reducers)
.withReducer("reducer", reducer)
.build();
}
```
Expand Down
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "redux-ts",
"version": "2.0.0",
"version": "2.1.0",
"description": "Utils to define redux reducer/action in typescript",
"main": "lib/index.js",
"typings": "lib/src/index.d.ts",
Expand Down Expand Up @@ -45,7 +45,6 @@
"decorator"
],
"dependencies": {
"lodash": "^4.15.0",
"react": "^15.3.1",
"react-dom": "^15.3.1",
"react-redux": "^4.4.5",
Expand All @@ -68,4 +67,4 @@
"react": "^0.14.0 || ^15.0.0-0",
"redux": "^2.0.0 || ^3.0.0"
}
}
}
23 changes: 17 additions & 6 deletions src/utils/asyncMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,29 @@ import { AsyncAction, ShowLoading, HideLoading } from './actionHelpers'


const isAsyncAction = (action: AsyncAction | any): action is AsyncAction => {
return action.resolve !== undefined && typeof action.resolve === "function";
return action instanceof AsyncAction;
}

const mergeObject = (action: any): any => {
let merged: any = {};
for (var key in action) {
if (key !== 'constructor') {
merged[key] = (<any>action)[key];
}
}
return merged;
}

export const asyncMiddleware = <S>(store: Redux.MiddlewareAPI<S>) => (next: Redux.Dispatch<S>): Redux.Dispatch<S> => (action: Redux.Action) => {
//Fix: Actions must be plain objects.
action = _.merge({}, action);
let merged = mergeObject(action);

if (isAsyncAction(action)) {

//First dispatch show loading action synchronously
store.dispatch(new ShowLoading());

//Change state immediately and register async operations
var nextState = next(action);
var nextState = next(merged);

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

//After original dispatch lifecycle, resolve dispatch in order to handle async operations
setTimeout(() => {
(<any>action).resolve(store.dispatch);
merged.resolve(store.dispatch);
});

return nextState;
}
return next(action);

return next(merged);
};
10 changes: 3 additions & 7 deletions src/utils/reducerBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,13 @@ export class ReducerBuilder<State> {
return this;
}

public build(mergeToState: boolean = true) {
return (state: State = this.initState, action: SyncAction) => {
public build() {
return (state: State = this.initState, action: SyncAction): State | {} => {
let type = action.type;
let actionBody = this.actions[type];

if (!!actionBody) {
let nextState = actionBody(state, action);
if (!mergeToState) {
return nextState;
}
return _.merge({}, state, nextState);
return actionBody(state, action);
}

return state || {};
Expand Down
11 changes: 8 additions & 3 deletions src/utils/storeBuilder.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,18 @@ import { StoreBuilder } from './storeBuilder'
describe("Store", () => {

var TestAction = <Redux.Action>{ type: "test" };
var reducer = (state: any = {}, action: any) => { return state };
var initState = { reducer: { test: true } };

describe("with inital state", () => {
var state = { test: true }

var store = new StoreBuilder()
.withInitialState(state)
.withInitialState(initState)
.withReducersMap({ reducer })
.build();

it("should have correct value", () => {
expect(store.getState()).equal(state);
expect(store.getState()).equal(initState);
});
});

Expand All @@ -24,6 +27,7 @@ describe("Store", () => {
var testMiddleware = (store: any) => (next: any) => (action: any) => { isSet = true; }
var store = new StoreBuilder()
.withMiddleware(testMiddleware)
.withReducersMap({ reducer })
.build();

store.dispatch(TestAction);
Expand Down Expand Up @@ -77,6 +81,7 @@ describe("Store", () => {
return f;
};
var store = new StoreBuilder()
.withReducersMap({ reducer })
.withEnhancer(enhancer)
.build();

Expand Down
5 changes: 3 additions & 2 deletions src/utils/storeBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import * as _ from 'lodash'
import { combineReducers, createStore, applyMiddleware, compose } from 'redux';
import { asyncMiddleware } from '../utils/asyncMiddleware'

Expand Down Expand Up @@ -33,7 +32,9 @@ export class StoreBuilder<StoreType> {
}

public withReducersMap(reducers: Redux.ReducersMapObject) {
this.reducers = _.merge({}, this.reducers, reducers) as Redux.ReducersMapObject;
for (var reducer in reducers) {
this.reducers[reducer] = reducers[reducer];
}
return this;
}

Expand Down
1 change: 0 additions & 1 deletion typings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"dependencies": {},
"globalDependencies": {
"es6-shim": "registry:dt/es6-shim#0.31.2+20160602141504",
"lodash": "registry:dt/lodash#4.14.0+20160829143836",
"react": "registry:dt/react#0.14.0+20160817201227",
"react-redux": "registry:dt/react-redux#4.4.0+20160724070751",
"redux": "registry:dt/redux#3.5.2+20160703092728"
Expand Down

0 comments on commit 514f9a6

Please sign in to comment.