* A simplified `createMachine` from `@xstate/fsm` with the following differences: * - the returned machine is technically a "service". No `interpret(machine).start()` is needed. * - the state definition only support `on` and target must be declared with { target: 'nextState', actions: [] } explici
({ states, context, initial }, { actions })
| 117 | * @returns {StateMachine} state machine |
| 118 | */ |
| 119 | function createMachine({ states, context, initial }, { actions }) { |
| 120 | let currentState = initial; |
| 121 | let currentContext = context; |
| 122 | |
| 123 | return { |
| 124 | send: (event) => { |
| 125 | const currentStateOn = states[currentState].on; |
| 126 | const transitionConfig = currentStateOn && currentStateOn[event.type]; |
| 127 | |
| 128 | if (transitionConfig) { |
| 129 | currentState = transitionConfig.target; |
| 130 | if (transitionConfig.actions) { |
| 131 | transitionConfig.actions.forEach((actName) => { |
| 132 | const actionImpl = actions[actName]; |
| 133 | |
| 134 | const nextContextValue = |
| 135 | actionImpl && actionImpl(currentContext, event); |
| 136 | |
| 137 | if (nextContextValue) { |
| 138 | currentContext = { |
| 139 | ...currentContext, |
| 140 | ...nextContextValue, |
| 141 | }; |
| 142 | } |
| 143 | }); |
| 144 | } |
| 145 | } |
| 146 | }, |
| 147 | }; |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * @typedef {object} ShowOverlayData |
no outgoing calls
no test coverage detected
searching dependent graphs…