Skip to content

Commit

Permalink
Merge pull request #1661 from sjy/saga_take
Browse files Browse the repository at this point in the history
enhance: saga-effect take should be able to take multi actions;
  • Loading branch information
sorrycc authored Jun 1, 2018
2 parents b01c9a6 + 2d72ff6 commit e8fdac3
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 @@ -124,6 +124,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 @@ -123,6 +123,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 e8fdac3

Please sign in to comment.