| OLD | NEW |
| (Empty) |
| 1 | |
| 2 (function(scope) { | |
| 3 var MoreRouting = scope.MoreRouting = scope.MoreRouting || {}; | |
| 4 MoreRouting.Emitter = Object.create(null); // Minimal set of properties. | |
| 5 | |
| 6 /** | |
| 7 * A dumb prototype that provides very simple event subscription. | |
| 8 * | |
| 9 * You are responsible for initializing `__listeners` as an array on objects | |
| 10 * that make use of this. | |
| 11 */ | |
| 12 Object.defineProperties(MoreRouting.Emitter, { | |
| 13 | |
| 14 /** | |
| 15 * Registers a callback that will be called each time any parameter managed by | |
| 16 * this object (or its parents) have changed. | |
| 17 * | |
| 18 * @param {!Function} callback | |
| 19 * @return {{close: function()}} | |
| 20 */ | |
| 21 __subscribe: { | |
| 22 value: function __subscribe(callback) { | |
| 23 this.__listeners.push(callback); | |
| 24 | |
| 25 return { | |
| 26 close: this.__unsubscribe.bind(this, callback), | |
| 27 }; | |
| 28 }, | |
| 29 }, | |
| 30 | |
| 31 /** | |
| 32 * Unsubscribes a previously registered callback. | |
| 33 * | |
| 34 * @param {!Function} callback | |
| 35 */ | |
| 36 __unsubscribe: { | |
| 37 value: function __unsubscribe(callback) { | |
| 38 var index = this.__listeners.indexOf(callback); | |
| 39 if (index < 0) { | |
| 40 console.warn(this, 'attempted unsubscribe of unregistered listener:', ca
llback); | |
| 41 return; | |
| 42 } | |
| 43 this.__listeners.splice(index, 1); | |
| 44 }, | |
| 45 }, | |
| 46 | |
| 47 /** | |
| 48 * Notifies subscribed callbacks. | |
| 49 */ | |
| 50 __notify: { | |
| 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); | |
| 58 } | |
| 59 | |
| 60 this.__listeners.forEach(function(listener) { | |
| 61 listener.apply(null, args); | |
| 62 }.bind(this)); | |
| 63 }, | |
| 64 }, | |
| 65 | |
| 66 }); | |
| 67 | |
| 68 })(window); | |
| OLD | NEW |