Skip to content

Comparison between mini signals v1.0 and js signals v1.0

Jayson Harshbarger edited this page Sep 27, 2015 · 3 revisions

Below is a discussion of the API differences between mini-signals and js-signals.

add only accepts a listener

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.

Example: Set execution context of the listener handler

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();

remove deprecated, use detach

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.

Example

var binding = signal.add(handler);
signal.dispatch();
signal.detach(binding);

removeAll deprecated, use detachAll

The detachAll method is used to detach all listeners.

Example

var binding = signal.add(handler);
signal.dispatch();
signal.detachAll();

listeners deprecated, use handlers

Example

signal.add(handler);
signal.add(handler2);
var handlers = signal.handlers();  // returns an array of SignalBinding instances

dispatch is not auto-rebound, use the Function.prototype.bind() method.

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).

Example

window.addEventListener("load", signal.dispatch);               // incorrect
window.addEventListener("load", signal.dispatch.bind(signal));  // correct

SignalBinding.params deprecated, use the Function.prototype.bind() method.

Use Function.prototype.bind() method to add default paramaters to the listener on add.

Add default parameters to Signal dispatch

function handler(a) {
  console.log(a);
}

signal.add(handler.bind(null, 'param'));  // this will print "param"

signal.dispatch();

Other missing features

Currently memorize, listener priority and Stop/Halt Propagation are not implemented. Open a github issue if you feel strong about any of these