-
Notifications
You must be signed in to change notification settings - Fork 12
Comparison between mini signals v1.0 and js signals v1.0
Below is a discussion of the API differences between mini-signals and js-signals.
MiniSignals.prototype.add
method registers the specified listener on the MiniSignals it's called on and returns a MiniSignalBinding
instance (An object representing the binding between the signal and listener).
Unlike js-signals, MiniSignals.prototype.add
accepts only the signal listener. listenerContext
and priority
are deprecated. To specify a listenerContext
use the Function.prototype.bind() method.
var ctx = { name: 'ctx' };
function handler() {
console.log(this.name);
}
signal.add(handler); // incorrect: this will print undefined
signal.add(handler.bind(ctx)); // correct: this will print "ctx"
signal.dispatch();
In mini-signals a listener cannot be removed by listener function. Instead, the preferred method is to remove the listener by the signal binding object.
var binding = signal.add(handler);
signal.dispatch();
signal.detach(binding);
The detachAll
method is used to detach all listeners.
var binding = signal.add(handler);
signal.dispatch();
signal.detachAll();
signal.add(handler);
signal.add(handler2);
var handlers = signal.handlers(); // returns an array of SignalBinding instances
Dispatching a signal directly from an EventListener won't work because in JavaScript events bind the callback to the emitting object (see The value of this within the handler).
window.addEventListener("load", signal.dispatch); // incorrect
window.addEventListener("load", signal.dispatch.bind(signal)); // correct
Use Function.prototype.bind() method to add default paramaters to the listener on add.
function handler(a) {
console.log(a);
}
signal.add(handler.bind(null, 'param')); // this will print "param"
signal.dispatch();
Currently memorize, listener priority and Stop/Halt Propagation are not implemented. Open a github issue if you feel strong about any of these