import { AppPluginBase } from '../AppPluginBase' ; import { Application } from 'egg' ; import { HostingPlatformInitEvent , HostingPlatformSyncDataEvent } from './event' ; import { HostingBase } from './HostingBase' ; import { HostingClientBase } from './HostingClientBase' ; import { waitUntil } from '../Utils' ; import { HostingConfigBase } from './HostingConfigBase' ; export abstract class HostingManagerBase < THostingPlatform extends HostingBase < TConfig , TClient , TRawClient > , TClient extends HostingClientBase < TConfig , TRawClient > , TRawClient , TConfig extends HostingConfigBase > extends AppPluginBase < null > { protected type : string ; private hpMap : Map < number , THostingPlatform > ; constructor ( config , app ) { super ( config , app ) ; this . hpMap = new Map < number , THostingPlatform > ( ) ; } public async onReady ( ) { this . app . event . subscribeOne ( HostingPlatformInitEvent , async ( e ) => { if ( e . type === this . type ) { await waitUntil ( ( ) => this . hpMap . has ( e . id ) , { interval : 500 } ) ; this . app . event . publish ( 'worker' , HostingPlatformSyncDataEvent , { id : e . id , } ) ; } } ) ; this . app . event . subscribeAll ( HostingPlatformInitEvent , async ( e ) => { if ( e . type === this . type ) { const hp = await this . getNewHostingPlatform ( e . id , e . config ) ; this . hpMap . set ( e . id , hp ) ; } } ) ; } public async onStart ( ) { } public async onClose ( ) { } public abstract getConfigType ( ) ; protected abstract getNewHostingPlatform ( id , config ) ; public getHostPlatform ( id ) { return this . hpMap . get ( id ) ; } public async getClient ( id , name ) { const hp = this . hpMap . get ( id ) ; if ( hp ) { return hp . getClient ( name ) ; } return undefined ; } public getHostingPlatformById ( id ) { return this . hpMap . get ( id ) ; } }