0.0.2 • Published 2 years ago
@bemedev/fstate v0.0.2
fstate
Features
'@bemedev/fstate' | |
---|---|
Finite states | ✅ |
Initial state | ✅ |
Transitions (object) | ❌ |
Transitions (string target) | ✅ |
Delayed transitions | ❌ |
Eventless transitions | ✅ |
Nested states | ❌ |
Parallel states | ❌ |
History states | ❌ |
Final states | ❌ |
Context | ✅ |
Entry actions | ❌ |
Exit actions | ❌ |
Transition actions | ✅ |
Parameterized actions | ❌ |
Transition guards | ✅ |
Parameterized guards | ❌ |
Spawned actors | ❌ |
Invoked actors(promises only) | ✅ |
Quick start
Installation
npm i @bemedev/fstate //or
yarn add @bemedev/fstate //or
pnpm add @bemedev/fstate
Usage (machine)
import { createMachine, serve } from '@bemedev/fstate';
const machine = createMachine(
{
tsTypes: {
args: {} as number,
context: {} as { val: string },
},
context: { val: '' },
initial: 'idle',
states: {
idle: {
type: 'sync',
transitions: [
{
target: 'prom',
},
],
},
prom: {
type: 'async',
promise: 'prom',
onDone: [
{
target: 'finish',
actions: ['ok'],
},
],
onError: [],
timeout: '0',
},
finish: { type: 'final' },
},
},
{
promises: {
prom: async () => true,
},
actions: {
ok: ctx => {
ctx.val = 'true';
},
},
},
);
// => 'inactive'
Usage (serve)
import { createMachine, interpret } from '@bemedev/fstate';
const toggleMachine = createMachine({...});
//Serve infer the return type (the context is the return type of the function)
//Also it infers the fact that serve will be an async function or not
//Here before the states contain an async one,
//"service" will be an async function.
const service = serve(machine); // (args: number)=>Promise<{ val: string }>
(()=>await service(2))() // expected = { val: 'true' }