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

recursive-state-machine

v0.1.5

Published

A simple non blocking recursive state machine for node.js

Downloads

18

Readme

recursive-state-machine

This library implements a simple non blocking recursive state machine for node.js. Multiple tasks can be defined for a specific state and multiple states can be defined as well. Tasks are asynchronous functions and are isolated from each other, meaning that the result does not transit from task to task. It offers a lot of flexibility in the control flow and infinite loops are also valid.

Example of a configuration:

const config = {
    initialState: 'S1',
    terminateState: 'S3',
    states: {
        S1: {
            tasks: [
                {
                    name: 'task1',
                    task: task1,
                },
                {
                    name: 'task2',
                    task: task2,
                }, 
            ],
            nextState: 'S2',
        },
        S2: {
            tasks: [
                {
                    name: 'task3',
                    task: task3,
                },
                {
                    name: 'task4',
                    task: task4,
                },
            ],
            nextState: 'S3',
        },
    },
    errorHandler: ({ currentState, taskName, error }) => { ... },
    cleanupTasks: [
        {
            name: 'foo1',
            task: foo1,
        },
        {
            name: 'foo2',
            task: foo2,
        },
    ],
};

Multiple states can be defined and it is possible to jump between them. States cannot be defined with the same name as the terminateState.

All errors thrown from the tasks will be handled by the errorHandler. The machine will resume in the state returned by the errorHandler. If nothing is returned, it terminates with result 1.

The machine returns 0 when terminating in success and returns 1 when terminating in error (e.g if nothing is returned from the errorHandler). It can also throw an error if something is wrong inside the errorHandler.

Cleanup tasks are optional. They will always run on machine termination (regardless if it terminates in success or error state); E.g disconnecting from a database.

The error handler is of the form: ({ currentState, taskName, error }) => { ... } We can return states to change the behaviour of the machine example:

({ currentState, taskName, error }) => { 
    if (error ...) {
        return 'S1';
    }
    ...

    // Terminate the machine
    return 'S3';
}

Installation

npm install --save recursive-state-machine

Usage:

const { StateMachine } = require('recursive-state-machine');

or

import { StateMachine } from 'recursive-state-machine';
const stateMachine = StateMachine(config);
stateMachine.start();

with more control:

(async () => {
    const stateMachine = StateMachine(config);
    
    try {
        const result = await stateMachine.start();
        ...
    } catch (err) {
        ...
    }
})();