sagaInjectors.js 2.28 KB
Newer Older
Julien Benchetrit's avatar
Julien Benchetrit committed
1
import invariant from 'invariant';
dengxiaofeng's avatar
dengxiaofeng committed
2
import { conformsTo, isEmpty, isFunction, isString } from 'lodash';
Julien Benchetrit's avatar
Julien Benchetrit committed
3 4 5 6 7 8 9 10 11

import checkStore from './checkStore';
import { DAEMON, ONCE_TILL_UNMOUNT, RESTART_ON_REMOUNT } from './constants';

const allowedModes = [RESTART_ON_REMOUNT, DAEMON, ONCE_TILL_UNMOUNT];

const checkKey = key =>
  invariant(
    isString(key) && !isEmpty(key),
dengxiaofeng's avatar
dengxiaofeng committed
12
    '(src/utils...) injectSaga: Expected `key` to be a non empty string',
Julien Benchetrit's avatar
Julien Benchetrit committed
13 14 15 16 17 18 19 20 21
  );

const checkDescriptor = descriptor => {
  const shape = {
    saga: isFunction,
    mode: mode => isString(mode) && allowedModes.includes(mode),
  };
  invariant(
    conformsTo(descriptor, shape),
dengxiaofeng's avatar
dengxiaofeng committed
22
    '(src/utils...) injectSaga: Expected a valid saga descriptor',
Julien Benchetrit's avatar
Julien Benchetrit committed
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
  );
};

export function injectSagaFactory(store, isValid) {
  return function injectSaga(key, descriptor = {}, args) {
    if (!isValid) checkStore(store);

    const newDescriptor = {
      ...descriptor,
      mode: descriptor.mode || DAEMON,
    };
    const { saga, mode } = newDescriptor;

    checkKey(key);
    checkDescriptor(newDescriptor);

    let hasSaga = Reflect.has(store.injectedSagas, key);

    if (process.env.NODE_ENV !== 'production') {
      const oldDescriptor = store.injectedSagas[key];
dengxiaofeng's avatar
dengxiaofeng committed
43

Julien Benchetrit's avatar
Julien Benchetrit committed
44 45 46 47 48 49
      if (hasSaga && oldDescriptor.saga !== saga) {
        oldDescriptor.task.cancel();
        hasSaga = false;
      }
    }

50
    if (!hasSaga || (hasSaga && mode !== DAEMON && mode !== ONCE_TILL_UNMOUNT)) {
Julien Benchetrit's avatar
Julien Benchetrit committed
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
      /* eslint-disable no-param-reassign */
      store.injectedSagas[key] = {
        ...newDescriptor,
        task: store.runSaga(saga, args),
      };
      /* eslint-enable no-param-reassign */
    }
  };
}

export function ejectSagaFactory(store, isValid) {
  return function ejectSaga(key) {
    if (!isValid) checkStore(store);

    checkKey(key);

    if (Reflect.has(store.injectedSagas, key)) {
      const descriptor = store.injectedSagas[key];
      if (descriptor.mode && descriptor.mode !== DAEMON) {
        descriptor.task.cancel();
        if (process.env.NODE_ENV === 'production') {
          store.injectedSagas[key] = 'done'; // eslint-disable-line no-param-reassign
        }
      }
    }
  };
}

export default function getInjectors(store) {
  checkStore(store);

  return {
    injectSaga: injectSagaFactory(store, true),
    ejectSaga: ejectSagaFactory(store, true),
  };
}