Skip to content

Commit

Permalink
feat: support throttle effect type
Browse files Browse the repository at this point in the history
  • Loading branch information
sorrycc committed Oct 9, 2016
1 parent e85564b commit a2e887c
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 2 deletions.
20 changes: 18 additions & 2 deletions src/createDva.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';
import { Provider } from 'react-redux';
import { createStore, applyMiddleware, compose, combineReducers } from 'redux';
import createSagaMiddleware, { takeEvery, takeLatest } from 'redux-saga';
import createSagaMiddleware, { takeEvery, takeLatest, throttle } from 'redux-saga';
import handleActions from 'redux-actions/lib/handleActions';
import * as sagaEffects from 'redux-saga/effects';
import isPlainObject from 'is-plain-object';
Expand Down Expand Up @@ -251,13 +251,25 @@ export default function createDva(createOpts) {
function getWatcher(key, _effect, model, onError) {
let effect = _effect;
let type = 'takeEvery';
let ms;

if (Array.isArray(_effect)) {
effect = _effect[0];
const opts = _effect[1];
if (opts && opts.type) {
type = opts.type;
if (type === 'throttle') {
invariant(
opts.ms,
'app.start: opts.ms should be defined if type is throttle'
);
ms = opts.ms;
}
}
invariant(['watcher', 'takeEvery', 'takeLatest'].indexOf(type) > -1, 'app.start: effect type should be takeEvery, takeLatest or watcher')
invariant(
['watcher', 'takeEvery', 'takeLatest', 'throttle'].indexOf(type) > -1,
'app.start: effect type should be takeEvery, takeLatest, throttle or watcher'
);
}

function *sagaWithCatch(...args) {
Expand All @@ -278,6 +290,10 @@ export default function createDva(createOpts) {
return function*() {
yield takeLatest(key, sagaWithOnEffect);
};
case 'throttle':
return function*() {
yield throttle(ms, key, sagaWithOnEffect);
};
// takeEvery
default:
return function*() {
Expand Down
43 changes: 43 additions & 0 deletions test/effects-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,49 @@ describe('effects', () => {
}, 200);
});

xit('type: throttle throw error if no ms', () => {
const app = dva();
app.model({
namespace: 'count',
state: 0,
effects: {
addDelay: [function*() {}, { type: 'throttle' }],
},
});
app.router(_ => 1);
expect(() => {
app.start();
}).toThrow(/app.start: opts.ms should be defined if type is throttle/);
});

it('type: throttle', (done) => {
const app = dva();
app.model({
namespace: 'count',
state: 0,
reducers: {
add(state, { payload }) { return state + payload || 1 },
},
effects: {
addDelay: [function*({ payload }, { call, put }) {
yield call(delay, 100);
yield put({ type: 'add', payload });
}, { type: 'throttle', ms: 100 }],
},
});
app.router(_ => 1);
app.start();

// Only catch the last one.
app._store.dispatch({ type: 'count/addDelay', payload: 2 });
app._store.dispatch({ type: 'count/addDelay', payload: 3 });

setTimeout(() => {
expect(app._store.getState().count).toEqual(2);
done();
}, 200);
});


it('type: watcher', (done) => {
const watcher = { type: 'watcher' };
Expand Down

0 comments on commit a2e887c

Please sign in to comment.