3 var MoreRouting = scope.MoreRouting = scope.MoreRouting || {};
4 MoreRouting.Emitter = Object.create(null); // Minimal set of properties.
7 * A dumb prototype that provides very simple event subscription.
9 * You are responsible for initializing `__listeners` as an array on objects
10 * that make use of this.
12 Object.defineProperties(MoreRouting.Emitter, {
15 * Registers a callback that will be called each time any parameter managed by
16 * this object (or its parents) have changed.
18 * @param {!Function} callback
19 * @return {{close: function()}}
22 value: function __subscribe(callback) {
23 this.__listeners.push(callback);
26 close: this.__unsubscribe.bind(this, callback),
32 * Unsubscribes a previously registered callback.
34 * @param {!Function} callback
37 value: function __unsubscribe(callback) {
38 var index = this.__listeners.indexOf(callback);
40 console.warn(this, 'attempted unsubscribe of unregistered listener:', callback);
43 this.__listeners.splice(index, 1);
48 * Notifies subscribed callbacks.
51 value: function __notify(key, value) {
52 if (this.__silent) return;
53 var args = Array.prototype.slice.call(arguments);
54 // Notify listeners on parents first.
55 var parent = Object.getPrototypeOf(this);
56 if (parent && parent.__notify && parent.__listeners) {
57 parent.__notify.apply(parent, args);
60 this.__listeners.forEach(function(listener) {
61 listener.apply(null, args);