https://github.com/fritzy/priority-chain
Chain events together to modify the emitted value in order of priority.
https://github.com/fritzy/priority-chain
Last synced: 7 months ago
JSON representation
Chain events together to modify the emitted value in order of priority.
- Host: GitHub
- URL: https://github.com/fritzy/priority-chain
- Owner: fritzy
- License: mit
- Created: 2015-12-08T07:16:47.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2015-12-08T07:17:00.000Z (almost 10 years ago)
- Last Synced: 2025-01-25T21:55:26.366Z (9 months ago)
- Language: JavaScript
- Size: 1.95 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Priority Chain
With Priority-Chain, you can emit events in order of priorty, allowing each subscriber to mutate the value.

## Example
```js
'use strict';
const PriorityChain = require('priority-chain');destiny = {};
const chain = new PriorityChain();
function timesTwo (event) {
return event * 2;
}function blocker (event) {
return;
}chain.subscribe('set', 10, timesTwo);
chain.subscribe('set', 0, (event) => {
return event + 8;
});chain.setLast('set', function (event) {
this.out = event;
return event;
}, destiny);chain.setFirst('set', (event) => {
return 0;
});console.log(chain.emit('set', 123)); // 16
chain.subscribe('set', 5, blocker);
console.log(chain.emit('set', 34)); // 8
chain.unsubscribe('set', blocker);
console.log(chain.emit('set', 123)); // 16
chain.unsubscribe('set', timesTwo);
console.log(chain.emit('set', 123)); // 8
chain.setLast('set', function (event) {
this.output = event;
return event;
}, destiny);console.log(chain.emit('set', 123)); // 8
console.log(destiny.output); // 8
```## Usage
__subscribe__(name, priority, function, ctx)
__unsubscribe__(name, function)
__setFirst__(name, function, ctx)
__setLast__(name, function, ctx)
__emit__(name, event)
If you return nothing or `undefined`, the event blocks the rest of the chain.