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

react-pipeline-runner

v0.2.0

Published

Lightweight React hook for running async actions sequentially with abort support and resume capability

Downloads

179

Readme

react-pipeline-runner

A lightweight React hook for running async actions sequentially with abort support, error handling, and resume capability.

Installation

npm install react-pipeline-runner

Features

  • Sequential execution of sync/async actions
  • AbortController support for cancellation
  • Resume from failed step
  • TypeScript-first with intelligent ID inference
  • Discriminated union for type-safe state handling
  • Automatic cleanup on unmount
  • Zero dependencies (except React 18+)

Basic Usage

import { useMemo } from 'react'
import { usePipeline } from 'react-pipeline-runner'

function MyComponent() {
  const steps = useMemo(() => [
    async (signal) => {
      await fetch('/api/step1', { signal })
    },
    async (signal) => {
      await fetch('/api/step2', { signal })
    },
    async () => {
      console.log('Step 3 - no signal needed')
    },
  ], [])

  const pipeline = usePipeline(steps)

  return (
    <div>
      <p>State: {pipeline.state}</p>
      
      {pipeline.state === 'idle' && (
        <button onClick={pipeline.start}>Start</button>
      )}
      
      {pipeline.state === 'running' && (
        <>
          <p>Running step {pipeline.current.index + 1}...</p>
          <button onClick={pipeline.stop}>Cancel</button>
        </>
      )}
      
      {pipeline.state === 'failed' && (
        <>
          <p>Error at step {pipeline.current.index + 1}: {String(pipeline.current.error)}</p>
          <button onClick={pipeline.resume}>Retry</button>
          <button onClick={pipeline.stop}>Abandon</button>
        </>
      )}
      
      {pipeline.state === 'completed' && (
        <p>Done!</p>
      )}
    </div>
  )
}

Steps with IDs

You can assign IDs to steps for better tracking. TypeScript will automatically infer the ID types.

const steps = useMemo(() => [
  { id: 'fetch-user', action: async () => fetchUser() },
  { id: 'validate', action: () => validateData() },
  async () => doSomethingWithoutId(),
  { id: 'save', action: async () => saveData() },
], [])

const pipeline = usePipeline(steps)

if (pipeline.state === 'running') {
  console.log('Current step:', pipeline.current.id)
  // TypeScript knows: id is 'fetch-user' | 'validate' | 'save' | undefined
}

Autostart

Start the pipeline automatically when the component mounts:

const steps = useMemo(() => [step1, step2, step3], [])

const pipeline = usePipeline(steps, { autostart: true })

API

usePipeline(steps, options?)

Parameters

  • steps - Array of actions. Each action can be:
    • A function: (signal?: AbortSignal) => Promise<unknown> | unknown
    • An object: { id: string, action: (signal?: AbortSignal) => Promise<unknown> | unknown }
  • options - Optional configuration:
    • autostart?: boolean - Start pipeline on mount (default: false)

Returns (Discriminated Union)

The hook returns a discriminated union based on state:

| State | current | Description | |-------|-----------|-------------| | 'idle' | undefined | Not started or stopped | | 'running' | CurrentStatus | Executing steps | | 'failed' | CurrentStatus | Stopped on error | | 'completed' | undefined | All steps done |

Methods:

  • start() - Start from beginning. Returns true if started, false if running or failed.
  • stop() - Cancel and reset to idle. Returns true if was running or failed, false otherwise.
  • resume() - Retry failed step. Returns true if was failed, false otherwise.

State transitions:

| State | start() | stop() | resume() | |-------|-----------|----------|------------| | idle | ✅ starts | ❌ false | ❌ false | | running | ❌ false | ✅ → idle | ❌ false | | failed | ❌ false | ✅ → idle | ✅ retries | | completed | ✅ restarts | ❌ false | ❌ false |

CurrentStatus:

{
  index: number              // Step index (0-based)
  id: string | undefined     // Step ID if provided
  state: 'running' | 'failed'
  error: unknown | undefined // Error if failed
}

Type Safety

Thanks to discriminated unions, TypeScript narrows the current type based on state:

if (pipeline.state === 'failed') {
  // TypeScript knows current is defined!
  console.log(pipeline.current.error) // ✅ No need for && pipeline.current
}

if (pipeline.state === 'idle') {
  pipeline.current.index // ❌ Compile error - current is undefined
}

AbortController Support

Each action receives an optional AbortSignal. Use it to make your actions cancellable:

async (signal) => {
  // Fetch automatically aborts when signal fires
  const response = await fetch('/api/data', { signal })
  return response.json()
}

When stop() is called or the component unmounts, the signal is aborted automatically.

Best Practice: Memoize Steps

Always wrap your steps array in useMemo to ensure stable references:

// ❌ Bad - new array on every render
const pipeline = usePipeline([
  () => fetchData(),
  () => processData(),
])

// ✅ Good - stable reference
const steps = useMemo(() => [
  () => fetchData(),
  () => processData(),
], [])

const pipeline = usePipeline(steps)

If steps depend on props or state, include them in dependencies:

const steps = useMemo(() => [
  () => fetchUser(userId),
  () => sendNotification(),
], [userId])

const pipeline = usePipeline(steps)

Why? Without useMemo, the start and resume methods get new references on every render, which can cause unnecessary re-renders in child components or issues with effect dependencies.

License

ISC