All files / src effects.js

100% Statements 35/35
100% Branches 2/2
100% Functions 10/10
100% Lines 33/33
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112                                17x           9x   9x   9x     9x   9x   17x   17x 17x   17x   2x 2x 2x       2x       2x   2x   1x       1x   1x   1x 1x     1x       1x         2x   1x   1x   1x         2x       2x                 9x 9x 2x     9x              
import {
    fork,
    cancel,
    take,
    call,
    put,
    select,
    race,
    all,
    takeEvery
} from "redux-saga/effects";
import {createAction} from 'redux-actions'
import {mergePlainObject, renamePlainObjectKey, mapKeys} from './util'
 
function mergerEffects(modelList) {
 
    return modelList.map(({namespace, effects}) => ({[namespace]: renamePlainObjectKey(namespace, effects)})).reduce(mergePlainObject, {})
 
}
 
function getSagas(modelList) {
 
    const effectsModuleMap = mergerEffects(modelList);
 
    return function* () {
 
        const globalTask = yield fork(function* () {
 
 
            const effectModuleKeyList = mapKeys(effectsModuleMap);
 
            for (let i = 0; i < effectModuleKeyList.length; i++) {
 
                const effectModuleKey = effectModuleKeyList[i];
 
                const effectSagaMap = effectsModuleMap[effectModuleKey];
                const effectKeyList = mapKeys(effectSagaMap);
 
                for (let j = 0; j < effectKeyList.length; j++) {
 
                    const effectSagaKey = effectKeyList[j];
                    const effectSaga = effectSagaMap[effectSagaKey];
                    const defaultArgs = {
                        call, put, select, race, all
                    }
 
                    const watcher = function* () {
 
                        function* f(...args) {
 
                            const action = args[1];
 
                            if (!action.autoStatus){
 
                                yield effectSaga(...args)
 
                            } else {
 
                                const [namespace, effectName] = effectSagaKey.split('/')
 
                                const Status = 'Status'
 
                                const currStatus = yield select(
                                    state => state[namespace]["@@status"]
                                );
 
                                const startStatus = {
                                    ...currStatus,
                                    [`${effectName}${Status}`]: true
                                };
                                const endStatus = {
                                    ...currStatus,
                                    [`${effectName}${Status}`]: false
                                };
 
                                const onChangeStatus = createAction(`${namespace}/@@mergeState`, payload => payload)
 
                                yield put(onChangeStatus({["@@status"]: startStatus}))
 
                                yield effectSaga(...args)
 
                                yield put(onChangeStatus({["@@status"]: endStatus}))
 
                            }
                        }
 
                        yield takeEvery(effectSagaKey, f, defaultArgs)
 
                    }
 
                    yield fork(watcher)
 
                }
 
 
            }
 
        })
 
        const cancelAllFork = function* () {
            yield take(`@@teeMoCore/cancel`)
            yield cancel(globalTask)
        }
 
        yield fork(cancelAllFork)
 
    }
}
 
export {
    getSagas
}