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

@roziscoding/composer

v1.0.5

Published

Standalone implementation of the "Chain of Command" pattern

Downloads

3

Readme

Composer

Standalone implementation of the "Chain of Command" pattern. Heavily inspired by grammY's Composer class

Usage

Getting started

You start by creating an instance of the Composer class, passing as a type param the type of your context. You then register your middleware functions and call the execute function, passing the value of the context. The resulting promise will contain the context as returned by the middleware tree.

Important: you must always call next. If you don't, your middleware tree will stay stuck and will not finish execution. Also, the call to next should always be awaited or returned, so you don't run into concurrency problems.

import { Composer } from "https://deno.land/x/composer/mod.ts";

type Context = {
  steps: number;
};

const composer = new Composer();
composer.use((ctx, next) => next({ ...ctx, steps: ctx.steps + 1 }));

composer.execute({ steps: 0 }).then(console.log); // { steps: 1 }

Timeshift Methods

Composer offers two methods, which we call Timeshift Methods, that allow you to register middleware to be run either after or before the regularly registered middleware. This allows you to ensure execution order and make your code clearer.

Deferring execution

Sometimes you need a middleware function to perform tasks after all the other middleware has finished running. To do that, you have two options: you can either await next(ctx) and perform your tasks after that; or you can use the after method, like so:

import { Composer } from 'https://deno.land/x/composer/mod.ts'

type Context = {
  start: number
  steps: number
}

const composer = new Composer<Context>()
composer.after((ctx, next) => { console.log(`took ${Date.now() - start} ms`}); return next(ctx) })
composer.use((ctx, next) => next({ ...ctx, start: Date.now() }))
composer.use((ctx, next) => next({ ...ctx, steps: ctx.steps + 1 }))

composer.execute({ steps: 0 }).then(console.log)
// took xxx ms
// { steps: 1 }

The after method registers the middleware function in a special way that makes sure that it always runs after all the other middleware. As with regular middleware, middleware registered using after can modify the context by passing the mutated context, or a mutated clone of it, to the next function.

Forwarding execution

Opposite to the after method, there is the before method for when you need to run things before any registered middleware:

import { Composer } from 'https://deno.land/x/composer/mod.ts'

type Context = {
  start: number
  steps: number
}

const composer = new Composer<Context>()
composer.use((ctx, next) => next({ ...ctx, start: Date.now() }))
composer.use((ctx, next) => next({ ...ctx, steps: ctx.steps + 1 }))
composer.use((ctx, next) => { console.log(`took ${Date.now() - start} ms`}); return next(ctx) })
composer.before((ctx, next) => next({ ...ctx, start: Date.now() }))

composer.execute({ steps: 0 }).then(console.log)
// took xxx ms
// { steps: 1 }

The before method works similarly to the after method in that it registers the middleware function in a special way that makes sure it will always run before any other middleware registered regularly. As with any other middleware, you can also modify the context by passing its new version to next, be it a new object or the mutated existing one.

Order matters

As you've seen with the examples, before middleware will always run at the beginning of the execution, and after middleware will always run at the end of the execution, no matter if you call them before or after use. However, when you call timeshift functions multiple times, they will be executed in the order they were registered. For example:

type Context = {
  steps: number[];
};

const composer = new Composer<Steps>();

composer.before((ctx, next) => next({ ...ctx, steps: ctx.steps.concat([1]) }));
composer.before((ctx, next) => next({ ...ctx, steps: ctx.steps.concat([2]) }));
composer.after((ctx, next) => next({ ...ctx, steps: ctx.steps.concat([3]) }));
composer.after((ctx, next) => next({ ...ctx, steps: ctx.steps.concat([4]) }));

composer.execute({ steps: [] }).then(console.log); // [1, 2, 3, 4]