Simple middleware manager
Lightweight and simple yet powerful middleware manager
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
; const manager = ; // adding callbacks/eventListenersconst listener1 = console;const listener2 = console; manager;manager; manager; // listener1 myData; listener2 myData // removing callbacks/eventListenersmanager; manager; // listener1 myData
Registering callbacks with additional conditions
You can register eventListeners and add additional conditions that can prevent them from being called
; const manager = ; // adding callbacks/eventListenersconst listener1 = console;const listener2 = console; const condition = { if data === 'foo' ; }; manager;manager; manager; // listener1 myData; listener2 myData manager; // 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
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
; const manager = ; // adding middlewaresconst middleware1 = { console; ;};const middleware2 = { console;}const middleware3 = { console; ;} manager;manager;manager; manager; // I will be called myData; I will be called as well myData // removing middlewaresmanager; manager; // 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
; const manager = ; // creating manager with autoMode on // adding middlewaresconst middleware1 = { // we don't pass next() function so it will be called automatically console;};const middleware2 = { console;}const middleware3 = { console; ;} manager;manager;manager; manager; // I will be called myData; I will be called as well myData
Roadmap
- Adding conditions function for middlewares