/* * MIT License * * Copyright (c) 2018 Choko (choko@curioswitch.org) * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ import { InjectableStore } from '../store'; import { DAEMON, ONCE_TILL_UNMOUNT, RESTART_ON_REMOUNT } from './constants'; interface Descriptor { saga: any; mode?: string; } function injectSagaFactory(store: InjectableStore) { return function injectsaga( key: string, descriptor: Descriptor, sagaArgs: any, ) { const newDescriptor = { ...descriptor, mode: descriptor.mode || RESTART_ON_REMOUNT, }; const { saga, mode } = newDescriptor; let hasSaga = Reflect.has(store.injectedSagas, key); if (process.env.NODE_ENV !== 'production') { const oldDescriptor = store.injectedSagas[key]; // enable hot reloading of daemon and once-till-unmount sagas if (hasSaga && oldDescriptor.saga !== saga) { oldDescriptor.task.cancel(); hasSaga = false; } } if ( !hasSaga || (hasSaga && mode !== DAEMON && mode !== ONCE_TILL_UNMOUNT) ) { // eslint-disable-next-line no-param-reassign store.injectedSagas[key] = { ...newDescriptor, task: store.runSaga(saga, sagaArgs), }; } }; } export function ejectSagaFactory(store: InjectableStore) { return function ejectSaga(key: string) { if (Reflect.has(store.injectedSagas, key)) { const descriptor = store.injectedSagas[key]; if (descriptor.mode !== DAEMON) { descriptor.task.cancel(); // Clean up in production; in development we need `descriptor.saga` for hot reloading if (process.env.NODE_ENV === 'production') { // Need some value to be able to detect `ONCE_TILL_UNMOUNT` sagas in `injectSaga` // eslint-disable-next-line no-param-reassign store.injectedSagas[key] = 'done'; } } } }; } export default function getInjectors(store: InjectableStore) { return { injectSaga: injectSagaFactory(store), ejectSaga: ejectSagaFactory(store), }; }