2 Copyright (c) 2015 The Polymer Project Authors. All rights reserved.
3 This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
4 The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
5 The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
6 Code distributed by Google as part of the polymer project is also
7 subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
11 var MoreRouting
= scope
.MoreRouting
= scope
.MoreRouting
|| {};
12 MoreRouting
.Emitter
= Object
.create(null); // Minimal set of properties.
15 * A dumb prototype that provides very simple event subscription.
17 * You are responsible for initializing `__listeners` as an array on objects
18 * that make use of this.
20 Object
.defineProperties(MoreRouting
.Emitter
, {
23 * Registers a callback that will be called each time any parameter managed by
24 * this object (or its parents) have changed.
26 * @param {!Function} callback
27 * @return {{close: function()}}
30 value
: function __subscribe(callback
) {
31 this.__listeners
.push(callback
);
34 close
: this.__unsubscribe
.bind(this, callback
),
40 * Unsubscribes a previously registered callback.
42 * @param {!Function} callback
45 value
: function __unsubscribe(callback
) {
46 var index
= this.__listeners
.indexOf(callback
);
48 console
.warn(this, 'attempted unsubscribe of unregistered listener:', callback
);
51 this.__listeners
.splice(index
, 1);
56 * Notifies subscribed callbacks.
59 value
: function __notify(key
, value
) {
60 if (this.__silent
) return;
61 var args
= Array
.prototype.slice
.call(arguments
);
62 // Notify listeners on parents first.
63 var parent
= Object
.getPrototypeOf(this);
64 if (parent
&& parent
.__notify
&& parent
.__listeners
) {
65 parent
.__notify
.apply(parent
, args
);
68 this.__listeners
.forEach(function(listener
) {
69 listener
.apply(null, args
);