mirror of
https://github.com/material-components/material-web.git
synced 2024-09-21 02:27:12 +03:00
41d41cc278
PiperOrigin-RevId: 455635969
42 lines
1.2 KiB
TypeScript
42 lines
1.2 KiB
TypeScript
/**
|
|
* @license
|
|
* Copyright 2021 Google LLC
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
*/
|
|
|
|
import {Foundation} from './foundation';
|
|
import {observeProperty, ObserverRecord} from './observer';
|
|
|
|
/**
|
|
* Legacy observer foundation class for components.
|
|
*/
|
|
export class ObserverFoundation<Adapter extends object> extends
|
|
Foundation<Adapter> {
|
|
/**
|
|
* Observe a target's properties for changes using the provided map of
|
|
* property names and observer functions.
|
|
*
|
|
* @template T The target type.
|
|
* @param target - The target to observe.
|
|
* @param observers - An object whose keys are target properties and values
|
|
* are observer functions that are called when the associated property
|
|
* changes.
|
|
* @return A cleanup function that can be called to unobserve the
|
|
* target.
|
|
*/
|
|
protected observe<T extends object>(
|
|
target: T, observers: ObserverRecord<T, this>) {
|
|
const cleanup: Function[] = [];
|
|
for (const property of Object.keys(observers) as Array<keyof T>) {
|
|
const observer = observers[property]!.bind(this);
|
|
cleanup.push(observeProperty(target, property, observer));
|
|
}
|
|
|
|
return () => {
|
|
for (const cleanupFn of cleanup) {
|
|
cleanupFn();
|
|
}
|
|
};
|
|
}
|
|
}
|