UNPKG

5.94 kBTypeScriptView Raw
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 */
23import { Constructor } from './types';
24/**
25 * A class that can observe targets and perform cleanup logic. Classes may
26 * implement this using the `mdcObserver()` mixin.
27 */
28export interface MDCObserver {
29 /**
30 * Observe a target's properties for changes using the provided map of
31 * property names and observer functions.
32 *
33 * @template T The target type.
34 * @param target - The target to observe.
35 * @param observers - An object whose keys are target properties and values
36 * are observer functions that are called when the associated property
37 * changes.
38 * @return A cleanup function that can be called to unobserve the
39 * target.
40 */
41 observe<T extends object>(target: T, observers: ObserverRecord<T, this>): () => void;
42 /**
43 * Enables or disables all observers for the provided target. Disabling
44 * observers will prevent them from being called until they are re-enabled.
45 *
46 * @param target - The target to enable or disable observers for.
47 * @param enabled - Whether or not observers should be called.
48 */
49 setObserversEnabled(target: object, enabled: boolean): void;
50 /**
51 * Clean up all observers and stop listening for property changes.
52 */
53 unobserve(): void;
54}
55/**
56 * A function used to observe property changes on a target.
57 *
58 * @template T The observed target type.
59 * @template K The observed property.
60 * @template This The `this` context of the observer function.
61 * @param current - The current value of the property.
62 * @param previous - The previous value of the property.
63 */
64export declare type Observer<T extends object, K extends keyof T = keyof T, This = unknown> = (this: This, current: T[K], previous: T[K]) => void;
65/**
66 * An object map whose keys are properties of a target to observe and values
67 * are `Observer` functions for each property.
68 *
69 * @template T The observed target type.
70 * @template This The `this` context of observer functions.
71 */
72export declare type ObserverRecord<T extends object, This = unknown> = {
73 [K in keyof T]?: Observer<T, K, This>;
74};
75/**
76 * Mixin to add `MDCObserver` functionality.
77 *
78 * @deprecated Prefer MDCObserverFoundation for stricter closure compliance.
79 * @return A class with `MDCObserver` functionality.
80 */
81export declare function mdcObserver(): Constructor<MDCObserver>;
82/**
83 * Mixin to add `MDCObserver` functionality to a base class.
84 *
85 * @deprecated Prefer MDCObserverFoundation for stricter closure compliance.
86 * @template T Base class instance type. Specify this generic if the base class
87 * itself has generics that cannot be inferred.
88 * @template C Base class constructor type.
89 * @param baseClass - Base class.
90 * @return A class that extends the optional base class with `MDCObserver`
91 * functionality.
92 */
93export declare function mdcObserver<T, C extends Constructor<T>>(baseClass: C): Constructor<MDCObserver> & Constructor<T> & C;
94/**
95 * Observe a target's property for changes. When a property changes, the
96 * provided `Observer` function will be invoked with the properties current and
97 * previous values.
98 *
99 * The returned cleanup function will stop listening to changes for the
100 * provided `Observer`.
101 *
102 * @template T The observed target type.
103 * @template K The observed property.
104 * @param target - The target to observe.
105 * @param property - The property of the target to observe.
106 * @param observer - An observer function to invoke each time the property
107 * changes.
108 * @return A cleanup function that will stop observing changes for the provided
109 * `Observer`.
110 */
111export declare function observeProperty<T extends object, K extends keyof T>(target: T, property: K, observer: Observer<T, K>): () => void;
112/**
113 * Retrieves the descriptor for a property from the provided target. This
114 * function will walk up the target's prototype chain to search for the
115 * descriptor.
116 *
117 * @template T The target type.
118 * @template K The property type.
119 * @param target - The target to retrieve a descriptor from.
120 * @param property - The name of the property to retrieve a descriptor for.
121 * @return the descriptor, or undefined if it does not exist. Keep in mind that
122 * plain properties may not have a descriptor defined.
123 */
124export declare function getDescriptor<T extends object, K extends keyof T>(target: T, property: K): TypedPropertyDescriptor<T[K]> | undefined;
125/**
126 * Enables or disables all observers for a provided target. Changes to observed
127 * properties will not call any observers when disabled.
128 *
129 * @template T The observed target type.
130 * @param target - The target to enable or disable observers for.
131 * @param enabled - True to enable or false to disable observers.
132 */
133export declare function setObserversEnabled<T extends object>(target: T, enabled: boolean): void;