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

@signver/handle-chain

v3.1.0

Published

Simply utility to create a handler chain.

Downloads

1

Readme

Overview

Simple utility package to create a chain-of-responsibilty-like pattern for any given context.

Note: Breaking change as of v3.0.0. See usage guide below for new usage guide.

Usage guide

The general idea is to define a chain of handlers that will be executed in FIFO order.

/* get-data-delegate.ts */
import createDelegation, { 
  Delegate,
  DelegateContext, 
  DelegateResult 
} from '@signver/handle-chain'

type Request = {
    // any request specific data
    // object is frozen internally before passing to delegates
    id: string
}

type State = {
    // any data that might be expected to be passed between delegates
    // object is expected to be mutable
    raw: any
}

type ResponseData = {
  // the ['data'] property of the response object
  // the final output once the last delegate is complete
}

export type CustomDelegateContext = DelegateContext<Request, ResponseData, State>

// initial state factory
export function initialState(): State {
  // ...
}

// ... define rest of delegates
export async function fetchDataDelegate(context: CustomDelegateContext): DelegateResult {
  const data = await Promise.all([
    getRawData1FromID(context.request.id),
    getRawData2FromID(context.request.id),
  ]) 
  if (!data[0] || !data[1]) {
      // stop the delegation prematurely
    return {
      shouldStop: true
    }
  }
  context.state.raw = data
  return {
    shouldStop: false
  }
}
export async function refineDataDelegate(context: CustomDelegateContext): DelegateResult {
  const data = await refineData(context.state.raw)
  // set the final output at context.response.data
  context.setResponse(data)
  return {
    shouldStop: false
  }
}

export default createDelegation({
  // FIFO 
  delegates: [
    fetchDataDelegate,
    refineDataDelegate
  ],
  stateFactory: initialState
})
import delegate from 'get-data-delegate.ts'

const response = await delegate({
  id: 'some_id'
})

// if any of the delegate function throws an error
// response.encounteredError will be `true`
if (response.encounteredError) {
  doSomethingWith(response.error)
}

// if any of the function could not complete
// if any of the delegate function throws an error
// response.terminatedEarly will also be `true`
if (response.terminatedEarly) {
  // do something
}

doSomethingWith(response.data)