All files action.ts

100% Statements 70/70
100% Branches 17/17
100% Functions 7/7
100% Lines 70/70

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 113 114 115 116 117 118 119 1201x   1x     1x                                                 1x 316x 337x 337x 337x 337x 337x 337x 337x 316x 316x 316x 316x   1x 21x 30x 30x 30x 30x 30x 30x 30x 21x 21x 21x 21x                   1x 319x 319x 319x 319x 313x 319x 4x 4x 2x 2x 2x 2x 2x 319x                       1x 11x 11x 11x 11x 11x 3x 3x 11x 2x 2x 2x 2x 2x   9x 6x 6x 6x 6x 6x 6x 6x 9x 9x 9x 9x  
import { FlowfulError } from './error';
 
export const ACTION_MODE_SINGLE = 'single';
export type ActionModeSingle = typeof ACTION_MODE_SINGLE;
 
export const ACTION_MODE_MULTI = 'multi';
export type ActionModeMulti = typeof ACTION_MODE_MULTI;
 
export type ActionMode = ActionModeSingle | ActionModeMulti;
 
export interface Action<Payload = any, Mode extends ActionMode = ActionMode> {
  readonly type: string;
  readonly mode: Mode;
  readonly creator: ActionCreator<Payload>;
  readonly payload: Payload;
}
 
export type Payload<T> = T extends ActionCreator<infer P> ? P : never;
 
export interface ActionCreator<
  Payload = any,
  Mode extends ActionMode = ActionMode,
  Arguments extends any[] = [Payload],
  S extends Action<Payload, Mode> = Action<Payload, Mode>
> {
  (...args: Arguments): S;
  readonly type: string;
  readonly mode: Mode;
}
 
export function actionCreatorSingle<Payload = any>(type: string): ActionCreator<Payload, ActionModeSingle, [Payload]> {
  function ActionCreator(payload: Payload) {
    return {
      type,
      mode: ACTION_MODE_SINGLE as ActionModeSingle,
      payload,
      creator: ActionCreator,
    };
  }
  ActionCreator.type = type;
  ActionCreator.mode = ACTION_MODE_SINGLE as ActionModeSingle;
  return ActionCreator;
}
 
export function actionCreatorMulti<Payload = any>(type: string): ActionCreator<Payload, ActionModeMulti, [Payload]> {
  function ActionCreator(payload: Payload) {
    return {
      type,
      mode: ACTION_MODE_MULTI as ActionModeMulti,
      payload,
      creator: ActionCreator,
    };
  }
  ActionCreator.type = type;
  ActionCreator.mode = ACTION_MODE_MULTI as ActionModeMulti;
  return ActionCreator;
}
 
export function actionCreator<Payload = any>(
  type: string,
  mode?: ActionModeSingle
): ActionCreator<Payload, ActionModeSingle>;
export function actionCreator<Payload = any>(
  type: string,
  mode: ActionModeMulti
): ActionCreator<Payload, ActionModeMulti>;
export function actionCreator<Payload = any>(
  type: string,
  mode: ActionMode = ACTION_MODE_SINGLE
): ActionCreator<Payload> {
  if (mode === ACTION_MODE_SINGLE) {
    return actionCreatorSingle<Payload>(type);
  } else if (mode === ACTION_MODE_MULTI) {
    return actionCreatorMulti<Payload>(type);
  } else {
    throw new FlowfulError(
      `[Flowful][actionCreator] Invalid mode: "${mode}" for action "${type}". Must be "${ACTION_MODE_SINGLE}" or "${ACTION_MODE_MULTI}".`,
      { type, mode }
    );
  }
}
 
export function actionCreatorDefault<Payload = any>(
  type: string,
  defaultPayload: Payload,
  mode?: ActionModeSingle
): ActionCreator<Payload, ActionModeSingle, [Payload] | []>;
export function actionCreatorDefault<Payload = any>(
  type: string,
  defaultPayload: Payload,
  mode: ActionModeMulti
): ActionCreator<Payload, ActionModeMulti, [Payload] | []>;
export function actionCreatorDefault<Payload = any, Mode extends ActionMode = ActionModeSingle>(
  type: string,
  defaultPayload: Payload,
  mode?: Mode
): ActionCreator<Payload, Mode, [Payload] | []> {
  if (mode === undefined) {
    mode = ACTION_MODE_SINGLE as Mode;
  }
  if (mode !== ACTION_MODE_SINGLE && mode !== ACTION_MODE_MULTI) {
    throw new FlowfulError(
      `[Flowful][actionCreatorDefault] Invalid action mode: "${mode}" for action "${type}". Must be "${ACTION_MODE_SINGLE}" or "${ACTION_MODE_MULTI}".`,
      { type, mode }
    );
  }
 
  function ActionCreatorDefault(payload?: Payload) {
    return {
      type,
      mode,
      payload: payload !== undefined ? payload : defaultPayload,
      creator: ActionCreatorDefault,
    };
  }
  ActionCreatorDefault.type = type;
  ActionCreatorDefault.mode = mode;
  return ActionCreatorDefault as ActionCreator<Payload, Mode, [Payload] | []>;
}