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 | 1x 1x 1x 1x 2052x | import { Rule } from "@jonloucks/concurrency-ts/api/Rule";
import { Transition } from "@jonloucks/concurrency-ts/api/Transition";
import { OptionalType, RequiredType, guardFunctions } from "@jonloucks/concurrency-ts/api/Types";
import { WaitableNotify, guard as guardWaitableNotify } from "@jonloucks/concurrency-ts/api/WaitableNotify";
import { WaitableSupplier, guard as guardWaitableSupplier } from "@jonloucks/concurrency-ts/api/WaitableSupplier";
import { Open } from "@jonloucks/contracts-ts/api/Open";
import { IsCompleted } from "@jonloucks/concurrency-ts/api/IsCompleted";
import { Contracts } from "@jonloucks/contracts-ts/api/Contracts";
/**
* State machine.
* User defined states with rules to restrict state transitions.
*
* @param <T> the user defined state type
*/
export interface StateMachine<T> extends Open, WaitableSupplier<T>, WaitableNotify<T>, IsCompleted {
/**
* Set the current state, state must already exist and be an allowed transition
*
* @param event the event name
* @param state the new state
* @return true if state was changed
* @throws IllegalArgumentException when event is null, state is null, or unknown
*/
setState(event: string, state: T): boolean;
/**
* Get the current state
*
* @return the current state, never null
*/
getState(): T;
/**
* Determine if the given state is known
*
* @param state the state to check
* @return true if and only if the state is known
* @throws IllegalArgumentException when state is null
*/
hasState(state: T): boolean;
/**
* Determine if a transition is allowed from the current state to a new one.
*
* @param event the event that is triggering the transition
* @param state the candidate state to transition to
* @return if transition event is allowed to change the current state to the given state
* @throws IllegalArgumentException when event is null, state is null, or unknown
*/
isTransitionAllowed(event: string, state: T): boolean;
/**
* Execute a transition from the current state to another
*
* @param transition the transition to execute
* @param <R> the return type of the transition. For example, a Closeable during open.
* @return the transition return value
* @throws IllegalArgumentException when transition is null or required fields are not present.
*/
transition<R>(transition: Transition<T, R>): OptionalType<R>;
}
/**
* StateMachine configuration
*
* @param <T> the type of each state
*/
export interface Config<T> {
/**
* The contracts to use
*/
contracts?: Contracts;
/**
* Return the initial value. It is required, the use of required is because
* the builder may not have provided a value
*
* @return the optional initial state
*/
initialValue: RequiredType<T>;
/**
* @return the list of states in the state machine
*/
states: readonly T[];
/**
* Get all the rules for a specified state
*
* @param state the state
* @return the rules of the state
*/
getStateRules?(state: T): ReadonlyArray<Rule<T>>;
}
export { Config as StateMachineConfig } ;
/**
* Determine if the given instance is a StateMachine
*
* @param instance the instance to check
* @return true if the instance is a StateMachine
*/
export function guard<T>(instance: unknown): instance is RequiredType<StateMachine<T>> {
return guardFunctions(instance,
'isTransitionAllowed',
'isCompleted',
'getState',
'setState',
'hasState',
'transition',
'open'
) && guardWaitableNotify<T>(instance) && guardWaitableSupplier<T>(instance);
} |