Skip to content

Commit

Permalink
feat(redux): support applied middlewares, add debug mode
Browse files Browse the repository at this point in the history
BREAKING CHANGE: middlewares not can be handled by user.
  • Loading branch information
Genuifx committed Oct 8, 2019
1 parent 68b7090 commit f85d7f6
Show file tree
Hide file tree
Showing 4 changed files with 2,229 additions and 1,484 deletions.
13 changes: 3 additions & 10 deletions packages/wxa-redux/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 14 additions & 2 deletions packages/wxa-redux/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const combine = (registryReducers, userReducers) => {
};

const checkAndFilterDataField = (data, maxSize = 1024 * 1000)=>{
const check = (key, value)=>{
const check = (key, value) => {
let str = JSON.stringify(value);
if (typeof str !== 'string') return true;

Expand Down Expand Up @@ -90,26 +90,38 @@ export const wxaRedux = (options = {}) => {
// get options.
let args = [];
let userReducers;
let debug = false;
if (Array.isArray(options)) {
userReducers = options[0];
// object reducer
args = [combine(reducerRegistry.getReducers(), userReducers), ...options.slice(1)];
} else {
userReducers = options.reducers;
debug = options.debug;
let {
middlewares,
initialState
} = options;

args = [combine(reducerRegistry.getReducers(), userReducers), initialState];
if (Array.isArray(middlewares)) args.push(applyMiddleware(...middlewares));
else if (typeof middlewares === 'function') args.push(middlewares);
}

// create Store directly;
// cause the reducer may be attached at subpackages.
let store = createStore.apply(null, args);
reducerRegistry.setChangeListener((reducer)=>{
store.replaceReducer(combine(reducer, userReducers));
let reducers = combine(reducer, userReducers);
if(debug) {
console.group('%c[@wxa/redux] Replacing reducers', 'font-size: 12px; color: green;');
console.table({
'registered reducer': reducer,
'init reducer': userReducers
});
console.groupEnd();
}
store.replaceReducer(reducers);
});

let syncStore = function(){
Expand Down
27 changes: 14 additions & 13 deletions packages/wxa-redux/test/wxa-redux.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,22 +80,23 @@ describe('App register redux', ()=>{
expect(vm.$store).not.toBeFalsy();
})

test('middlewares not array', ()=>{
let mw = jest.fn();
// middlewares not can be handled by user.
// test('middlewares not array', ()=>{
// let mw = jest.fn();

let reduxFnMW = wxaRedux({
reducers: combineReducers({todo}),
middlewares: function(store){
return function(){
mw();
}
}
});
// let reduxFnMW = wxaRedux({
// reducers: combineReducers({todo}),
// middlewares: function(store){
// return function(){
// mw();
// }
// }
// });

reduxFnMW({}, 'App');
// reduxFnMW({}, 'App');

expect(mw).not.toHaveBeenCalled();
})
// expect(mw).not.toHaveBeenCalled();
// })

test('throw error while type undefined', ()=>{
let reduxFn = wxaRedux({}, 'Hello');
Expand Down
Loading

0 comments on commit f85d7f6

Please sign in to comment.