Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a61563c
commit 7907c8e
Showing
9 changed files
with
717 additions
and
597 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/** | ||
* @typedef {Object} StateDefinitions | ||
* @property {{[event: string]: { target: string; actions?: Array<string> }}} [on] | ||
*/ | ||
|
||
/** | ||
* @typedef {Object} Options | ||
* @property {{[state: string]: StateDefinitions}} states | ||
* @property {object} context; | ||
* @property {string} initial | ||
*/ | ||
|
||
/** | ||
* @typedef {Object} Implementation | ||
* @property {{[actionName: string]: (ctx: object, event: any) => object}} actions | ||
*/ | ||
|
||
/** | ||
* 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: [] } explicitly. | ||
* - event passed to `send` must be an object with `type` property. | ||
* - actions implementation will be [assign action](https://xstate.js.org/docs/guides/context.html#assign-action) if you return any value. | ||
* Do not return anything if you just want to invoke side effect. | ||
* | ||
* The goal of this custom function is to avoid installing the entire `'xstate/fsm'` package, while enabling modeling using | ||
* state machine. You can copy the first parameter into the editor at https://stately.ai/viz to visualize the state machine. | ||
* | ||
* @param {Options} options | ||
* @param {Implementation} implementation | ||
*/ | ||
function createMachine({ states, context, initial }, { actions }) { | ||
let currentState = initial; | ||
let currentContext = context; | ||
|
||
return { | ||
send: (event) => { | ||
const transitionConfig = states[currentState].on?.[event.type]; | ||
|
||
if (transitionConfig) { | ||
currentState = transitionConfig.target; | ||
transitionConfig.actions?.forEach((actName) => { | ||
const nextContextValue = actions[actName]?.(currentContext, event); | ||
if (nextContextValue) { | ||
currentContext = { | ||
...currentContext, | ||
...nextContextValue, | ||
}; | ||
} | ||
}); | ||
} | ||
}, | ||
}; | ||
} | ||
|
||
export default createMachine; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.