/**
 * 仅供typescript下使用，将redux-saga的yield select(selector)结果转换为实际类型
 * For typescript only, cast redux-saga yield select(selector) to actual result
 * 用法/usage： const result = asResult(selector, yield select(selector))
 */ export function asResult < T > ( fn , result ) { return result ; } /**
 * 常用工具方法 - 生成reducer
 * helper - generate simple reducer
 * 用法/usage： num : reduceFromPayload(types.SET_NUM, 0)
 */ export function reduceFromPayload < TState , TType = string > ( actionType , initialState ) { return ( state = initialState , action ) => { if ( action . type === actionType ) { return action . payload ; } return state ; } ; } /**
 * 常用工具方法 - 生成creator
 * helper - generate simple action creator
 * 用法/usage： inc: createToPayload(types.INC)
 */ export function createToPayload < TState , TType = string > ( actionType ) { return ( payload ) => ( { type : actionType , payload } ) ; } param promise export function * resolvePromise < T > ( promise ) { return yield promise ; } param { * } fn return { Function } deprecated export function memorize < T > ( fn ) { const cacheKey = "_sagaDuckMemorized" ; const idKey = "_sagaDuckUniqId" ; const fnId = fn [ idKey ] || ( fn [ idKey ] = generateId ( "MEMORIZE-FN" ) ) ; return function memorizedFn ( duckComponent ) { let cacheHost ; let props ; if ( duckComponent . isReactComponent && duckComponent . props ) { props = duckComponent . props ; } else { props = duckComponent ; duckComponent = null ; } const { duck , dispatch } = props ; cacheHost = duckComponent || duck ; const cache = cacheHost [ cacheKey ] || ( cacheHost [ cacheKey ] = { } ) ; if ( ! dispatch [ idKey ] ) { dispatch [ idKey ] = generateId ( "DISPATCH" ) ; } const key = duck . id + ":" + dispatch [ idKey ] + ":" + fnId ; if ( ! ( key in cache ) ) { cache [ key ] = fn ( duck , dispatch ) ; } return cache [ key ] ; } ; } let idSeed = 1 ; export function generateId ( prefix = "SAGA-DUCK" ) { return `${ prefix }-${ idSeed ++ }` ; }