Skip to content

Commit

Permalink
Merge 907487d into 1a7b3c9
Browse files Browse the repository at this point in the history
  • Loading branch information
DiamondYuan committed Jun 14, 2019
2 parents 1a7b3c9 + 907487d commit 59a775f
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 21 deletions.
1 change: 1 addition & 0 deletions packages/dva-core/package.json
Expand Up @@ -39,6 +39,7 @@
"redux": "4.x"
},
"devDependencies": {
"mm": "^2.5.0",
"redux": "^4.0.1"
},
"files": [
Expand Down
23 changes: 14 additions & 9 deletions packages/dva-core/src/getSaga.js
Expand Up @@ -4,11 +4,11 @@ import { effects as sagaEffects } from 'redux-saga';
import { NAMESPACE_SEP } from './constants';
import prefixType from './prefixType';

export default function getSaga(effects, model, onError, onEffect) {
export default function getSaga(effects, model, onError, onEffect, opts = {}) {
return function*() {
for (const key in effects) {
if (Object.prototype.hasOwnProperty.call(effects, key)) {
const watcher = getWatcher(key, effects[key], model, onError, onEffect);
const watcher = getWatcher(key, effects[key], model, onError, onEffect, opts);
const task = yield sagaEffects.fork(watcher);
yield sagaEffects.fork(function*() {
yield sagaEffects.take(`${model.namespace}/@@CANCEL_EFFECTS`);
Expand All @@ -19,7 +19,7 @@ export default function getSaga(effects, model, onError, onEffect) {
};
}

function getWatcher(key, _effect, model, onError, onEffect) {
function getWatcher(key, _effect, model, onError, onEffect, opts) {
let effect = _effect;
let type = 'takeEvery';
let ms;
Expand Down Expand Up @@ -47,7 +47,7 @@ function getWatcher(key, _effect, model, onError, onEffect) {
args.length > 0 ? args[0] : {};
try {
yield sagaEffects.put({ type: `${key}${NAMESPACE_SEP}@@start` });
const ret = yield effect(...args.concat(createEffects(model)));
const ret = yield effect(...args.concat(createEffects(model, opts)));
yield sagaEffects.put({ type: `${key}${NAMESPACE_SEP}@@end` });
resolve(ret);
} catch (e) {
Expand Down Expand Up @@ -81,13 +81,18 @@ function getWatcher(key, _effect, model, onError, onEffect) {
}
}

function createEffects(model) {
function createEffects(model, opts) {
function assertAction(type, name) {
invariant(type, 'dispatch: action should be a plain Object with type');
warning(
type.indexOf(`${model.namespace}${NAMESPACE_SEP}`) !== 0,
`[${name}] ${type} should not be prefixed with namespace ${model.namespace}`,
);

const { namespacePrefixWarning = true } = opts;

if (namespacePrefixWarning) {
warning(
type.indexOf(`${model.namespace}${NAMESPACE_SEP}`) !== 0,
`[${name}] ${type} should not be prefixed with namespace ${model.namespace}`,
);
}
}
function put(action) {
const { type } = action;
Expand Down
6 changes: 4 additions & 2 deletions packages/dva-core/src/index.js
Expand Up @@ -75,7 +75,7 @@ export function create(hooksAndOpts = {}, createOpts = {}) {
store.asyncReducers[m.namespace] = getReducer(m.reducers, m.state, plugin._handleActions);
store.replaceReducer(createReducer());
if (m.effects) {
store.runSaga(app._getSaga(m.effects, m, onError, plugin.get('onEffect')));
store.runSaga(app._getSaga(m.effects, m, onError, plugin.get('onEffect'), hooksAndOpts));
}
if (m.subscriptions) {
unlisteners[m.namespace] = runSubscription(m.subscriptions, m, app, onError);
Expand Down Expand Up @@ -177,7 +177,9 @@ export function create(hooksAndOpts = {}, createOpts = {}) {
const reducers = { ...initialReducer };
for (const m of app._models) {
reducers[m.namespace] = getReducer(m.reducers, m.state, plugin._handleActions);
if (m.effects) sagas.push(app._getSaga(m.effects, m, onError, plugin.get('onEffect')));
if (m.effects) {
sagas.push(app._getSaga(m.effects, m, onError, plugin.get('onEffect'), hooksAndOpts));
}
}
const reducerEnhancer = plugin.get('onReducer');
const extraReducers = plugin.get('extraReducers');
Expand Down
52 changes: 43 additions & 9 deletions packages/dva-core/test/effects.test.js
@@ -1,4 +1,5 @@
import expect from 'expect';
import mm from 'mm';
import { create } from '../src/index';

const delay = timeout => new Promise(resolve => setTimeout(resolve, timeout));
Expand Down Expand Up @@ -30,8 +31,8 @@ describe('effects', () => {
}, 200);
});

it('put action with namespace will get a warning', done => {
const app = create();
function testAppCreator(opts) {
const app = create(opts);
app.model({
namespace: 'count',
state: 0,
Expand All @@ -41,19 +42,52 @@ describe('effects', () => {
},
},
effects: {
*addDelay({ payload }, { put, call }) {
yield call(delay, 100);
*putWithNamespace({ payload }, { put }) {
yield put({ type: 'count/add', payload });
},
*putWithoutNamespace({ payload }, { put }) {
yield put({ type: 'add', payload });
},
},
});
return app;
}

it('put action with namespace will get a warning', () => {
const app = testAppCreator();
const logs = [];
mm(console, 'error', log => {
logs.push(log);
});
app.start();
app._store.dispatch({ type: 'count/addDelay', payload: 2 });
expect(app._store.getState().count).toEqual(0);
setTimeout(() => {
expect(app._store.getState().count).toEqual(2);
done();
}, 200);
expect(logs.length).toEqual(0);
app._store.dispatch({ type: 'count/putWithNamespace', payload: 2 });
expect(logs.length).toEqual(1);
expect(logs[0]).toEqual(
'Warning: [sagaEffects.put] count/add should not be prefixed with namespace count',
);
app._store.dispatch({ type: 'count/putWithoutNamespace', payload: 2 });
expect(logs.length).toEqual(1);
expect(app._store.getState().count).toEqual(4);
mm.restore();
});

it('test disable namespacePrefixWarning', () => {
const app = testAppCreator({ namespacePrefixWarning: false });
const logs = [];
mm(console, 'error', log => {
logs.push(log);
});
app.start();
expect(app._store.getState().count).toEqual(0);
expect(logs.length).toEqual(0);
app._store.dispatch({ type: 'count/putWithNamespace', payload: 2 });
expect(logs.length).toEqual(0);
app._store.dispatch({ type: 'count/putWithoutNamespace', payload: 2 });
expect(logs.length).toEqual(0);
expect(app._store.getState().count).toEqual(4);
mm.restore();
});

it('put multi effects in order', done => {
Expand Down
3 changes: 2 additions & 1 deletion packages/dva/index.d.ts
Expand Up @@ -29,6 +29,7 @@ export interface Hooks {
}

export type DvaOption = Hooks & {
namespacePrefixWarning: boolean,
initialState?: Object,
history?: Object,
}
Expand All @@ -44,7 +45,7 @@ export interface EffectsCommandMap {

export type Effect = (action: AnyAction, effects: EffectsCommandMap) => void;
export type EffectType = 'takeEvery' | 'takeLatest' | 'watcher' | 'throttle';
export type EffectWithType = [Effect, { type : EffectType }];
export type EffectWithType = [Effect, { type: EffectType }];
export type Subscription = (api: SubscriptionAPI, done: Function) => void;
export type ReducersMapObjectWithEnhancer = [ReducersMapObject, ReducerEnhancer];

Expand Down

0 comments on commit 59a775f

Please sign in to comment.