ApplicationImpl cleanup, part 1:
[chromium-blink-merge.git] / third_party / polymer / v1_0 / components-chromium / more-routing / emitter-extracted.js
blob48461026da06b49bf7ddad91d10418f1ef3c5fe5
2 (function(scope) {
3 var MoreRouting = scope.MoreRouting = scope.MoreRouting || {};
4 MoreRouting.Emitter = Object.create(null); // Minimal set of properties.
6 /**
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, {
14 /**
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()}}
21 __subscribe: {
22 value: function __subscribe(callback) {
23 this.__listeners.push(callback);
25 return {
26 close: this.__unsubscribe.bind(this, callback),
31 /**
32 * Unsubscribes a previously registered callback.
34 * @param {!Function} callback
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:', callback);
41 return;
43 this.__listeners.splice(index, 1);
47 /**
48 * Notifies subscribed callbacks.
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);
60 this.__listeners.forEach(function(listener) {
61 listener.apply(null, args);
62 }.bind(this));
66 });
68 })(window);