observer-foundation.d.ts 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 { MDCFoundation } from './foundation';
  24. import { Observer, ObserverRecord } from './observer';
  25. export declare class MDCObserverFoundation<Adapter> extends MDCFoundation<Adapter> {
  26. /** A set of cleanup functions to unobserve changes. */
  27. protected unobserves: Set<Function>;
  28. constructor(adapter: Adapter);
  29. destroy(): void;
  30. /**
  31. * Observe a target's properties for changes using the provided map of
  32. * property names and observer functions.
  33. *
  34. * @template T The target type.
  35. * @param target - The target to observe.
  36. * @param observers - An object whose keys are target properties and values
  37. * are observer functions that are called when the associated property
  38. * changes.
  39. * @return A cleanup function that can be called to unobserve the
  40. * target.
  41. */
  42. protected observe<T extends object>(target: T, observers: ObserverRecord<T, this>): () => void;
  43. /**
  44. * Observe a target's property for changes. When a property changes, the
  45. * provided `Observer` function will be invoked with the properties current
  46. * and previous values.
  47. *
  48. * The returned cleanup function will stop listening to changes for the
  49. * provided `Observer`.
  50. *
  51. * @template T The observed target type.
  52. * @template K The observed property.
  53. * @param target - The target to observe.
  54. * @param property - The property of the target to observe.
  55. * @param observer - An observer function to invoke each time the property
  56. * changes.
  57. * @return A cleanup function that will stop observing changes for the
  58. * provided `Observer`.
  59. */
  60. protected observeProperty<T extends object, K extends keyof T>(target: T, property: K, observer: Observer<T, K>): () => void;
  61. /**
  62. * Enables or disables all observers for the provided target. Disabling
  63. * observers will prevent them from being called until they are re-enabled.
  64. *
  65. * @param target - The target to enable or disable observers for.
  66. * @param enabled - Whether or not observers should be called.
  67. */
  68. protected setObserversEnabled(target: object, enabled: boolean): void;
  69. /**
  70. * Clean up all observers and stop listening for property changes.
  71. */
  72. protected unobserve(): void;
  73. }