Skip to content
This repository has been archived by the owner on Oct 19, 2019. It is now read-only.

Commit

Permalink
fix: compatiable redux pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
wxnet2013 committed Sep 8, 2018
1 parent d2935c1 commit 523da44
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 38 deletions.
26 changes: 13 additions & 13 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import produce from "immer";

export default function rxloopImmer(
conf = {},
) {
const config = {
disabled: [],
...conf,
};
export default function rxloopImmer() {
return function init() {
function createReducer(action = {}, reducer = () => {}, model = '') {
return config.disabled.indexOf(model) > -1 ?
(state) => reducer(state, action) :
(state) => produce(state, draft => {
reducer(draft, action);
});
function createReducer(action = {}, reducer = () => {}) {
return (state) => {
const rtn = produce(state, draft => {
const compatiableRet = reducer(draft, action);
if (compatiableRet !== undefined) {
// which means you are use redux pattern
// it's compatiable. https://github.com/mweststrate/immer#returning-data-from-producers
return compatiableRet;
}
});
return rtn === undefined ? {} : rtn;
}
}
this.createReducer = createReducer;
};
Expand Down
26 changes: 1 addition & 25 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ import immer from '../src/';

const app = rxloop({
plugins: [
immer(
{ disabled: [ 'a' ] }
),
immer(),
],
});

Expand All @@ -28,20 +26,6 @@ app.model({
});
app.stream('test').subscribe();

const aState = {
a: 1,
};
app.model({
name: 'a',
state: aState,
reducers: {
add(state) {
state.a = 2;
},
},
});
app.stream('a').subscribe();

describe('Basic usage', () => {
test('immer number', () => {
app.dispatch({
Expand Down Expand Up @@ -69,12 +53,4 @@ describe('Basic usage', () => {
arr: [1, 2],
});
});
test('config test', () => {
app.dispatch({
type: 'a/add',
});
expect(aState).toEqual({
a: 2,
});
});
});

0 comments on commit 523da44

Please sign in to comment.