-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(combineReducers): support redux combineReducers function
now export a combineReducers function behaving similarly as redux's one, so you can now return effects from reducers combined together into a main one.
- Loading branch information
1 parent
e9dff24
commit 0268cc7
Showing
8 changed files
with
329 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,9 @@ | |
"main": "dist/redux-data-fx.umd.js", | ||
"module": "dist/redux-data-fx.es5.js", | ||
"typings": "dist/types/redux-data-fx.d.ts", | ||
"files": ["dist"], | ||
"files": [ | ||
"dist" | ||
], | ||
"author": "Matthieu Béteille <[email protected]>", | ||
"repository": { | ||
"type": "git", | ||
|
@@ -19,17 +21,15 @@ | |
"scripts": { | ||
"lint": "tslint -t codeFrame 'src/**/*.ts' 'test/**/*.ts'", | ||
"prebuild": "rimraf dist", | ||
"build": | ||
"tsc && rollup -c rollup.config.ts && rimraf compiled && typedoc --out dist/docs --target es6 --theme minimal src", | ||
"build": "tsc && rollup -c rollup.config.ts && rimraf compiled && typedoc --out dist/docs --target es6 --theme minimal src", | ||
"start": "tsc -w & rollup -c rollup.config.ts -w", | ||
"test": "jest", | ||
"test:watch": "jest --watch", | ||
"test:prod": "npm run lint && npm run test -- --coverage --no-cache", | ||
"deploy-docs": "ts-node tools/gh-pages-publish", | ||
"report-coverage": "cat ./coverage/lcov.info | coveralls", | ||
"commit": "git-cz", | ||
"semantic-release": | ||
"semantic-release pre && npm publish && semantic-release post", | ||
"semantic-release": "semantic-release pre && npm publish && semantic-release post", | ||
"semantic-release-prepare": "ts-node tools/semantic-release-prepare", | ||
"precommit": "lint-staged", | ||
"prepush": "npm run test:prod && npm run build", | ||
|
@@ -47,23 +47,29 @@ | |
}, | ||
"validate-commit-msg": { | ||
"types": "conventional-commit-types", | ||
"helpMessage": | ||
"Use \"npm run commit\" instead, we use conventional-changelog format :) (https://github.com/commitizen/cz-cli)" | ||
"helpMessage": "Use \"npm run commit\" instead, we use conventional-changelog format :) (https://github.com/commitizen/cz-cli)" | ||
} | ||
}, | ||
"jest": { | ||
"transform": { | ||
".(ts|tsx)": "<rootDir>/node_modules/ts-jest/preprocessor.js" | ||
}, | ||
"testRegex": "(/__tests__/.*|\\.(test|spec))\\.(ts|tsx|js)$", | ||
"moduleFileExtensions": ["ts", "tsx", "js"], | ||
"coveragePathIgnorePatterns": ["/node_modules/", "/test/"], | ||
"moduleFileExtensions": [ | ||
"ts", | ||
"tsx", | ||
"js" | ||
], | ||
"coveragePathIgnorePatterns": [ | ||
"/node_modules/", | ||
"/test/" | ||
], | ||
"coverageThreshold": { | ||
"global": { | ||
"branches": 60, | ||
"functions": 60, | ||
"lines": 60, | ||
"statements": 60 | ||
"branches": 80, | ||
"functions": 80, | ||
"lines": 80, | ||
"statements": 80 | ||
} | ||
}, | ||
"collectCoverage": true, | ||
|
@@ -103,7 +109,9 @@ | |
}, | ||
"dependencies": { | ||
"@types/lodash.foreach": "^4.5.3", | ||
"@types/lodash.mapvalues": "^4.6.3", | ||
"babel-polyfill": "^6.26.0", | ||
"lodash.foreach": "^4.5.0" | ||
"lodash.foreach": "^4.5.0", | ||
"lodash.mapvalues": "^4.6.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { combineReducers as reduxCombineReducers, Action } from 'redux' | ||
import { hasFX, fx, StateWithFx } from './helpers' | ||
import { FXReducer, BatchEffects } from './types' | ||
import mapValues from 'lodash.mapvalues' | ||
|
||
export interface ReducersMapObject { | ||
[key: string]: FXReducer<any, Action> | ||
} | ||
|
||
function combineReducers<A extends Action>(reducers: ReducersMapObject) { | ||
let reducer = reduxCombineReducers(reducers) | ||
|
||
return function(state: any, action: A): StateWithFx<any> { | ||
const newStateWithFx = reducer(state, action) | ||
let batchEffects: BatchEffects = [] | ||
|
||
const newState = mapValues(newStateWithFx, (value: any) => { | ||
if (hasFX(value)) { | ||
let { state, effects } = value | ||
if (Array.isArray(effects)) { | ||
batchEffects = batchEffects.concat(effects) | ||
} else { | ||
batchEffects.push(effects) | ||
} | ||
|
||
return state | ||
} | ||
|
||
return value | ||
}) | ||
|
||
return fx(newState, batchEffects) | ||
} | ||
} | ||
|
||
export { combineReducers } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { Action, StoreEnhancer, Dispatch, Store } from 'redux' | ||
import { StateWithFx } from './helpers' | ||
|
||
export type Effects = { [key: string]: any } | ||
|
||
export type BatchEffects = Effects[] | ||
|
||
export interface StoreCreator { | ||
<S, A extends Action>( | ||
reducer: FXReducer<S, A>, | ||
enhancer?: StoreEnhancer<S> | ||
): FXStore<S> | ||
<S, A extends Action>( | ||
reducer: FXReducer<S, A>, | ||
preloadedState: S, | ||
enhancer?: StoreEnhancer<S> | ||
): FXStore<S> | ||
} | ||
|
||
export interface FXReducer<S, A> { | ||
(state: S | undefined, action: A): S | StateWithFx<S> | ||
} | ||
|
||
export interface FXHandler<S> { | ||
(params: FXParams, getState: () => S, dispatch: Dispatch<S>): void | ||
} | ||
|
||
export interface FXParams { | ||
[key: string]: any | ||
} | ||
|
||
export interface RegisteredFXs<S> { | ||
[key: string]: FXHandler<S> | ||
} | ||
|
||
export type QueuedFX = [string, FXParams] | ||
|
||
export interface FXStore<S> extends Store<S> { | ||
registerFX(id: string, handler: FXHandler<S>): void | ||
} |
Oops, something went wrong.