import { MDCFoundation } from '@material/base' ; export abstract class MdcComponent < FoundationType extends MDCFoundation > { constructor ( public root ) { } foundation ? : FoundationType ; continueAttaching : boolean ; initialised = this . createInitiliasedPromise ( ) ; protected initialisedResolve : ( value ? : unknown ) => void ; private async createInitiliasedPromise ( ) { return new Promise ( r => this . initialisedResolve = r ) ; } async initialise ( ) { } initialSyncWithDOM ( ) { } async attached ( ) { this . continueAttaching = true ; await this . initialise ( ) ; if ( ! this . continueAttaching ) { return ; } this . foundation = this . getDefaultFoundation ( ) ; this . foundation . init ( ) ; this . initialisedResolve ( ) ; this . initialSyncWithDOM ( ) ; } destroy ( ) { } detached ( ) { this . continueAttaching = false ; this . destroy ( ) ; this . foundation ?. destroy ( ) ; this . foundation = undefined ; this . initialised = this . createInitiliasedPromise ( ) ; } abstract getDefaultFoundation ( ) ; listen ( evtType , handler , options ? ) { this . root . addEventListener ( evtType , handler , options ) ; } unlisten ( evtType , handler , options ? ) { this . root . removeEventListener ( evtType , handler , options ) ; } hidden emit < T > ( evtType , evtData , shouldBubble = false ) { let evt ; if ( typeof CustomEvent === 'function' ) { evt = new CustomEvent < T > ( evtType , { bubbles : shouldBubble , detail : evtData , } ) ; } else { evt = document . createEvent ( 'CustomEvent' ) ; evt . initCustomEvent ( evtType , shouldBubble , false , evtData ) ; } this . root . dispatchEvent ( evt ) ; } }