All files / src/api Rule.ts

100% Statements 3/3
100% Branches 0/0
100% Functions 1/1
100% Lines 3/3

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 331x                                                           1x 6x  
import { RequiredType, guardFunctions } from "@jonloucks/concurrency-ts/api/Types";
 
/**
* Opt-in interface a state type can implement to assist in determining the valid transitions
*/
export interface Rule<T> {
 
  /**
   * Determine if 'this' state can transition to the target
   *
   * @param event the event name
   * @param goal  the goal state
   * @return true if the transition is valid
   */
  canTransition(event: string, goal: T): boolean;
 
  /**
   * Determine if the given state is a terminal state
   *
   * @return true if the state is terminal
   */
  isTerminal?: boolean;
}
 
/**
 * Type guard to determine if an instance implements Rule
 *
 * @param instance the instance to check
 * @return true if instance is a Rule
 */
export function guard<T>(instance: unknown): instance is RequiredType<Rule<T>> {
  return guardFunctions(instance, 'canTransition');
}