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

simple-middleware-manager

v1.0.0

Published

> Lightweight and simple yet powerful middleware manager

Downloads

2

Readme

Simple middleware manager

Lightweight and simple yet powerful middleware manager

CircleCI

Tool to manage middlewares and/or eventListeners. It works similar to express middlewares with a little more flexibility.

FAQ

Why this exists?

There are not many tools that helps manage middlewares and callbacks and none that have auto mode (see below)

I found a bug! What should I do?

Feel free to make a pull request. All contributions are appreciated!

Usage

Installing library

npm i --save simple-middleware-manager

Registering callbacks

You can register callback (or eventListeners) using .on method and remove them with .off method

import createMiddlewareManager from 'simple-middleware-manager';

const manager = createMiddlewareManager();

// adding callbacks/eventListeners
const listener1 = data => console.log('listener1', data);
const listener2 = data => console.log('listener2', data);

manager.on('myEvent', listener1);
manager.on('myEvent', listener2);

manager.trigger('myEvent', 'myData'); // listener1 myData; listener2 myData

// removing callbacks/eventListeners
manager.off('myEvent', listener2);

manager.trigger('myEvent', 'myData'); // listener1 myData

Registering callbacks with additional conditions

You can register eventListeners and add additional conditions that can prevent them from being called

import createMiddlewareManager from 'simple-middleware-manager';

const manager = createMiddlewareManager();

// adding callbacks/eventListeners
const listener1 = data => console.log('listener1', data);
const listener2 = data => console.log('listener2', data);

const condition = (event, data, listener) => {
  if (data === 'foo') {
    listener(data);
  }
};

manager.on('myEvent', listener1);
manager.on('myEvent', condition, listener2);

manager.trigger('myEvent', 'myData'); // listener1 myData; listener2 myData

manager.trigger('myEvent', 'myData'); // listener1 myData

NOTE: adding additional function may seem counter intuitive as the same logic can be just added to the listener. In the case of altering the listener, however, it will make it impossible to remove it later. See the example below:

Imagine you use middleware-manager internally in another library, that allows you to add conditions:

yourLibrary.on('eventName', { foo: 'bar' }, listener).

In that case you can wrap condition in a function and just pass listener unchanged which will allow you to remove it later easily still using simple-middleware-manager mechanism:

yourLibrary.off('eventName', listener);

Registering middlewares

  • You can add middlewares using .use method, remove them using .unuse or .remove methods.
  • Middlewares will be triggered in the order of registering
  • Each middleware get data as first argument and next() function as second
  • Calling next() function will trigger next middleware
  • You can break middleware pipeline by not calling next
import createMiddlewareManager from 'simple-middleware-manager';

const manager = createMiddlewareManager();

// adding middlewares
const middleware1 = (data, next) => {
  console.log('I will be called', data);
  next();
};
const middleware2 = (data, next) => {
  console.log('I will be called as well', data);
}
const middleware3 = (data, next) => {
  console.log('Will I be called?', data);
  next();
}

manager.use('myEvent', middleware1);
manager.use('myEvent', middleware2);
manager.use('myEvent', middleware3);

manager.trigger('myEvent', 'myData'); // I will be called myData; I will be called as well myData

// removing middlewares
manager.unuse('myEvent', middleware2);

manager.trigger('myEvent', 'myData'); // I will be called myData; Will I be called? myData

Using middleware manager in auto mode

  • In auto mode, manager will detect if you passed next as second argument to the middleware
  • If you did, manager will wait for it to be called
  • If you didn't, next() function will be called automatically
import createMiddlewareManager from 'simple-middleware-manager';

const manager = createMiddlewareManager({ autoMode: true }); // creating manager with autoMode on

// adding middlewares
const middleware1 = (data) => { // we don't pass next() function so it will be called automatically
  console.log('I will be called', data);
};
const middleware2 = (data, next) => {
  console.log('I will be called as well', data);
}
const middleware3 = (data, next) => {
  console.log('I will not be called :(', data);
  next();
}

manager.use('myEvent', middleware1);
manager.use('myEvent', middleware2);
manager.use('myEvent', middleware3);

manager.trigger('myEvent', 'myData'); // I will be called myData; I will be called as well myData

Roadmap

  • Adding conditions function for middlewares