diff --git a/packages/core-browser/src/application/application.service.ts b/packages/core-browser/src/application/application.service.ts index 50d85d7e98..0d0cb71ee2 100644 --- a/packages/core-browser/src/application/application.service.ts +++ b/packages/core-browser/src/application/application.service.ts @@ -1,4 +1,5 @@ -import { Injectable, Autowired } from '@opensumi/di'; +import { Injectable, Autowired, INJECTOR_TOKEN, Injector } from '@opensumi/di'; +import { WSChannelHandler } from '@opensumi/ide-connection/lib/browser/ws-channel-handler'; import { OS, OperatingSystem, @@ -9,6 +10,7 @@ import { } from '@opensumi/ide-core-common'; import { AppConfig } from '../react-providers/config-provider'; +import { electronEnv } from '../utils/electron'; @Injectable() export class ApplicationService implements IApplicationService { @@ -18,6 +20,9 @@ export class ApplicationService implements IApplicationService { @Autowired(AppConfig) private readonly appConfig: AppConfig; + @Autowired(INJECTOR_TOKEN) + protected readonly injector: Injector; + private _backendOS: OperatingSystem; private _initialized = new Deferred(); @@ -48,4 +53,13 @@ export class ApplicationService implements IApplicationService { await this._initialized.promise; return this.backendOS; } + + get clientId(): string { + if (this.appConfig.isElectronRenderer && !this.appConfig.isRemote) { + return electronEnv.metadata.windowClientId; + } else { + const wsChannel = this.injector.get(WSChannelHandler); + return wsChannel.clientId; + } + } } diff --git a/packages/core-common/src/types/application.ts b/packages/core-common/src/types/application.ts index 998420b5e4..ef32e5d3d8 100644 --- a/packages/core-common/src/types/application.ts +++ b/packages/core-common/src/types/application.ts @@ -3,6 +3,12 @@ import { OperatingSystem } from '@opensumi/ide-utils'; export const IApplicationService = Symbol('IApplicationService'); export interface IApplicationService { + /** + * In Electron environment, if `isRemote` is not specified, use local connection by default: `electronEnv.metadata.windowClientId` + * Otherwise, use WebSocket connection: `WSChannelHandler.clientId` + */ + clientId: string; + /** 前端 OS */ frontendOS: OperatingSystem; /** 后端 OS */ diff --git a/packages/extension/src/browser/extension-node.service.ts b/packages/extension/src/browser/extension-node.service.ts index eeac9de5df..98720f9aef 100644 --- a/packages/extension/src/browser/extension-node.service.ts +++ b/packages/extension/src/browser/extension-node.service.ts @@ -17,6 +17,7 @@ import { IDisposable, toDisposable, createElectronClientConnection, + IApplicationService, } from '@opensumi/ide-core-browser'; import { @@ -47,6 +48,9 @@ export class NodeExtProcessService implements AbstractNodeExtProcessService = new Deferred(); @@ -136,17 +140,7 @@ export class NodeExtProcessService implements AbstractNodeExtProcessService { - let clientId: string; - const appConfig: AppConfig = injector.get(AppConfig); - - // Electron 环境下,未指定 isRemote 时默认使用本地连接 - // 否则使用 WebSocket 连接 - if (appConfig.isElectronRenderer && !appConfig.isRemote) { - clientId = electronEnv.metadata.windowClientId; - } else { - const channelHandler = injector.get(WSChannelHandler); - clientId = channelHandler.clientId; - } - return clientId; + const service: IApplicationService = injector.get(IApplicationService); + return service.clientId; }; @Domain(ClientAppContribution) diff --git a/packages/remote-opener/src/browser/remote.opener.contribution.ts b/packages/remote-opener/src/browser/remote.opener.contribution.ts index 3f9901cc41..d75285d8da 100644 --- a/packages/remote-opener/src/browser/remote.opener.contribution.ts +++ b/packages/remote-opener/src/browser/remote.opener.contribution.ts @@ -1,7 +1,6 @@ import { Autowired, Injector, INJECTOR_TOKEN } from '@opensumi/di'; -import { WSChannelHandler } from '@opensumi/ide-connection/lib/browser'; -import { AppConfig, ClientAppContribution, electronEnv } from '@opensumi/ide-core-browser'; -import { ContributionProvider, Domain, getDebugLogger } from '@opensumi/ide-core-common'; +import { ClientAppContribution } from '@opensumi/ide-core-browser'; +import { ContributionProvider, Domain, getDebugLogger, IApplicationService } from '@opensumi/ide-core-common'; import { IRemoteOpenerService, RemoteOpenerServicePath } from '../common'; import { @@ -12,18 +11,8 @@ import { // 从extension.contribution.ts中Copy过来,因为直接引入会有一定概率触发IDE初始化问题 const getClientId = (injector: Injector) => { - let clientId: string; - const appConfig: AppConfig = injector.get(AppConfig); - - // Electron 环境下,未指定 isRemote 时默认使用本地连接 - // 否则使用 WebSocket 连接 - if (appConfig.isElectronRenderer && !appConfig.isRemote) { - clientId = electronEnv.metadata.windowClientId; - } else { - const channelHandler = injector.get(WSChannelHandler); - clientId = channelHandler.clientId; - } - return clientId; + const service: IApplicationService = injector.get(IApplicationService); + return service.clientId; }; @Domain(ClientAppContribution) diff --git a/packages/terminal-next/__tests__/browser/inject.ts b/packages/terminal-next/__tests__/browser/inject.ts index b010b547dc..31b2d55a92 100644 --- a/packages/terminal-next/__tests__/browser/inject.ts +++ b/packages/terminal-next/__tests__/browser/inject.ts @@ -5,6 +5,7 @@ import { PreferenceService, EventBusImpl, CorePreferences, + ApplicationService, } from '@opensumi/ide-core-browser'; import { MockContextKeyService } from '@opensumi/ide-core-browser/__mocks__/context-key'; import { MockLogger, MockLoggerManageClient, MockLoggerService } from '@opensumi/ide-core-browser/__mocks__/logger'; @@ -19,6 +20,7 @@ import { ILoggerManagerClient, ILogServiceManager, ILogger, + IApplicationService, } from '@opensumi/ide-core-common'; import { MockInjector } from '@opensumi/ide-dev-tool/src/mock-injector'; import { WorkbenchEditorService } from '@opensumi/ide-editor'; @@ -88,6 +90,10 @@ export const injector = new MockInjector([ token: ITerminalService, useClass: MockSocketService, }, + { + token: IApplicationService, + useClass: ApplicationService, + }, { token: IContextKeyService, useValue: new MockContextKeyService(), diff --git a/packages/terminal-next/src/browser/terminal.controller.ts b/packages/terminal-next/src/browser/terminal.controller.ts index 24233cb27a..3cfb4058ba 100644 --- a/packages/terminal-next/src/browser/terminal.controller.ts +++ b/packages/terminal-next/src/browser/terminal.controller.ts @@ -21,6 +21,7 @@ import { withNullAsUndefined, isThemeColor, Uri, + IApplicationService, } from '@opensumi/ide-core-common'; import { WorkbenchEditorService } from '@opensumi/ide-editor'; import { IMainLayoutService } from '@opensumi/ide-main-layout'; @@ -118,6 +119,9 @@ export class TerminalController extends WithEventBus implements ITerminalControl @Autowired(IMenuRegistry) private readonly menuRegistry: IMenuRegistry; + @Autowired(IApplicationService) + protected readonly applicationService: IApplicationService; + @Autowired(INJECTOR_TOKEN) private readonly injector: Injector; @@ -177,12 +181,7 @@ export class TerminalController extends WithEventBus implements ITerminalControl if (this._clientId) { return this._clientId; } - if (this.appConfig.isElectronRenderer) { - this._clientId = (global as any).metadata?.windowClientId; - } else { - const WSHandler = this.injector.get(WSChannelHandler); - this._clientId = WSHandler.clientId; - } + this._clientId = this.applicationService.clientId; return this._clientId; } diff --git a/packages/terminal-next/src/browser/terminal.service.ts b/packages/terminal-next/src/browser/terminal.service.ts index dd070de03c..f94117b6b0 100644 --- a/packages/terminal-next/src/browser/terminal.service.ts +++ b/packages/terminal-next/src/browser/terminal.service.ts @@ -1,9 +1,8 @@ import { Emitter as Dispatcher } from 'event-kit'; import { Injectable, Autowired, Injector, INJECTOR_TOKEN } from '@opensumi/di'; -import { WSChannelHandler as IWSChannelHandler } from '@opensumi/ide-connection/lib/browser/ws-channel-handler'; -import { AppConfig, electronEnv, PreferenceService, OperatingSystem } from '@opensumi/ide-core-browser'; -import { Emitter, ILogger, Event } from '@opensumi/ide-core-common'; +import { PreferenceService, OperatingSystem } from '@opensumi/ide-core-browser'; +import { Emitter, ILogger, Event, IApplicationService } from '@opensumi/ide-core-common'; import { generateSessionId, @@ -46,8 +45,8 @@ export class NodePtyTerminalService implements ITerminalService { @Autowired(PreferenceService) private preferenceService: PreferenceService; - @Autowired(AppConfig) - private readonly appConfig: AppConfig; + @Autowired(IApplicationService) + protected readonly applicationService: IApplicationService; private _onError = new Emitter(); public onError: Event = this._onError.event; @@ -70,14 +69,7 @@ export class NodePtyTerminalService implements ITerminalService { >(); generateSessionId() { - // Electron 环境下,未指定 isRemote 时默认使用本地连接 - // 否则使用 WebSocket 连接 - if (this.appConfig.isElectronRenderer && !this.appConfig.isRemote) { - return electronEnv.metadata.windowClientId + TERMINAL_ID_SEPARATOR + generateSessionId(); - } else { - const WSChannelHandler = this.injector.get(IWSChannelHandler); - return WSChannelHandler.clientId + TERMINAL_ID_SEPARATOR + generateSessionId(); - } + return this.applicationService.clientId + TERMINAL_ID_SEPARATOR + generateSessionId(); } async check(ids: string[]) {