All files / xstate/src actions.ts

90.77% Statements 59/65
73.68% Branches 28/38
94.44% Functions 17/18
90.63% Lines 58/64

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 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 2421x                                       1x 1x   1x   1x       1681x 1404x         1404x     277x             496x 493x   3x   3x 3x                   1x           503x 226x       277x 7x         270x 270x               233x 52x       233x     1x     134x   134x             1x       991x       991x   991x               1x     14x           1x       34x                       1x       5x           1x       1x             1x 19x           1x     98x   98x             1x     19x   19x             1x     31x           1x           1x 100x 100x     1x 17x     1x       1x 1x                    
import {
  Action,
  Event,
  EventObject,
  SendAction,
  SendActionOptions,
  CancelAction,
  ActionObject,
  ActionType,
  Assigner,
  PropertyAssigner,
  AssignAction,
  ActionFunction,
  ActionFunctionMap,
  ActivityActionObject,
  ActionTypes,
  ActivityDefinition,
  SpecialTargets,
  Invocation
} from './types';
import * as actionTypes from './actionTypes';
import { getEventType } from './utils';
 
export { actionTypes };
 
export function toEventObject<TEvents extends EventObject>(
  event: Event<TEvents>
  // id?: TEvents['type']
): TEvents {
  if (typeof event === 'string' || typeof event === 'number') {
    const eventObject = { type: event };
    // if (id !== undefined) {
    //   eventObject.id = id;
    // }
 
    return eventObject as TEvents;
  }
 
  return event as TEvents;
}
 
function getActionFunction<TContext>(
  actionType: ActionType,
  actionFunctionMap?: ActionFunctionMap<TContext>
): ActionFunction<TContext> | undefined {
  if (!actionFunctionMap) {
    return undefined;
  }
  const actionReference = actionFunctionMap[actionType];
 
  Eif (!actionReference) {
    return undefined;
  }
 
  if (typeof actionReference === 'function') {
    return actionReference;
  }
 
  return actionReference.exec;
}
 
export const toActionObject = <TContext>(
  action: Action<TContext>,
  actionFunctionMap?: ActionFunctionMap<TContext>
): ActionObject<TContext> => {
  let actionObject: ActionObject<TContext>;
 
  if (typeof action === 'string' || typeof action === 'number') {
    actionObject = {
      type: action,
      exec: getActionFunction(action, actionFunctionMap)
    };
  } else if (typeof action === 'function') {
    actionObject = {
      type: action.name,
      exec: action
    };
  } else {
    const exec = getActionFunction(action.type, actionFunctionMap);
    return exec
      ? {
          ...action,
          exec
        }
      : action;
  }
 
  Object.defineProperty(actionObject, 'toString', {
    value: () => actionObject.type,
    enumerable: false
  });
 
  return actionObject;
};
 
export function toActivityDefinition<TContext>(
  action: string | ActivityDefinition<TContext>
): ActivityDefinition<TContext> {
  const actionObject = toActionObject(action);
 
  return {
    id: typeof action === 'string' ? action : actionObject.id,
    ...actionObject,
    type: actionObject.type
  };
}
 
export const toActionObjects = <TContext>(
  action: Array<Action<TContext> | Action<TContext>> | undefined,
  actionFunctionMap?: ActionFunctionMap<TContext>
): Array<ActionObject<TContext>> => {
  Iif (!action) {
    return [];
  }
 
  const actions = Array.isArray(action) ? action : [action];
 
  return actions.map(subAction => toActionObject(subAction, actionFunctionMap));
};
 
interface RaiseEvent<TContext, TEvents extends EventObject>
  extends ActionObject<TContext> {
  event: TEvents['type'];
}
 
export function raise<TContext, TEvents extends EventObject>(
  eventType: TEvents['type']
): RaiseEvent<TContext, TEvents> {
  return {
    type: actionTypes.raise,
    event: eventType
  };
}
 
export function send<TContext, TEvents extends EventObject>(
  event: Event<TEvents>,
  options?: SendActionOptions
): SendAction<TContext, TEvents> {
  return {
    target: options ? options.target : undefined,
    type: actionTypes.send,
    event: toEventObject<TEvents>(event),
    delay: options ? options.delay : undefined,
    id:
      options && options.id !== undefined
        ? options.id
        : (getEventType<TEvents>(event) as string)
  };
}
 
export function sendParent<TContext, TEvents extends EventObject>(
  event: Event<TEvents>,
  options?: SendActionOptions
): SendAction<TContext, TEvents> {
  return send<TContext, TEvents>(event, {
    ...options,
    target: SpecialTargets.Parent
  });
}
 
export function log<TContext, TEvents extends EventObject>(
  expr: (ctx: TContext, event: TEvents) => void,
  label?: string
) {
  return {
    type: actionTypes.log,
    label,
    expr
  };
}
 
export const cancel = (sendId: string | number): CancelAction => {
  return {
    type: actionTypes.cancel,
    sendId
  };
};
 
export function start<TContext>(
  activity: string | ActivityDefinition<TContext>
): ActivityActionObject<TContext> {
  const activityDef = toActivityDefinition(activity);
 
  return {
    type: ActionTypes.Start,
    activity: activityDef,
    exec: undefined
  };
}
 
export function stop<TContext>(
  activity: string | ActivityDefinition<TContext>
): ActivityActionObject<TContext> {
  const activityDef = toActivityDefinition(activity);
 
  return {
    type: ActionTypes.Stop,
    activity: activityDef,
    exec: undefined
  };
}
 
export const assign = <TContext, TEvents extends EventObject = EventObject>(
  assignment: Assigner<TContext, TEvents> | PropertyAssigner<TContext, TEvents>
): AssignAction<TContext, TEvents> => {
  return {
    type: actionTypes.assign,
    assignment
  };
};
 
export function isActionObject<TContext>(
  action: Action<TContext>
): action is ActionObject<TContext> {
  return typeof action === 'object' && 'type' in action;
}
 
export function after(delay: number, id?: string) {
  const idSuffix = id ? `#${id}` : '';
  return `${ActionTypes.After}(${delay})${idSuffix}`;
}
 
export function done(id: string) {
  return `${ActionTypes.DoneState}.${id}`;
}
 
export function invoke<TContext>(
  invokeConfig: string | Invocation<TContext>,
  options?: Partial<Invocation<TContext>>
): ActivityDefinition<TContext> {
  Eif (typeof invokeConfig === 'string') {
    return {
      id: invokeConfig,
      src: invokeConfig,
      type: ActionTypes.Invoke,
      ...options
    };
  }
 
  return invokeConfig;
}