0.0.6 • Published 6 months ago

lite-fsm v0.0.6

Weekly downloads
-
License
MIT
Repository
github
Last release
6 months ago

lite-fsm

lite-fsm is a lightweight finite state machine library

Features

  • Simple and clear API
  • Provides application architecture out of the box (global store, effects, service layer)
  • Support redux middleware
  • Small size and no dependencies

Install

npm install lite-fsm

Examples

Quickstart (basic example)

import { createMachine, MachineManager } from "lite-fsm";

const playback = createMachine({
  config: {
    IDLE: {
      DO_INIT: "PAUSED",
    },
    PAUSED: {
      DO_PLAY: "PLAYING",
    },
    PLAYING: {
      DO_PAUSE: "PAUSED",
      TIME_UPDATE: null,
    },
    END: {},
  },
  initialState: "IDLE",
  initialContext: {
    currentTime: null,
    duration: null,
    remainingTime: null,
  },
  effects: {
    PLAYING: ({ services }) => services.playerService.play(),
    PAUSED: ({ services }) => services.playerService.pause(),
  },
});

const manager = MachineManager({ playback });
manager.setDependencies({
  services: {
    playerService: {
      play: () => Promise.resolve(),
      pause: () => Promise.resolve(),
    },
  },
});

manager.onTransition((prevState, nextState) => {
  console.log("[onTransition]", { prevState, nextState });
});

manager.transition({ type: "DO_INIT" });
manager.transition({ type: "DO_PLAY" });
manager.transition({
  type: "TIME_UPDATE",
  payload: { currentTime: 0, duration: 60, remainingTime: 60 },
});

const { state, context } = manager.getState().playback;
0.0.6

6 months ago

0.0.5

7 months ago

0.0.4

11 months ago

0.0.3

11 months ago

0.0.2

11 months ago

0.0.1

11 months ago