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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@ydbjs/fsm

v6.1.1

Published

Lightweight async-first FSM runtime for YDB JavaScript SDK packages.

Readme

@ydbjs/fsm

@ydbjs/fsm is a lightweight async-first finite state machine runtime for YDB JavaScript SDK packages.

It is designed for long-lived async workflows where predictable state transitions, explicit side effects, and safe shutdown semantics are required.

Features

  • Single-writer event processing (race-resistant transition flow)
  • Explicit transition contract with typed effects
  • Runtime outputs as AsyncIterable (for await ... of runtime)
  • Async source ingestion via runtime.ingest(...)
  • Native AbortSignal integration
  • AsyncDisposable support for runtime and ingest handles
  • Minimal runtime surface (dispatch, ingest, close, destroy)

Installation

npm install @ydbjs/fsm

Requires Node.js >= 20.19.0.

Quick Start

import { createMachineRuntime } from '@ydbjs/fsm'

type State = 'idle' | 'running' | 'done'
type Event =
  | { type: 'counter.start' }
  | { type: 'counter.increment'; step?: number }
  | { type: 'counter.finish' }

type Effect = { type: 'counter.log'; message: string }
type Output = { type: 'counter.state'; value: State } | { type: 'counter.value'; value: number }

type Context = { count: number }

let runtime = createMachineRuntime<State, Context, Event, Effect, Output>({
  initialState: 'idle',
  context: { count: 0 },
  transition(context, state, event, runtime) {
    switch (event.type) {
      case 'counter.start':
        runtime.emit({ type: 'counter.state', value: 'running' })
        return { state: 'running', effects: [{ type: 'counter.log', message: 'started' }] }

      case 'counter.increment':
        if (state !== 'running') return
        context.count += event.step ?? 1
        runtime.emit({ type: 'counter.value', value: context.count })
        return { effects: [{ type: 'counter.log', message: `count=${context.count}` }] }

      case 'counter.finish':
        runtime.emit({ type: 'counter.state', value: 'done' })
        return { state: 'done', effects: [{ type: 'counter.log', message: 'finished' }] }
    }
  },
  effects: {
    'counter.log'(effect) {
      console.log('[effect]', effect.message)
    },
  },
})

void (async () => {
  for await (let out of runtime) {
    console.log(out)
  }
})()

runtime.dispatch({ type: 'counter.start' })
runtime.dispatch({ type: 'counter.increment' })
runtime.dispatch({ type: 'counter.finish' })

await runtime.close('done')
await runtime.destroy('finalized')

Core Concepts

Transition

transition(context, state, event, runtime) is called for every event in queue order.

It can:

  • return next state
  • return effects to execute
  • emit output events via runtime.emit(...)

Effects

effects is a typed map keyed by effect.type. Each effect handler receives (effect, context, state, runtime).

This map-based approach keeps handlers exhaustive and explicit.

Runtime Output

Runtime itself is AsyncIterable<Output>:

for await (let out of runtime) {
  // ...
}

Lifecycle: close vs destroy

@ydbjs/fsm intentionally provides two shutdown modes.

| Method | Semantics | Typical Use | | ------------------ | ------------------------------------------------------------------------------------------------- | ----------------------------- | | close(reason?) | Graceful shutdown: stop accepting new work, drain queued events/effects, then close output stream | Normal completion | | destroy(reason?) | Hard shutdown: abort signal, drop queued events immediately, close output stream | Fatal error / forced teardown |

Recommended pattern

Use graceful-first teardown for async disposal:

await runtime.close('graceful shutdown')
await runtime.destroy('finalized')

[Symbol.asyncDispose] follows this same intent.

Ingesting Async Sources

Use runtime.ingest(source, map, signal?) to route external async streams into machine events.

await using ingest = runtime.ingest(stream, (input) => {
  if (input.type === 'message') {
    return { type: 'reader.message', payload: input.payload }
  }
  return null
})

Notes:

  • map can filter by returning null
  • ingest is rejected after runtime is closing/closed/destroyed
  • ingest handle is AsyncDisposable

API Summary

  • createMachineRuntime(options)
  • runtime.dispatch(event)
  • runtime.ingest(source, map, signal?)
  • runtime.close(reason?)
  • runtime.destroy(reason?)
  • runtime[Symbol.asyncIterator]()
  • runtime[Symbol.asyncDispose]()

Design Notes

This package is runtime infrastructure, not a domain framework. Domain-specific states/events/effects must stay in each package (coordination, topic, query, etc).

For full design rationale, see DESIGN.md.

Examples

Development

npm run build
npm run test

License

Apache-2.0