Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@alwatr/fsm

Package Overview
Dependencies
Maintainers
0
Versions
26
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@alwatr/fsm

A robust TypeScript library for implementing Flux (Finite) State Machines, enabling clear and organized management of application state and transitions.

  • 3.1.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
113
decreased by-23.13%
Maintainers
0
Weekly downloads
 
Created
Source

Flux: Finite State Machine

A robust TypeScript library for implementing Flux (Finite) State Machines, enabling clear and organized management of application state and transitions.

Features

  • Clear State Management: Model your application's behavior with well-defined states and transitions.
  • TypeScript Support: Written in TypeScript with full type definitions for improved code quality and developer experience.
  • Flexible Actions: Attach custom actions to state transitions and events.
  • Observable Integration: Built-in integration with @alwatr/observable for reactive state updates.
  • Built-in Logging: Integrated logging for debugging and monitoring state machine behavior.

Installation

npm install @alwatr/fsm

Usage

import {FluxStateMachineBase} from '@alwatr/fsm';

// Define your states and events
type MyState = 'idle' | 'loading' | 'success' | 'error';
type MyEvent = 'fetch' | 'success' | 'error';

class MyStateMachine extends FluxStateMachineBase<MyState, MyEvent> {
  constructor() {
    super({
      name: 'my-state-machine',
      initialState: 'idle',
    });

    // Define state transitions
    this.stateRecord_ = {
      idle: {
        fetch: 'loading',
      },
      loading: {
        success: 'success',
        error: 'error',
      },
      success: {}, // Terminal state
      error: {},    // Terminal state
    };

    // Define actions (optional)
    this.actionRecord_ = {
      'on_fetch': this.handleFetch,
      'on_success': this.handleSuccess,
      'on_error': this.handleError,
    };
  }

  // ... (Implement your action methods: handleFetch, handleSuccess, handleError)

  // Trigger a state transition
  fetchData() {
    this.transition_('fetch'); 
  }
}

API

FluxStateMachineBase<S extends string, E extends string>

  • constructor(config: {name: string; loggerPrefix?: string; initialState: S}):

    • config.name: The name of the state machine (used for logging).
    • config.loggerPrefix: Optional prefix for log messages.
    • config.initialState: The initial state of the machine.
  • stateRecord_: StateRecord<S, E>: Defines the states and their possible transitions based on events.

  • actionRecord_: ActionRecord<S, E>: Binds action names to class methods for execution during transitions.

  • transition_(event: E): Triggers a state transition based on the provided event.

  • shouldTransition_(_eventDetail: StateEventDetail<S, E>): MaybePromise<boolean>: (Optional) Allows you to define custom conditions for transitions.

  • postTransition__(eventDetail: StateEventDetail<S, E>): Executes actions associated with state transitions and events.

  • execAction__(name: ActionName<S, E>, eventDetail: StateEventDetail<S, E>): Executes a specific action if defined in the actionRecord_.

  • resetToInitialState_(): Resets the machine to its initial state without notifying subscribers.

Sponsors

The following companies, organizations, and individuals support flux ongoing maintenance and development. Become a Sponsor to get your logo on our README and website.

Exir Studio

Contributing

Contributions are welcome! Please read our contribution guidelines before submitting a pull request.

License

This project is licensed under the AGPL-3.0 License.

Keywords

FAQs

Package last updated on 29 Sep 2024

Did you know?

Socket

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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc