Socket
Socket
Sign inDemoInstall

extra-fsm

Package Overview
Dependencies
7
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    extra-fsm

The library supports both OOP and FP.


Version published
Weekly downloads
897
increased by237.22%
Maintainers
1
Install size
5.10 MB
Created
Weekly downloads
 

Changelog

Source

0.2.1 (2023-06-10)

Bug Fixes

  • export src (12e9a63)

Readme

Source

extra-fsm

The library supports both OOP and FP.

Install

npm install --save extra-fsm
# or
yarn add extra-fsm

Usage

import { FiniteStateMachine, transition } from 'extra-fsm'

const schema = {
  stopped: { start: 'running' }
, running: { stop: 'stopped' }
}

// OOP
const fsm = new FiniteStateMachine(schema, 'stopped')
fsm.send('start')
console.log(fsm.state) // running

// FP
const newState = transition(schema, 'stopped', 'start')
console.log(newState)// running

API

type IFiniteStateMachineSchema<
  State extends string | number | symbol
, Event extends string | number | symbol
> = Record<State, Partial<Record<Event, State>>>

FiniteStateMachine

class FiniteStateMachine<
  State extends string | number | symbol
, Event extends string | number | symbol
> {
  get [Symbol.toStringTag](): string
  get state(): State

  constructor(
    schema: IFiniteStateMachineSchema<State, Event>
  , initialState: State
  )

  matches(state: State): boolean
  can(event: Event): boolean

  /**
   * @throws {BadEventError}
   */
  send(event: Event): void
}

ObservableFiniteStateMachine

interface IFiniteStateMachineStateChange<
  State extends string | number | symbol
, Event extends string | number | symbol
> {
  event: Event
  oldState: State
  newState: State
}

class ObservableFiniteStateMachine<
  State extends string | number | symbol
, Event extends string | number | symbol
> extends FiniteStateMachine<State, Event> {
  get [Symbol.toStringTag](): string

  observeStateChanges(): Observable<IFiniteStateMachineStateChange<State, Event>>
}

transition

export function transition<
  State extends string | number | symbol
, Event extends string | number | symbol
, TransitionEvent extends Event
>(
  schema: IFiniteStateMachineSchema<State, Event>
, state: State
, event: TransitionEvent
): State

canTransition

function canTransition<
  State extends string | number | symbol
, Event extends string | number | symbol
>(
  schema: IFiniteStateMachineSchema<State, Event>
, state: State
, event: Event
) => boolean

FAQs

Last updated on 10 Jun 2023

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc