Skip to content

Commit

Permalink
refactor: consolidated clientId retrieval code (#2703)
Browse files Browse the repository at this point in the history
  • Loading branch information
bytemain authored May 18, 2023
1 parent b085e3f commit 9a1e368
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 61 deletions.
16 changes: 15 additions & 1 deletion packages/core-browser/src/application/application.service.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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 {
Expand All @@ -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<void>();
Expand Down Expand Up @@ -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;
}
}
}
6 changes: 6 additions & 0 deletions packages/core-common/src/types/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
16 changes: 5 additions & 11 deletions packages/extension/src/browser/extension-node.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
IDisposable,
toDisposable,
createElectronClientConnection,
IApplicationService,
} from '@opensumi/ide-core-browser';

import {
Expand Down Expand Up @@ -47,6 +48,9 @@ export class NodeExtProcessService implements AbstractNodeExtProcessService<IExt
@Autowired(ExtensionNodeServiceServerPath)
private readonly extensionNodeClient: IExtensionNodeClientService;

@Autowired(IApplicationService)
protected readonly applicationService: IApplicationService;

private _apiFactoryDisposables: IDisposable[] = [];

public ready: Deferred<void> = new Deferred();
Expand Down Expand Up @@ -136,17 +140,7 @@ export class NodeExtProcessService implements AbstractNodeExtProcessService<IExt
}

private get clientId() {
let clientId: string;

if (this.appConfig.isElectronRenderer && !this.appConfig.isRemote) {
this.logger.verbose('createExtProcess electronEnv.metadata.windowClientId', electronEnv.metadata.windowClientId);
clientId = electronEnv.metadata.windowClientId;
} else {
const WSChannelHandler = this.injector.get(IWSChannelHandler);
clientId = WSChannelHandler.clientId;
}

return clientId;
return this.applicationService.clientId;
}

private async createExtProcess() {
Expand Down
18 changes: 3 additions & 15 deletions packages/extension/src/browser/extension.contribution.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type vscode from 'vscode';

import { Autowired, Injector, INJECTOR_TOKEN } from '@opensumi/di';
import { WSChannelHandler } from '@opensumi/ide-connection/lib/browser';
import {
EDITOR_COMMANDS,
UriComponents,
Expand All @@ -10,7 +9,6 @@ import {
CommandRegistry,
CommandService,
Domain,
electronEnv,
FILE_COMMANDS,
formatLocalize,
getIcon,
Expand All @@ -25,10 +23,10 @@ import {
replaceLocalizePlaceholder,
URI,
ILogger,
AppConfig,
CUSTOM_EDITOR_SCHEME,
runWhenIdle,
QuickPickService,
IApplicationService,
} from '@opensumi/ide-core-browser';
import {
IStatusBarService,
Expand Down Expand Up @@ -64,18 +62,8 @@ import * as VSCodeBuiltinCommands from './vscode/builtin-commands';
import { WalkthroughsService } from './walkthroughs.service';

export 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)
Expand Down
19 changes: 4 additions & 15 deletions packages/remote-opener/src/browser/remote.opener.contribution.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions packages/terminal-next/__tests__/browser/inject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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';
Expand Down Expand Up @@ -88,6 +90,10 @@ export const injector = new MockInjector([
token: ITerminalService,
useClass: MockSocketService,
},
{
token: IApplicationService,
useClass: ApplicationService,
},
{
token: IContextKeyService,
useValue: new MockContextKeyService(),
Expand Down
11 changes: 5 additions & 6 deletions packages/terminal-next/src/browser/terminal.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}

Expand Down
18 changes: 5 additions & 13 deletions packages/terminal-next/src/browser/terminal.service.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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<ITerminalError>();
public onError: Event<ITerminalError> = this._onError.event;
Expand All @@ -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[]) {
Expand Down

0 comments on commit 9a1e368

Please sign in to comment.