import IVSCServerManagerCommands from "./IVSCServerManagerCommands" ; import IVSCServerManagerEventsHandler from "./IVSCServerManagerEventsHandler" ; import IVSCServerManagerState from "./IVSCServerManagerState" ; import ServerBase from "./ServerBase" ; import VSCServerManagerCleaningState from "./VSCServerManagerCleaningState" ; import VSCServerManagerClosingState from "./VSCServerManagerClosingState" ; import { VSCServerManagerStartingState } from "./VSCServerManagerStartingState" ; export default abstract class VSCServerManagerBase implements IVSCServerManagerCommands { private handlers : IVSCServerManagerEventsHandler [ ] = [ ] ; private startSession = 0 ; private closeSession = 0 ; private cleanSession = 0 ; public constructor ( public readonly name , protected state , protected server ? ) { } public getStartCommand ( ) { throw Error ( "Should implemented in children class." ) ; } public getCloseCommand ( ) { throw Error ( "Should implemented in children class." ) ; } public getCleanCommand ( ) { throw Error ( "Should implemented in children class." ) ; } public addEventListener ( handler ) { this . handlers . push ( handler ) ; } public getServer ( ) { return this . server ; } public abstract createImpl ( ) ; public abstract startImpl ( ) ; public abstract closeImpl ( ) ; public abstract cleanImpl ( ) ; public async start ( ) { this . startSession ++ ; const previousState = this . state ; this . state = new VSCServerManagerStartingState ( ) ; try { this . onStart ( this . startSession ) ; this . state = await previousState . start ( this ) ; this . onStartSuccess ( this . startSession ) ; } catch ( err ) { this . state = previousState ; this . onStartFail ( this . startSession , err ) ; } } public async close ( ) { this . closeSession ++ ; const previousState = this . state ; this . state = new VSCServerManagerClosingState ( ) ; try { this . onClose ( this . closeSession ) ; this . state = await previousState . close ( this ) ; this . onCloseSuccess ( this . closeSession ) ; } catch ( err ) { this . state = previousState ; this . onCloseFail ( this . closeSession , err ) ; } } public async clean ( ) { this . cleanSession ++ ; const previousState = this . state ; this . state = new VSCServerManagerCleaningState ( ) ; try { this . onClean ( this . cleanSession ) ; this . state = await previousState . clean ( this ) ; this . onCleanSuccess ( this . cleanSession ) ; } catch ( err ) { this . state = previousState ; this . onCleanFail ( this . cleanSession , err ) ; } } protected onStart ( session ) { for ( const handler of this . handlers ) { try { handler . onStart ( this , session ) ; } catch { } } } protected onStartFail ( session , error ) { for ( const handler of this . handlers ) { try { handler . onStartFail ( this , session , error ) ; } catch { } } } protected onStartSuccess ( session ) { for ( const handler of this . handlers ) { try { handler . onStartSuccess ( this , session ) ; } catch { } } } protected onClean ( session ) { for ( const handler of this . handlers ) { try { handler . onClean ( this , session ) ; } catch { } } } protected onCleanFail ( session , error ) { for ( const handler of this . handlers ) { try { handler . onCleanFail ( this , session , error ) ; } catch { } } } protected onCleanSuccess ( session ) { for ( const handler of this . handlers ) { try { handler . onCleanSuccess ( this , session ) ; } catch { } } } protected onClose ( session ) { for ( const handler of this . handlers ) { try { handler . onClose ( this , session ) ; } catch { } } } protected onCloseFail ( session , error ) { for ( const handler of this . handlers ) { try { handler . onCloseFail ( this , session , error ) ; } catch { } } } protected onCloseSuccess ( session ) { for ( const handler of this . handlers ) { try { handler . onCloseSuccess ( this , session ) ; } catch { } } } }