Skip to content

Latest commit

 

History

161 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Matchina

TypeScript-first state machines with type-safe pattern matching.

npm install matchina

What it is

Matchina lets you model states as tagged unions and drive transitions with full type inference. Pattern matching is exhaustive — TypeScript won't let you forget a state.

import { matchina, defineStates } from "matchina";

const states = defineStates({
  Idle: undefined,
  Playing: (trackId: string) => ({ trackId }),
  Paused: (trackId: string) => ({ trackId }),
  Stopped: undefined,
});

const player = matchina(
  states,
  {
    Idle:    { start: "Playing" },
    Playing: { pause: "Paused", stop: "Stopped" },
    Paused:  { resume: "Playing", stop: "Stopped" },
    Stopped: { start: "Playing" },
  },
  "Idle"
);

player.start("song-123");

const message = player.getState().match({
  Playing: ({ trackId }) => `Now playing: ${trackId}`,
  Paused:  ({ trackId }) => `Paused: ${trackId}`,
  Idle:    () => "Ready",
  Stopped: () => "Stopped",
});

Docs

License

MIT

About

Lightweight state machines in TypeScript

Topics

Resources

Stars

4 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages