All files / src/api CompletionState.ts

100% Statements 17/17
100% Branches 11/11
100% Functions 5/5
100% Lines 17/17

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    13x         13x                     13x               13x 457x   117x     22x 22x             339x     3x 3x 3x           1x                 13x 119x                         13x 24x  
import { Rule } from "@jonloucks/concurrency-ts/api/Rule";
import { Config } from "@jonloucks/concurrency-ts/api/StateMachine";
import { used } from "../auxiliary/Checks";
 
/**
 * The possible states of a Completion
 */
export const STATES: string[] = ['PENDING', 'FAILED', 'CANCELLED', 'SUCCEEDED'] as const;
 
/**
 * The possible states of a Completion
 */
export type State = typeof STATES[number];
 
/** The CompletionState type */
export { State as CompletionState };
 
/** The starting state for a Completion */
export const START_STATE: State = 'PENDING';
 
/**
 * Get the state transition rules for a given Completion state
 *
 * @param state the state to get rules for
 * @return the array of rules for the given state
 */
export function getStateRules(state: State): Array<Rule<State>> {
  switch (state) {
    case 'PENDING':
      return [
        {
          canTransition: (event: string, goal: State): boolean => {
            used(event);
            return goal === 'FAILED' || goal === 'CANCELLED' || goal === 'SUCCEEDED';
          }
        }
      ];
    case 'FAILED':
    case 'CANCELLED':
    case 'SUCCEEDED':
      return [
        {
          canTransition: (event: string, goal: State): boolean => {
            used(event);
            used(goal);
            return false;
          },
          isTerminal: true
        }
      ];
    default:
      return [];
  }
};
 
/**
 * Get the default StateMachine configuration for a Completion
 *
 * @return the default StateMachine configuration
 */
export function getStateMachineConfig(): Config<State> {
  return {
    initialValue: START_STATE,
    states: STATES,
    getStateRules: getStateRules
  };
}
 
/**
 * Determine if a state is a terminal state
 *
 * @param state the state to check
 * @return true if terminal state
 */
export function isTerminalState(state: State): boolean {
  return state === 'FAILED' || state === 'CANCELLED' || state === 'SUCCEEDED';
}