lightweight signal with priority support.
npm install min-signalEvents/Promises/Callbacks. To know more about the differences, you can check out the Wiki page of js-signals.
add(), dispatch() and remove() with min-signal
js
var onStarted = new MinSignal();
function callback(param1, param2) {
console.log(param1 + ' ' + param2);
}
onStarted.add(callback); //add listener
onStarted.dispatch('foo', 'bar'); //dispatch signal passing custom parameters
onStarted.remove(callback); //remove a single listener
``
$3
If you want the callback to be dispatched only once, you can use addOnce().
``js
var onStarted = new MinSignal();
function callback() {
console.log('hello');
}
onStarted.addOnce(callback);
onStarted.dispatch(); // log : hello
onStarted.dispatch(); // do nothing
``
$3
The third argument of the add() and addOnce() is the priority, the higher it is, the earlier it will be dispatched. The default value is 0, and order by first comes first served principle.
``js
var onStarted = new MinSignal();
function callback1() {
console.log('1');
}
function callback2() {
console.log('2');
}
function callback3() {
console.log('3');
}
onStarted.add(callback1);
onStarted.add(callback2);
onStarted.add(callback3, null, 10);
onStarted.dispatch(); // log : 3, 1, 2
``
$3
Like function binding, you can provide the context for the callback binding. Same context with the same function will be ignored.
``js
var onStarted = new MinSignal();
function callback() {
console.log(this.id);
}
var a = {id: 'a'};
var b = {id: 'b'};
onStarted.add(callback, a);
onStarted.add(callback, a);
onStarted.add(callback, b);
onStarted.dispatch(); // log : a, b
``
$3
You can also add argument prefix with the add() and addOnce() like in Function.bind(). One thing need to keep in mind is that, for the duplicated callback checking, it only check the function and the context, same function with the same context with new argument prefix will be rejected instead of overriding.
``js
var onStarted = new MinSignal();
function callback(param1, param2, param3, param4) {
console.log(param1 + param2 + param3 + param4);
}
onStarted.add(callback, null, 0, 'a', 'b');
onStarted.dispatch('c', 'd'); // log : abcd
``
$3
You can stop propagation by returning false in the callback function. By stopping propagation, in the dispatch() call it will return the listener which triggered the stop propagation.
``js
var onStarted = new MinSignal();
onStarted.add(function(){
return false; // returns "false" to stop propagation
});
onStarted.add(function(){
console.log('wont be triggered'); // this function won't be trigger
});
onStarted.dispatch(); // log : {...} // will return the listener that stopped the propagation
``
Installation
---
$3
Download min-signal here.
$3
Check out the npm page here.
Testing
---
run npm run test` to run the test suite.