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 🙏

© 2025 – Pkg Stats / Ryan Hefner

svelte-state-machine

v0.2.0

Published

Finite State Machine (FSM) for Svelte 5+ (Runes) with Strategy Pattern for Routing. Highly reactive, strongly typed, and includes an efficient state matching utility.

Readme

⚛️ Svelte Rune-Powered Finite State Machine (FSM)

This is a strongly typed and highly reactive implementation of a Finite State Machine designed for Svelte 5+ using Runes ($state).

It leverages the Strategy Pattern via the StatesRouter interface to decouple transition logic from the core FSM class, improving maintainability and testability.

📦 Installation

This project is published on npm, so you can use it with this command:

npm i svelte-state-machine

✨ Key Features

  • Svelte Reactivity: Uses $state principles to ensure state changes automatically update Svelte components.
  • Strong Typing: States are defined as string literals, providing auto-completion and compile-time safety.
  • Strategy Pattern (StatesRouter): Allows defining complex, reusable transition logic (e.g., linear, cyclic, error handling jumps) outside the FSM core.
  • Pattern Matching: The match<T>() utility simplifies rendering logic by mapping state indices to return values (a TypeScript equivalent of a switch-case).

🚀 Usage Example

<script lang="ts">
  // 1. Imports: Bring in the FSM class and the default linear router strategy.
  import { FiniteStateMachine, LinearIncreaseStateRouter } from "svelte-state-machine";

  // 2. Reactive State: Declare a reactive Svelte state using the $state rune.
  // This variable will hold the data fetched from the API.
  let title: string = $state("");

  // 3. FSM Initialization: Create a new FSM instance.
  // The states define the lifecycle of the data fetching process.
  const FSM = new FiniteStateMachine(
    "FetchingData",
    "ProcessingData",
    "DisplayResults",
    "FatalError"
  );

  // Initial check: The FSM starts at the first declared state (index 0).
  console.log(
    FSM.check.FetchingData() // The same as `FSM.state === FSM.enum.FetchingData`
  ); // Output: true

  // 4. State-Driven Async Logic: Start the API request.
  fetch("https://jsonplaceholder.typicode.com/todos/1")
    .then(response => {
      // State Transition 1: Data received, now processing.
      // Use the router strategy to move to the next sequential state (index + 1).
      FSM.next(LinearIncreaseStateRouter);
      return response.json();
    })
    .then(json => {
      // Update the reactive variable 'title' with the fetched data.
      title = json.title;
      
      // State Transition 2: Processing complete, ready to display results.
      FSM.next(LinearIncreaseStateRouter); // FSM.state is now DisplayResults
    })
    .catch(error => {
      // Error Handling: If any promise fails, transition immediately to the error state.
      console.error(error);
      
      // Direct state assignment (bypassing the router) to jump to the error state.
      FSM.state = FSM.enum.FatalError;
    });
</script>

<h1>{FSM.match(
  // Match 1: If FetchingData, display loading message.
  [FSM.enum.FetchingData, () => "Fetching..."],
  
  // Match 2: If ProcessingData, display processing message.
  [FSM.enum.ProcessingData, () => "Processing..."],
  
  // Match 3: If DisplayResults, render the reactive 'title' variable.
  [FSM.enum.DisplayResults, () => title],
  
  // Match 4: If FatalError, display the error message.
  [FSM.enum.FatalError, () => "Something went wrong..."],
  
  // Note: FSM.match automatically handles the current FSM.state, 
  // ensuring the UI always reflects the correct stage of the async operation.
)}</h1>