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