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

@pcan/generator-chain

v1.0.0

Published

Generators-based Chain of Responsibility

Downloads

77

Readme

@pcan/generator-chain

An implementation of the Chain of responsibility pattern that supports both synchronous and asynchronous processing, through the same API.

Example

A basic usage with synchronous handlers:

import { chain, ChainInvocation } from '@pcan/generator-chain';

type GreetingInvocation = ChainInvocation<string, { name: string }>;

function* greetingHandler({ proceed }: GreetingInvocation) {
    const hours = new Date().getHours();
    const hello = hours > 6 && hours < 11 ? 'Good morning, ' : 'hello, ';
    const nextValue = yield* proceed();
    return hello + nextValue;
}

function* nameHandler({ context, proceed }: GreetingInvocation) {
    const nextValue = yield* proceed();
    return context.name + nextValue;
}

function* emojiHandler() {
    return ' ' + (Math.random() > .5 ? '😄' : '😉');
}

const greetingChain = chain('my-greeting-chain')
    .append('greetingHandler', greetingHandler)
    .append('nameHandler', nameHandler)
    .append('emojiHandler', emojiHandler)
    .build()

const greeting = greetingChain.invoke({ name: 'pcan' });
console.log(greeting);

Output:

Good morning, pcan 😄

Motivation

Generally speaking, the classical Chain of Responsibility pattern implementation is constrained by the stack size, which has a fixed limit. Creating a chain with many handlers (or handlers taking many parameters) may result in Maximum call stack size exceeded errors. Adding extension points (interceptors) can exacerbate the problem, thus impairing customizability.

This library overcomes such limitations by moving the stack management to the heap, using an engine that coordinates the execution of handlers (implemented as generator functions).

How it works

Chain handlers are generator functions that utilize the yield instruction to pass execution control to the next handler (or delegate it to another chain).
In order to do so, the values a handler yields can be instances of Proceed() or Delegate(). These command objects instruct the execution engine to schedule the next handlers' execution, passing the provided context (or the current one, if none specified).
When a handler returns, the control is passed back to the previous handler in the chain, restarting its execution immediately after the last yield.
The execution engine implements a call stack in the heap to keep track of the current generator instances and their relative order. It also detects whether the return value types are Promise instances or not, to behave synchronously or asynchronously based on the handlers' return types. To maintain the same API, the chain execution returns a PromiseOrValue<T> instance, which represents either normal Promise<T> or the immediate execution result T if the execution is fully synchronous. In case there's at least one asynchronous handler (implemented as async generator function), the chain will always return a Promise<T>.

Features & APIs

Coming soon!

License

MIT