'use strict' ; import { IpcCommandParamsOf , IpcCommandType , IpcMessage , ReadyCommandType } from '../../protocol' ; import { Disposable } from './events' ; import { initializeAndWatchThemeColors } from './theme' ; interface VsCodeApi { postMessage ( msg ) : void ; setState ( state ) : void ; getState ( ) : object ; } declare function acquireVsCodeApi ( ) ; let ipcSequence = 0 ; function nextIpcId ( ) { if ( ipcSequence === Number . MAX_SAFE_INTEGER ) { ipcSequence = 1 ; } else { ipcSequence ++ ; } return `webview:${ ipcSequence }` ; } export abstract class App < State extends object > { private readonly _api : VsCodeApi ; protected state : State ; constructor ( protected readonly appName , state ) { this . log ( `${ this . appName }.ctor` ) ; this . _api = acquireVsCodeApi ( ) ; initializeAndWatchThemeColors ( ) ; this . state = state ; setTimeout ( ( ) => { this . log ( `${ this . appName }.initializing` ) ; this . onInitialize ?. ( ) ; this . bind ( ) ; if ( this . onMessageReceived != null ) { window . addEventListener ( 'message' , this . onMessageReceived . bind ( this ) ) ; } this . sendCommand ( ReadyCommandType , { } ) ; this . onInitialized ?. ( ) ; setTimeout ( ( ) => { document . body . classList . remove ( 'preload' ) ; } , 500 ) ; } , 0 ) ; } protected onInitialize ? ( ) ; protected onBind ? ( ) ; protected onInitialized ? ( ) ; protected onMessageReceived ? ( e ) ; private bindDisposables : Disposable [ ] | undefined ; protected bind ( ) { this . bindDisposables ?. forEach ( d => d . dispose ( ) ) ; this . bindDisposables = this . onBind ?. ( ) ; } protected log ( _message ) { } protected getState ( ) { return this . _api . getState ( ) as State ; } protected sendCommand < CT extends IpcCommandType > ( type , params ) { return this . postMessage ( { id : nextIpcId ( ) , method : type . method , params : params } ) ; } protected setState ( state ) { this . state = state ; this . _api . setState ( state ) ; } private postMessage ( e ) { this . _api . postMessage ( e ) ; } }