2 var MoreRouting = scope.MoreRouting = scope.MoreRouting || {};
3 MoreRouting.Emitter = Object.create(null); // Minimal set of properties.
6 * A dumb prototype that provides very simple event subscription.
8 * You are responsible for initializing `__listeners` as an array on objects
9 * that make use of this.
11 Object.defineProperties(MoreRouting.Emitter, {
14 * Registers a callback that will be called each time any parameter managed by
15 * this object (or its parents) have changed.
17 * @param {!Function} callback
18 * @return {{close: function()}}
21 value: function __subscribe(callback) {
22 this.__listeners.push(callback);
25 close: this.__unsubscribe.bind(this, callback),
31 * Unsubscribes a previously registered callback.
33 * @param {!Function} callback
36 value: function __unsubscribe(callback) {
37 var index = this.__listeners.indexOf(callback);
39 console.warn(this, 'attempted unsubscribe of unregistered listener:', callback);
42 this.__listeners.splice(index, 1);
47 * Notifies subscribed callbacks.
50 value: function __notify(key, value) {
51 if (this.__silent) return;
52 var args = Array.prototype.slice.call(arguments);
53 // Notify listeners on parents first.
54 var parent = Object.getPrototypeOf(this);
55 if (parent && parent.__notify && parent.__listeners) {
56 parent.__notify.apply(parent, args);
59 this.__listeners.forEach(function(listener) {
60 listener.apply(null, args);