Skip to content

Commit

Permalink
enhance(take): saga-effect take should be able to take multi actions;
Browse files Browse the repository at this point in the history
  • Loading branch information
sjy committed May 23, 2018
1 parent e114cfb commit 2d72ff6
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
10 changes: 10 additions & 0 deletions packages/dva-core/src/getSaga.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,16 @@ function createEffects(model) {
if (typeof type === 'string') {
assertAction(type, 'sagaEffects.take');
return sagaEffects.take(prefixType(type, model));
} else if (Array.isArray(type)) {
return sagaEffects.take(
type.map(t => {
if (typeof t === 'string') {
assertAction(t, 'sagaEffects.take');
return prefixType(type, model);
}
return t;
})
);
} else {
return sagaEffects.take(type);
}
Expand Down
38 changes: 38 additions & 0 deletions packages/dva-core/test/effects.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,44 @@ describe('effects', () => {
}, 300);
});

it('take with array of actions', () => {
const app = create();
app.model({
namespace: 'count',
state: null,
reducers: {
addRequest() {
return 1;
},
addFailure() {
return -1;
},
addSuccess() {
return 0;
},
},
effects: {
*add(action, { put }) {
yield put({ type: 'addRequest' });
if (action.amount > 0.5) {
yield put({ type: 'addSuccess' });
} else {
yield put({ type: 'addFailure' });
}
},
*test(action, { put, take }) {
yield put({ type: 'add', amount: action.amount });
yield take(['addSuccess', 'addFailure']);
},
},
});
app.start();
app._store.dispatch({ type: 'count/test', amount: 0 });
expect(app._store.getState().count).toEqual(-1);
app._store.dispatch({ type: 'count/test', amount: 1 });
expect(app._store.getState().count).toEqual(0);
});

it('dispatch action for other models', () => {
const app = create();
app.model({
Expand Down

0 comments on commit 2d72ff6

Please sign in to comment.