Example #1
0
export function take(patternOrChannel = '*', multicastPattern) {
  if (process.env.NODE_ENV !== 'production' && arguments.length) {
    check(arguments[0], is.notUndef, 'take(patternOrChannel): patternOrChannel is undefined')
  }
  if (is.pattern(patternOrChannel)) {
    return makeEffect(effectTypes.TAKE, { pattern: patternOrChannel })
  }
  if (is.multicast(patternOrChannel) && is.notUndef(multicastPattern) && is.pattern(multicastPattern)) {
    return makeEffect(effectTypes.TAKE, { channel: patternOrChannel, pattern: multicastPattern })
  }
  if (is.channel(patternOrChannel)) {
    return makeEffect(effectTypes.TAKE, { channel: patternOrChannel })
  }
  if (process.env.NODE_ENV !== 'production') {
    throw new Error(`take(patternOrChannel): argument ${patternOrChannel} is not valid channel or a valid pattern`)
  }
}
Example #2
0
export function cancel(taskOrTasks = SELF_CANCELLATION) {
  if (process.env.NODE_ENV !== 'production') {
    if (arguments.length > 1) {
      throw new Error(
        'cancel(...tasks) is not supported any more. Please use cancel([...tasks]) to cancel multiple tasks.',
      )
    }
    if (is.array(taskOrTasks)) {
      taskOrTasks.forEach(t => {
        check(t, is.task, `cancel([...tasks]): argument ${t} is not a valid Task object ${TEST_HINT}`)
      })
    } else if (taskOrTasks !== SELF_CANCELLATION && is.notUndef(taskOrTasks)) {
      check(taskOrTasks, is.task, `cancel(task): argument ${taskOrTasks} is not a valid Task object ${TEST_HINT}`)
    }
  }

  return makeEffect(effectTypes.CANCEL, taskOrTasks)
}
export default function parseEffect(effect: Object): Object {
  let parsedEffect;

  switch (true) {
    case is.notUndef((parsedEffect = asEffect.take(effect))):
      return {
        type: TAKE,
        effect: parsedEffect,
        providerKey: 'take',
      };

    case is.notUndef((parsedEffect = asEffect.put(effect))):
      return {
        type: PUT,
        effect: parsedEffect,
        providerKey: 'put',
      };

    case is.notUndef((parsedEffect = asEffect.race(effect))):
      return createRace(parsedEffect, { providerKey: 'race' });

    case is.notUndef((parsedEffect = asEffect.call(effect))):
      return {
        type: CALL,
        effect: parsedEffect,
        providerKey: 'call',
      };

    case is.notUndef((parsedEffect = asEffect.cancel(effect))):
      return {
        type: CANCEL,
        effect: parsedEffect,
        providerKey: 'cancel',
      };

    case is.notUndef((parsedEffect = asEffect.cancelled(effect))):
      return {
        type: CANCELLED,
        effect: parsedEffect,
        providerKey: 'cancelled',
      };

    case is.notUndef((parsedEffect = asEffect.cps(effect))):
      return {
        type: CPS,
        effect: parsedEffect,
        providerKey: 'cps',
      };

    case is.notUndef((parsedEffect = asEffect.flush(effect))):
      return {
        type: FLUSH,
        effect: parsedEffect,
        providerKey: 'flush',
      };

    case is.notUndef((parsedEffect = asEffect.fork(effect))):
      return {
        type: FORK,
        effect: parsedEffect,
        providerKey: parsedEffect.detached ? 'spawn' : 'fork',
      };

    case is.notUndef((parsedEffect = asEffect.getContext(effect))):
      return {
        type: GET_CONTEXT,
        effect: parsedEffect,
        providerKey: 'getContext',
      };

    case is.notUndef((parsedEffect = asEffect.join(effect))):
      return {
        type: JOIN,
        effect: parsedEffect,
        providerKey: 'join',
      };

    case is.notUndef((parsedEffect = asEffect.select(effect))):
      return {
        type: SELECT,
        effect: parsedEffect,
        providerKey: 'select',
      };

    case is.notUndef((parsedEffect = asEffect.setContext(effect))):
      return {
        type: SET_CONTEXT,
        effect: parsedEffect,
        providerKey: 'setContext',
      };

    case is.notUndef((parsedEffect = asEffect.actionChannel(effect))):
      return {
        type: ACTION_CHANNEL,
        effect: parsedEffect,
        providerKey: 'actionChannel',
      };

    case is.notUndef((parsedEffect = asEffect.all(effect))):
      return createAll(parsedEffect);

    default:
      return { type: NONE };
  }
}