1 | /**
|
2 | * @license
|
3 | * Copyright 2021 Google Inc.
|
4 | *
|
5 | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
6 | * of this software and associated documentation files (the "Software"), to deal
|
7 | * in the Software without restriction, including without limitation the rights
|
8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9 | * copies of the Software, and to permit persons to whom the Software is
|
10 | * furnished to do so, subject to the following conditions:
|
11 | *
|
12 | * The above copyright notice and this permission notice shall be included in
|
13 | * all copies or substantial portions of the Software.
|
14 | *
|
15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21 | * THE SOFTWARE.
|
22 | */
|
23 | import { __extends, __read, __spreadArray, __values } from "tslib";
|
24 | import { getDescriptor } from './observer';
|
25 | /**
|
26 | * Mixin to add `MDCObserver` functionality to an optional base class.
|
27 | *
|
28 | * @deprecated Prefer MDCObserverFoundation for stricter closure compliance.
|
29 | * @template C Optional base class constructor type.
|
30 | * @param baseClass - Optional base class.
|
31 | * @return A class that extends the optional base class with `MDCObserver`
|
32 | * functionality.
|
33 | */
|
34 | export function mdcObserver(baseClass) {
|
35 | var _a, _b;
|
36 | if (baseClass === void 0) { baseClass = /** @class */ (function () {
|
37 | function class_1() {
|
38 | }
|
39 | return class_1;
|
40 | }()); }
|
41 | var unobserves = Symbol();
|
42 | return _b = /** @class */ (function (_super) {
|
43 | __extends(MDCObserver, _super);
|
44 | function MDCObserver() {
|
45 | var _this = _super !== null && _super.apply(this, arguments) || this;
|
46 | _this[_a] = [];
|
47 | return _this;
|
48 | }
|
49 | MDCObserver.prototype.observe = function (target, observers) {
|
50 | var e_1, _b;
|
51 | var _this = this;
|
52 | var cleanup = [];
|
53 | try {
|
54 | for (var _c = __values(Object.keys(observers)), _d = _c.next(); !_d.done; _d = _c.next()) {
|
55 | var property = _d.value;
|
56 | var observer = observers[property].bind(this);
|
57 | cleanup.push(observeProperty(target, property, observer));
|
58 | }
|
59 | }
|
60 | catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
61 | finally {
|
62 | try {
|
63 | if (_d && !_d.done && (_b = _c.return)) _b.call(_c);
|
64 | }
|
65 | finally { if (e_1) throw e_1.error; }
|
66 | }
|
67 | var unobserve = function () {
|
68 | var e_2, _b;
|
69 | try {
|
70 | for (var cleanup_1 = __values(cleanup), cleanup_1_1 = cleanup_1.next(); !cleanup_1_1.done; cleanup_1_1 = cleanup_1.next()) {
|
71 | var cleanupFn = cleanup_1_1.value;
|
72 | cleanupFn();
|
73 | }
|
74 | }
|
75 | catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
76 | finally {
|
77 | try {
|
78 | if (cleanup_1_1 && !cleanup_1_1.done && (_b = cleanup_1.return)) _b.call(cleanup_1);
|
79 | }
|
80 | finally { if (e_2) throw e_2.error; }
|
81 | }
|
82 | var index = _this[unobserves].indexOf(unobserve);
|
83 | if (index > -1) {
|
84 | _this[unobserves].splice(index, 1);
|
85 | }
|
86 | };
|
87 | this[unobserves].push(unobserve);
|
88 | return unobserve;
|
89 | };
|
90 | MDCObserver.prototype.setObserversEnabled = function (target, enabled) {
|
91 | setObserversEnabled(target, enabled);
|
92 | };
|
93 | MDCObserver.prototype.unobserve = function () {
|
94 | var e_3, _b;
|
95 | try {
|
96 | // Iterate over a copy since unobserve() will remove themselves from the
|
97 | // array
|
98 | for (var _c = __values(__spreadArray([], __read(this[unobserves]))), _d = _c.next(); !_d.done; _d = _c.next()) {
|
99 | var unobserve = _d.value;
|
100 | unobserve();
|
101 | }
|
102 | }
|
103 | catch (e_3_1) { e_3 = { error: e_3_1 }; }
|
104 | finally {
|
105 | try {
|
106 | if (_d && !_d.done && (_b = _c.return)) _b.call(_c);
|
107 | }
|
108 | finally { if (e_3) throw e_3.error; }
|
109 | }
|
110 | };
|
111 | return MDCObserver;
|
112 | }(baseClass)),
|
113 | _a = unobserves,
|
114 | _b;
|
115 | }
|
116 | var isTargetObservers = Symbol();
|
117 | var isEnabled = Symbol();
|
118 | var getObservers = Symbol();
|
119 | /**
|
120 | * Observe a target's property for changes. When a property changes, the
|
121 | * provided `Observer` function will be invoked with the properties current and
|
122 | * previous values.
|
123 | *
|
124 | * The returned cleanup function will stop listening to changes for the
|
125 | * provided `Observer`.
|
126 | *
|
127 | * @template T The observed target type.
|
128 | * @template K The observed property.
|
129 | * @param target - The target to observe.
|
130 | * @param property - The property of the target to observe.
|
131 | * @param observer - An observer function to invoke each time the property
|
132 | * changes.
|
133 | * @return A cleanup function that will stop observing changes for the provided
|
134 | * `Observer`.
|
135 | */
|
136 | export function observeProperty(target, property, observer) {
|
137 | var observerPrototype = installObserver(target);
|
138 | var observers = observerPrototype[getObservers](property);
|
139 | observers.push(observer);
|
140 | return function () {
|
141 | observers.splice(observers.indexOf(observer), 1);
|
142 | };
|
143 | }
|
144 | /**
|
145 | * Installs a `TargetObservers` for the provided target (if not already
|
146 | * installed).
|
147 | *
|
148 | * A target's `TargetObservers` is installed as a Proxy on the target's
|
149 | * prototype.
|
150 | *
|
151 | * @template T The observed target type.
|
152 | * @param target - The target to observe.
|
153 | * @return The installed `TargetObservers` for the provided target.
|
154 | */
|
155 | function installObserver(target) {
|
156 | var e_4, _a, e_5, _b;
|
157 | var prototype = Object.getPrototypeOf(target);
|
158 | if (prototype[isTargetObservers]) {
|
159 | return prototype;
|
160 | }
|
161 | // Proxy prototypes will not trap plain properties (not a getter/setter) that
|
162 | // are already defined. They only work on new plain properties.
|
163 | // We can work around this by deleting the properties, installing the Proxy,
|
164 | // then re-setting the properties.
|
165 | var existingKeyValues = new Map();
|
166 | var keys = Object.getOwnPropertyNames(target);
|
167 | try {
|
168 | for (var keys_1 = __values(keys), keys_1_1 = keys_1.next(); !keys_1_1.done; keys_1_1 = keys_1.next()) {
|
169 | var key = keys_1_1.value;
|
170 | var descriptor = getDescriptor(target, key);
|
171 | if (descriptor && descriptor.writable) {
|
172 | existingKeyValues.set(key, descriptor.value);
|
173 | delete target[key];
|
174 | }
|
175 | }
|
176 | }
|
177 | catch (e_4_1) { e_4 = { error: e_4_1 }; }
|
178 | finally {
|
179 | try {
|
180 | if (keys_1_1 && !keys_1_1.done && (_a = keys_1.return)) _a.call(keys_1);
|
181 | }
|
182 | finally { if (e_4) throw e_4.error; }
|
183 | }
|
184 | var proxy = new Proxy(Object.create(prototype), {
|
185 | get: function (target, key, receiver) {
|
186 | return Reflect.get(target, key, receiver);
|
187 | },
|
188 | set: function (target, key, newValue, receiver) {
|
189 | var e_6, _a;
|
190 | var isTargetObserversKey = key === isTargetObservers ||
|
191 | key === isEnabled || key === getObservers;
|
192 | var previous = Reflect.get(target, key, receiver);
|
193 | // Do not use receiver when setting the target's key. We do not want
|
194 | // to change whatever the target's inherent receiver is.
|
195 | Reflect.set(target, key, newValue);
|
196 | if (!isTargetObserversKey && proxy[isEnabled] &&
|
197 | newValue !== previous) {
|
198 | try {
|
199 | for (var _b = __values(proxy[getObservers](key)), _c = _b.next(); !_c.done; _c = _b.next()) {
|
200 | var observer = _c.value;
|
201 | observer(newValue, previous);
|
202 | }
|
203 | }
|
204 | catch (e_6_1) { e_6 = { error: e_6_1 }; }
|
205 | finally {
|
206 | try {
|
207 | if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
208 | }
|
209 | finally { if (e_6) throw e_6.error; }
|
210 | }
|
211 | }
|
212 | return true;
|
213 | }
|
214 | });
|
215 | proxy[isTargetObservers] = true;
|
216 | proxy[isEnabled] = true;
|
217 | var observersMap = new Map();
|
218 | proxy[getObservers] = function (key) {
|
219 | var observers = observersMap.get(key) || [];
|
220 | if (!observersMap.has(key)) {
|
221 | observersMap.set(key, observers);
|
222 | }
|
223 | return observers;
|
224 | };
|
225 | Object.setPrototypeOf(target, proxy);
|
226 | try {
|
227 | // Re-set plain pre-existing properties so that the Proxy can trap them
|
228 | for (var _c = __values(existingKeyValues.entries()), _d = _c.next(); !_d.done; _d = _c.next()) {
|
229 | var _e = __read(_d.value, 2), key = _e[0], value = _e[1];
|
230 | target[key] = value;
|
231 | }
|
232 | }
|
233 | catch (e_5_1) { e_5 = { error: e_5_1 }; }
|
234 | finally {
|
235 | try {
|
236 | if (_d && !_d.done && (_b = _c.return)) _b.call(_c);
|
237 | }
|
238 | finally { if (e_5) throw e_5.error; }
|
239 | }
|
240 | return proxy;
|
241 | }
|
242 | /**
|
243 | * Enables or disables all observers for a provided target. Changes to observed
|
244 | * properties will not call any observers when disabled.
|
245 | *
|
246 | * @template T The observed target type.
|
247 | * @param target - The target to enable or disable observers for.
|
248 | * @param enabled - True to enable or false to disable observers.
|
249 | */
|
250 | export function setObserversEnabled(target, enabled) {
|
251 | var prototype = Object.getPrototypeOf(target);
|
252 | if (prototype[isTargetObservers]) {
|
253 | prototype[isEnabled] = enabled;
|
254 | }
|
255 | }
|
256 | //# sourceMappingURL=observer-proxy.js.map |
\ | No newline at end of file |