Skip to content

Commit

Permalink
feat(@dxjs/core): 拆分 combin saga
Browse files Browse the repository at this point in the history
  • Loading branch information
taixw2 committed Apr 28, 2020
1 parent 0841a71 commit d8419a4
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 42 deletions.
48 changes: 6 additions & 42 deletions packages/core/src/dx/create-effect/combin-saga.ts
Original file line number Diff line number Diff line change
@@ -1,49 +1,13 @@
import { EffectTypeInterface } from '@dxjs/shared/interfaces/dx-effect-type.interface';
import { DxModelInterface } from '@dxjs/shared/interfaces/dx-model.interface';
import { AnyAction } from 'redux';
import { isGenerator } from './is-generator';
import { TAKE_LATEST, TAKE_EVERY, TAKE_LEADING, THROTTLE } from '@dxjs/shared/symbol';
import { spawn, takeLatest, takeEvery, takeLeading, throttle, ForkEffect } from 'redux-saga/effects';
const invariant = require('invariant');
import { ForkEffect } from 'redux-saga/effects';
import { createEffectHandler } from './create-effect-handler';
import { createEffectFork } from './create-effect-fork';

export function combinSaga(model: DxModelInterface, meta: EffectTypeInterface): ForkEffect {
function* effectHandler(action: AnyAction): Generator {
try {
const currentEffect = Reflect.get(model, meta.name);
if (__DEV__) {
invariant(
currentEffect && isGenerator(currentEffect),
'副作用函数不是一个 generator 函数,函数名为: %s, 如果确定不是此原因,请提交 issus: %s',
meta.name,
'__ISSUE__',
);
}

const resolve = yield currentEffect.call(model, action.payload);
action.__dxjs_resolve?.(resolve);
return resolve;
} catch (error) {
action.__dxjs_reject?.(error);
}
function* effect(): Generator {
createEffectHandler(model, meta);
}

return spawn(function*() {
switch (meta.helperType) {
case TAKE_EVERY:
yield takeEvery(meta.actionType, effectHandler);
break;
case TAKE_LATEST:
yield takeLatest(meta.actionType, effectHandler);
break;
case TAKE_LEADING:
yield takeLeading(meta.actionType, effectHandler);
break;
case THROTTLE:
yield throttle(meta.value[0] ?? 350, meta.actionType, effectHandler);
break;
default:
yield takeEvery(meta.actionType, effectHandler);
break;
}
});
return createEffectFork(meta, effect);
}
26 changes: 26 additions & 0 deletions packages/core/src/dx/create-effect/create-effect-fork.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { EffectTypeInterface } from '@dxjs/shared/interfaces/dx-effect-type.interface';
import { TAKE_LATEST, TAKE_EVERY, TAKE_LEADING, THROTTLE } from '@dxjs/shared/symbol';
import { spawn, takeLatest, takeEvery, takeLeading, throttle, ForkEffect } from 'redux-saga/effects';
import { AnyAction } from 'redux';

export function createEffectFork(meta: EffectTypeInterface, effectHandler: (action: AnyAction) => Generator): ForkEffect {
return spawn(function*() {
switch (meta.helperType) {
case TAKE_EVERY:
yield takeEvery(meta.actionType, effectHandler);
break;
case TAKE_LATEST:
yield takeLatest(meta.actionType, effectHandler);
break;
case TAKE_LEADING:
yield takeLeading(meta.actionType, effectHandler);
break;
case THROTTLE:
yield throttle(meta.value[0] ?? 350, meta.actionType, effectHandler);
break;
default:
yield takeEvery(meta.actionType, effectHandler);
break;
}
});
}
27 changes: 27 additions & 0 deletions packages/core/src/dx/create-effect/create-effect-handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { AnyAction } from 'redux';
import { isGenerator } from './is-generator';
import { EffectTypeInterface } from '@dxjs/shared/interfaces/dx-effect-type.interface';
import { DxModelInterface } from '@dxjs/shared/interfaces/dx-model.interface';
const invariant = require('invariant');

export function createEffectHandler(model: DxModelInterface, meta: EffectTypeInterface) {
return function*(action: AnyAction): Generator {
try {
const currentEffect = Reflect.get(model, meta.name);
if (__DEV__) {
invariant(
currentEffect && isGenerator(currentEffect),
'副作用函数不是一个 generator 函数,函数名为: %s, 如果确定不是此原因,请提交 issus: %s',
meta.name,
'__ISSUE__',
);
}

const resolve = yield currentEffect.call(model, action.payload);
action.__dxjs_resolve?.(resolve);
return resolve;
} catch (error) {
action.__dxjs_reject?.(error);
}
};
}

0 comments on commit d8419a4

Please sign in to comment.