|
| 1 | +import { Injectable, Injector, ConstructorOf, Provider } from '@ali/common-di' |
| 2 | +import { |
| 3 | + BrowserModule, |
| 4 | + ClientApp as BasicClientApp, |
| 5 | + IAppRenderer, |
| 6 | + IClientAppOpts as IBasicClientAppOpts, |
| 7 | +} from '@ali/ide-core-browser' |
| 8 | +import { BasicModule } from '@ali/ide-core-common' |
| 9 | + |
| 10 | +import { FCServiceCenter, ClientPort, initFCService } from '../connection' |
| 11 | +import { KaitianExtFsProvider, KtExtFsProviderContribution } from './extension' |
| 12 | +import { IServerApp } from '../common' |
| 13 | + |
| 14 | +export * from './extension' |
| 15 | + |
| 16 | +export type ModuleConstructor = ConstructorOf<BrowserModule> |
| 17 | + |
| 18 | +@Injectable() |
| 19 | +export class ClientModule extends BrowserModule { |
| 20 | + providers = [KaitianExtFsProvider, KtExtFsProviderContribution] |
| 21 | +} |
| 22 | + |
| 23 | +export interface IClientAppOpts extends IBasicClientAppOpts { |
| 24 | + serverApp: IServerApp |
| 25 | +} |
| 26 | + |
| 27 | +export class ClientApp extends BasicClientApp { |
| 28 | + constructor(opts: IClientAppOpts) { |
| 29 | + super(opts) |
| 30 | + opts.injector?.addProviders({ |
| 31 | + token: IServerApp, |
| 32 | + useValue: opts.serverApp, |
| 33 | + }) |
| 34 | + } |
| 35 | + |
| 36 | + public async start(container: HTMLElement | IAppRenderer, type?: string): Promise<void> { |
| 37 | + bindConnectionService(this.injector, this.modules) |
| 38 | + return super.start(container, type) |
| 39 | + } |
| 40 | + |
| 41 | + public dispose() { |
| 42 | + this.injector.disposeAll() |
| 43 | + this.injector.get(IServerApp).dispose() |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +export async function bindConnectionService(injector: Injector, modules: ModuleConstructor[]) { |
| 48 | + const clientCenter = new FCServiceCenter(ClientPort) |
| 49 | + |
| 50 | + const { getFCService } = initFCService(clientCenter) |
| 51 | + |
| 52 | + for (const module of modules) { |
| 53 | + const moduleInstance = injector.get(module) as BasicModule |
| 54 | + if (!moduleInstance.backServices) { |
| 55 | + continue |
| 56 | + } |
| 57 | + for (const backService of moduleInstance.backServices) { |
| 58 | + const { servicePath } = backService |
| 59 | + const fcService = getFCService(servicePath) |
| 60 | + |
| 61 | + const injectService = { |
| 62 | + token: servicePath, |
| 63 | + useValue: fcService, |
| 64 | + } as Provider |
| 65 | + |
| 66 | + injector.addProviders(injectService) |
| 67 | + |
| 68 | + if (backService.clientToken) { |
| 69 | + const clientService = injector.get(backService.clientToken) |
| 70 | + fcService.onRequestService(clientService) |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | +} |
0 commit comments