npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@deviltea/tiny-state-machine

v0.0.1

Published

A simple state machine with tiny size and type safe.

Downloads

76

Readme

@deviltea/tiny-state-machine

npm version npm downloads bundle License

A simple state machine with a tiny size and type safety.

Overview

@deviltea/tiny-state-machine is a lightweight, type-safe state machine library designed for simplicity and ease of use. It aims to help developers easily manage application states and transitions without adding unnecessary complexity or bloat. This package is ideal for projects that require efficient state management with a minimal footprint.

Core Concepts

To fully utilize @deviltea/tiny-state-machine, it's essential to understand the core concepts that form the foundation of the state machine.

  • State: A state represents a particular status of the system at a given point in time. In this library, each state is defined with possible transitions and actions.
  • Event: Events are external or internal triggers that cause the state machine to transition from one state to another. Events are the primary mechanism by which state changes occur.
  • Transition: A transition is the movement from one state to another, triggered by an event. Each state defines which events it can handle and the target state for those events.
  • Initial State: The state in which the machine starts when it is created. This state is defined using the initial property.
  • Final State: A terminal state that indicates the state machine has reached the end of its workflow. No transitions are possible from a final state.
  • Context: The context represents extended state information that persists across transitions. It can store data that may influence how transitions occur.
  • Transition Handlers: Handlers can be attached to transitions to execute custom logic, such as side effects or additional actions, during a state change.
  • State Machine Configuration: The configuration object defines the structure of the state machine, including its states, transitions, and optional context.

Features

  • Tiny and Lightweight: Minimal footprint, optimized for projects where reducing bundle size is important.
  • Type Safety: Utilizes TypeScript to provide a safe and type-checked state management system.
  • Flexible State Transitions: Define custom states, events, and transitions to suit your application's needs.
  • Built-in Event Handling: Attach transition handlers for custom actions during state changes.
  • Support for Final States: Easily manage terminal states.

Installation

To install the package, use either npm, yarn, or pnpm:

npm install @deviltea/tiny-state-machine
yarn add @deviltea/tiny-state-machine
pnpm add @deviltea/tiny-state-machine

Usage

Here is an example of how to create a simple state machine using @deviltea/tiny-state-machine.

Example: Basic State Machine

import { createMachine } from '@deviltea/tiny-state-machine'

// Define a simple state machine with three states: idle, loading, and finished
const machine = createMachine({
	initial: 'idle',
	states: {
		idle: {
			on: {
				start: 'loading',
			},
		},
		loading: {
			on: {
				done: 'finished',
			},
		},
		finished: {
			type: 'final',
		},
	},
})

console.log(machine.currentState) // "idle"

// Transition to the next state
machine.send('start')
console.log(machine.currentState) // "loading"

// Finish the transition
machine.send('done')
console.log(machine.currentState) // "finished"

Example: Adding Transition Handlers

You can also add handlers that will be called during state transitions.

import { createMachine } from '@deviltea/tiny-state-machine'

const machine = createMachine({
	initial: 'idle',
	states: {
		idle: {
			on: {
				start: 'loading',
			},
		},
		loading: {
			on: {
				done: 'finished',
			},
		},
		finished: {
			type: 'final',
		},
	},
})

const unsubscribe = machine.onTransition(({ transition }) => {
	console.log(
		`Transitioned from ${transition.source} to ${transition.target} via event '${transition.event}'`
	)
})

machine.send('start') // Logs: Transitioned from idle to loading via event 'start'
machine.send('done') // Logs: Transitioned from loading to finished via event 'done'

unsubscribe()

License

MIT License © 2023-PRESENT DevilTea