license import { MDCFoundation } from './foundation' ; import { observeProperty , Observer , ObserverRecord , setObserversEnabled } from './observer' ; export class MDCObserverFoundation < Adapter > extends MDCFoundation < Adapter > { /** A set of cleanup functions to unobserve changes. */ protected unobserves = new Set < Function > ( ) ; constructor ( adapter ) { super ( adapter ) ; } override destroy ( ) { super . destroy ( ) ; this . unobserve ( ) ; } template T param target param observers return protected observe < T extends object > ( target , observers ) { const cleanup = [ ] ; for ( const property of Object . keys ( observers ) as Array < keyof T > ) { const observer = observers [ property ] ! . bind ( this ) ; cleanup . push ( this . observeProperty ( target , property , observer ) ) ; } const unobserve = ( ) => { for ( const cleanupFn of cleanup ) { cleanupFn ( ) ; } this . unobserves . delete ( unobserve ) ; } ; this . unobserves . add ( unobserve ) ; return unobserve ; } template T template K param target param property param observer return protected observeProperty < T extends object , K extends keyof T > ( target , property , observer ) { return observeProperty ( target , property , observer ) ; } param target param enabled protected setObserversEnabled ( target , enabled ) { setObserversEnabled ( target , enabled ) ; } /**
     * Clean up all observers and stop listening for property changes.
     */ protected unobserve ( ) { for ( const unobserve of [ ... this . unobserves ] ) { unobserve ( ) ; } } }