-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
59 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
27
packages/core/src/dx/create-effect/create-effect-handler.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}; | ||
} |