observer-foundation.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 { MDCFoundation } from './foundation';
  25. import { observeProperty, setObserversEnabled } from './observer';
  26. // @ts-ignore(go/ts48upgrade) Fix code and remove this comment. Error:
  27. // TS2344: Type 'Adapter' does not satisfy the constraint '{}'.
  28. var MDCObserverFoundation = /** @class */ (function (_super) {
  29. __extends(MDCObserverFoundation, _super);
  30. function MDCObserverFoundation(adapter) {
  31. var _this = _super.call(this, adapter) || this;
  32. /** A set of cleanup functions to unobserve changes. */
  33. _this.unobserves = new Set();
  34. return _this;
  35. }
  36. MDCObserverFoundation.prototype.destroy = function () {
  37. _super.prototype.destroy.call(this);
  38. this.unobserve();
  39. };
  40. /**
  41. * Observe a target's properties for changes using the provided map of
  42. * property names and observer functions.
  43. *
  44. * @template T The target type.
  45. * @param target - The target to observe.
  46. * @param observers - An object whose keys are target properties and values
  47. * are observer functions that are called when the associated property
  48. * changes.
  49. * @return A cleanup function that can be called to unobserve the
  50. * target.
  51. */
  52. MDCObserverFoundation.prototype.observe = function (target, observers) {
  53. var e_1, _a;
  54. var _this = this;
  55. var cleanup = [];
  56. try {
  57. for (var _b = __values(Object.keys(observers)), _c = _b.next(); !_c.done; _c = _b.next()) {
  58. var property = _c.value;
  59. var observer = observers[property].bind(this);
  60. cleanup.push(this.observeProperty(target, property, observer));
  61. }
  62. }
  63. catch (e_1_1) { e_1 = { error: e_1_1 }; }
  64. finally {
  65. try {
  66. if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
  67. }
  68. finally { if (e_1) throw e_1.error; }
  69. }
  70. var unobserve = function () {
  71. var e_2, _a;
  72. try {
  73. for (var cleanup_1 = __values(cleanup), cleanup_1_1 = cleanup_1.next(); !cleanup_1_1.done; cleanup_1_1 = cleanup_1.next()) {
  74. var cleanupFn = cleanup_1_1.value;
  75. cleanupFn();
  76. }
  77. }
  78. catch (e_2_1) { e_2 = { error: e_2_1 }; }
  79. finally {
  80. try {
  81. if (cleanup_1_1 && !cleanup_1_1.done && (_a = cleanup_1.return)) _a.call(cleanup_1);
  82. }
  83. finally { if (e_2) throw e_2.error; }
  84. }
  85. _this.unobserves.delete(unobserve);
  86. };
  87. this.unobserves.add(unobserve);
  88. return unobserve;
  89. };
  90. /**
  91. * Observe a target's property for changes. When a property changes, the
  92. * provided `Observer` function will be invoked with the properties current
  93. * and previous values.
  94. *
  95. * The returned cleanup function will stop listening to changes for the
  96. * provided `Observer`.
  97. *
  98. * @template T The observed target type.
  99. * @template K The observed property.
  100. * @param target - The target to observe.
  101. * @param property - The property of the target to observe.
  102. * @param observer - An observer function to invoke each time the property
  103. * changes.
  104. * @return A cleanup function that will stop observing changes for the
  105. * provided `Observer`.
  106. */
  107. MDCObserverFoundation.prototype.observeProperty = function (target, property, observer) {
  108. return observeProperty(target, property, observer);
  109. };
  110. /**
  111. * Enables or disables all observers for the provided target. Disabling
  112. * observers will prevent them from being called until they are re-enabled.
  113. *
  114. * @param target - The target to enable or disable observers for.
  115. * @param enabled - Whether or not observers should be called.
  116. */
  117. MDCObserverFoundation.prototype.setObserversEnabled = function (target, enabled) {
  118. setObserversEnabled(target, enabled);
  119. };
  120. /**
  121. * Clean up all observers and stop listening for property changes.
  122. */
  123. MDCObserverFoundation.prototype.unobserve = function () {
  124. var e_3, _a;
  125. try {
  126. // Iterate over a copy since unobserve() will remove themselves from the set
  127. for (var _b = __values(__spreadArray([], __read(this.unobserves))), _c = _b.next(); !_c.done; _c = _b.next()) {
  128. var unobserve = _c.value;
  129. unobserve();
  130. }
  131. }
  132. catch (e_3_1) { e_3 = { error: e_3_1 }; }
  133. finally {
  134. try {
  135. if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
  136. }
  137. finally { if (e_3) throw e_3.error; }
  138. }
  139. };
  140. return MDCObserverFoundation;
  141. }(MDCFoundation));
  142. export { MDCObserverFoundation };
  143. //# sourceMappingURL=observer-foundation.js.map