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

start-stop-state-machine

v2.0.0

Published

[![Node.js CI](https://github.com/digidem/start-stop-state-machine/workflows/Node.js%20CI/badge.svg)](https://github.com/digidem/start-stop-state-machine/actions/workflows/node.js.yml) [![Coverage Status](https://coveralls.io/repos/github/digidem/start-st

Readme

start-stop-state-machine

Node.js CI Coverage Status Npm package version standard-readme compliant

A simple state machine for managing a service that asynchronously starts and stops.

Table of Contents

Install

npm install start-stop-state-machine

Usage

import StateMachine from 'start-stop-state-machine'

async function startService() {
  console.log('starting')
  await new Promise((res) => setTimeout(res, 200))
  console.log('started')
}

async function stopService() {
  console.log('stopping')
  await new Promise((res) => setTimeout(res, 200))
  console.log('stopped')
}

const sm = new StateMachine({ start: startService, stop: stopService })

;(async () => {
  sm.start()
  sm.start()
  await sm.started()
  // logs "starting" then "started", but only once
  await sm.stop()
  // logs "stopping", then "stopped"
})()

API

new StateMachine(opts)

Create a state machine instance.

  • opts.start — async function called to start the service. Any arguments passed to sm.start() are forwarded to it. Its resolved value becomes the start result (see started()). Defaults to a no-op.
  • opts.stop — async function called to stop the service. Any arguments passed to sm.stop() are forwarded to it. Defaults to a no-op.

You can call start() and stop() multiple times. The service ends in the state of the last call, and opts.start() / opts.stop() are each called at most once per transition:

  • Calling start() when stopped calls opts.start() and resolves when it completes.
  • Calling start() when starting does not call opts.start() again, but resolves once the service has started.
  • Calling start() when started resolves immediately and does nothing.
  • Calling start() when stopping waits until the service is stopped, then starts it.

stop() follows the inverse logic.

If opts.start() or opts.stop() throw, the service is left in an unrecoverable error state. Calling any method while in the error state rejects with that error.

sm.start(...args)

Start the service, forwarding args to opts.start(). Returns a Promise that resolves with the value returned by opts.start() once the service is started.

sm.stop(...args)

Stop the service, forwarding args to opts.stop(). Returns a Promise that resolves once the service is stopped.

sm.started()

Returns a Promise that resolves with the start result once the service is in the started state, and rejects if it enters the error state. Useful for gating other methods on the service being ready:

await sm.started()

Note: if the service is stopping or stopped, this queues until the next time the service starts. Check sm.state.value first if that is not desired.

sm.stopped()

Returns a Promise that resolves once the service is in the stopped state, and rejects if it enters the error state. The counterpart to started().

Note: if the service is starting or started, this queues until the next time the service stops.

sm.state

Getter returning the current state, an object of the form:

{ value: 'stopped' | 'starting' | 'started' | 'stopping' }
// or, in the error state:
{ value: 'error', error: Error }

'state' event

The state machine extends tiny-typed-emitter. It emits a state event with the new state object on every transition:

sm.on('state', (state) => {
  console.log(state.value)
})

Maintainers

@digidem

Contributing

PRs accepted.

Small note: If editing the README, please conform to the standard-readme specification.

License

MIT © 2022 Digital Democracy