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

user-flow-engine

v1.0.0

Published

An Engine for Customizable Scenarios

Readme

User Flow Engine

User Flow Engine is a lightweight runtime engine for executing user flows as ordered steps with deterministic transitions.

It is framework-agnostic and works in both browser and Node.js environments.

Features

  • Deterministic step execution by index
  • Goto-style transitions with next('stepShort')
  • Works with sync and async handlers
  • Extensible flow lifecycle hooks: onCreate, onBeforeFinish, onError
  • Zero framework coupling

Quick Start

import { BaseFlow, createEngine } from 'user-flow-engine';

class OnboardingFlow extends BaseFlow {
  short = 'onboarding';
  abstract = false;

  onCreate() {
    this.addStep({
      short: 'welcome',
      async handle(_from, _to, next) {
        console.log('Welcome step');
        return next('profile');
      },
    });

    this.addStep({
      short: 'profile',
      handle() {
        console.log('Profile step');
        // return undefined/null -> engine moves to next step by index
      },
    });
  }
}

const engine = createEngine({
  short: 'app',
  context: { isAuthorized: true },
  flows: [new OnboardingFlow()],
});

engine.handle('onboarding');

Installation

npm i user-flow-engine

Example

flows.js:

import { BaseFlow } from 'user-flow-engine';

export class SignedInFlow extends BaseFlow {
  abstract = true;
}

export class UsersWithCompaniesFlow extends SignedInFlow {
  abstract = true;
}

export class JustRegisteredFlow extends SignedInFlow {
  short = 'justRegistered';
  abstract = false;

  onCreate() {
    this.addStep({
      short: 'todoReplaceByTheRealStepShort',
      handle() {
        // TODO: Replace by the real step handler
      },
    });
  }
}

export class LandingToPaymentFlow extends UsersWithCompaniesFlow {
  short = 'landingToPayment';
  abstract = false;
  todoRemoveThisCounter = 0;

  onCreate() {
    this.addStep({
      short: 'todoReplaceByTheRealStepShort1',
      async handle() {
        console.log('TEST FLOW #' + this.todoRemoveThisCounter);
        this.todoRemoveThisCounter += 1;
      },
    });

    this.addStep({
      short: 'todoReplaceByTheRealStepShort2',
      handle(_from, _to, next) {
        console.log('------------');
        return next('todoReplaceByTheRealStepShort1');
      },
    });
  }
}

engine.js:

import { createEngine } from 'user-flow-engine';
import {
  JustRegisteredFlow,
  LandingToPaymentFlow,
} from './flows';

export const engine = createEngine({
  short: 'legacyFrontend',
  context: {
    isAuthorized: true,
    isJustSignedIn: true,
    isJustSignedUp: true,
    isActivePlan: false,
    isTrial: false,
  },
  flows: [
    new JustRegisteredFlow(),
    new LandingToPaymentFlow(),
  ],
});

export default engine;

API

Exports

  • createEngine(options)
  • FlowEngine (default export)
  • BaseFlow
  • FlowController
  • FlowStep
  • BaseError
  • types (TypeScript contracts)
  • FLOW_SUCCESS
  • DEFAULT_TICK

createEngine({/* options */})

| Option | Type | Required | Description | |-----------| --- | --- | --- | | short | string | yes | Engine identifier | | context | Record<string, any> \| null | no | Default context | | flows | FlowInterface[] \| null | no | Registered flows |

FlowEngine

Key methods:

  • setShort(short: string): this
  • setContext(context: Obj): this
  • setFlows(flows: FlowInterface[]): this
  • addFlow(flow: FlowInterface, _default?: boolean | null): this
  • getFlow(short: string): FlowInterface | null
  • handle(val?: FlowInterface | string, context?: Obj | null): FlowControllerInterface

handle(...) modes:

  1. engine.handle(flowObj) -> starts provided flow object.
  2. engine.handle('flowShort') -> starts registered flow by short.
  3. engine.handle() -> starts first flow in engine list.

BaseFlow

Fields:

  • short: string (default: 'baseFlow')
  • abstract: boolean (default: true)
  • steps: StepInterface[]

Hooks:

  • onCreate(): void | Promise<void>
  • onBeforeFinish(val?: any): void | Promise<void>
  • onError(error: Error): void

Key methods:

  • addStep(step, _default?)
  • setSteps(steps)
  • handle(context?)

Important:

  • abstract = true flows are not runnable and throw BaseError
  • onCreate() runs on each handle() call

FlowController

Fields:

  • tick
  • stepNum
  • steps
  • interval
  • isInProgress
  • doStop
  • stopVal

Methods:

  • handle(): Promise<any>
  • stop(val?: any): void
  • getStep(...), getStepIndex(...), setSteps(...), addStep(...)

FlowStep

handle(from, to, next)
  • from -> previous step or null
  • to -> next linear step or null
  • next('stepShort') -> returns target step by short

Execution Semantics

  • Execution is sequential by stepNum
  • If current step is missing, flow resolves with FLOW_SUCCESS
  • If step handler returns undefined or null, controller moves to next step by index
  • If handler returns next('stepShort'), controller jumps to that step
  • If step throws, flow fails and onError is called
  • controller.stop(value) stops execution and resolves with that value

Notes

  • The engine does not provide routing/UI/state/network/storage/analytics
  • No built-in automatic "best flow" selection (priority, scoring, tie-breakers, etc.)
  • onCreate() should register steps predictably to avoid accidental accumulation across re-runs

TypeScript Contracts

Main contracts are defined in src/types.ts:

  • StepInterface
  • FlowInterface
  • FlowControllerInterface
  • StepHandler, StepNextHandler
  • CreateEngineOptions, AddStepOptions

Development

Build library bundles and declarations:

npm run build

User Flow Engine by Kenny Romanov
TrustMe