From a25cfe1dbc5688fbcc39afd3d1b6602948312457 Mon Sep 17 00:00:00 2001 From: Alex Dima Date: Sat, 25 Jun 2016 20:44:29 +0200 Subject: [PATCH] Simplify IThreadService now that ICompatWorkerService is used --- src/vs/editor/common/languages.common.ts | 1 - .../common/services/compatWorkerService.ts | 10 --- .../platform/test/common/nullThreadService.ts | 25 +----- .../thread/common/abstractThreadService.ts | 67 +--------------- .../thread/common/extHostThreadService.ts | 21 +---- .../thread/common/mainThreadService.ts | 31 ++------ src/vs/platform/thread/common/thread.ts | 45 ++--------- .../platform/thread/common/threadService.ts | 33 -------- .../thread/common/workerThreadService.ts | 78 ------------------- src/vs/workbench/browser/workbench.ts | 6 +- .../thread/electron-browser/threadService.ts | 2 +- 11 files changed, 24 insertions(+), 295 deletions(-) delete mode 100644 src/vs/platform/thread/common/threadService.ts delete mode 100644 src/vs/platform/thread/common/workerThreadService.ts diff --git a/src/vs/editor/common/languages.common.ts b/src/vs/editor/common/languages.common.ts index 069ad74c780..0a40e8addc2 100644 --- a/src/vs/editor/common/languages.common.ts +++ b/src/vs/editor/common/languages.common.ts @@ -23,7 +23,6 @@ import 'vs/platform/jsonschemas/common/jsonContributionRegistry'; import 'vs/platform/request/common/request'; import 'vs/platform/workspace/common/workspace'; import 'vs/platform/telemetry/common/telemetry'; -import 'vs/platform/thread/common/thread'; // editor common import 'vs/editor/common/editorCommon'; diff --git a/src/vs/editor/common/services/compatWorkerService.ts b/src/vs/editor/common/services/compatWorkerService.ts index b1afb74443c..4edb621d8a8 100644 --- a/src/vs/editor/common/services/compatWorkerService.ts +++ b/src/vs/editor/common/services/compatWorkerService.ts @@ -46,13 +46,3 @@ export function CompatWorkerAttr(type: Function, target: Function): void { return obj.compatWorkerService.CompatWorker(obj, methodName, target, param); }; } - -// export class NullCompatWorkerService implements ICompatWorkerService { -// public serviceId = ICompatWorkerService; -// public isInMainThread = true; - -// constructor() {} -// registerCompatMode(compatMode:ICompatMode): void {} -// } - -// export const NULL_COMPAT_WORKER_SERVICE = new NullCompatWorkerService(); diff --git a/src/vs/platform/test/common/nullThreadService.ts b/src/vs/platform/test/common/nullThreadService.ts index ec8aeb26e9d..1e0f7df71f7 100644 --- a/src/vs/platform/test/common/nullThreadService.ts +++ b/src/vs/platform/test/common/nullThreadService.ts @@ -4,29 +4,20 @@ *--------------------------------------------------------------------------------------------*/ 'use strict'; -import {TPromise} from 'vs/base/common/winjs.base'; import abstractThreadService = require('vs/platform/thread/common/abstractThreadService'); import {InstantiationService} from 'vs/platform/instantiation/common/instantiationService'; import {ServiceCollection} from 'vs/platform/instantiation/common/serviceCollection'; import {SyncDescriptor0} from 'vs/platform/instantiation/common/descriptors'; -import {IThreadService, IThreadSynchronizableObject} from 'vs/platform/thread/common/thread'; +import {IThreadService} from 'vs/platform/thread/common/thread'; export class NullThreadService extends abstractThreadService.AbstractThreadService implements IThreadService { public serviceId = IThreadService; constructor() { - super(true); + super(); this.setInstantiationService(new InstantiationService(new ServiceCollection([IThreadService, this]))); } - protected _doCreateInstance(params: any[]): any { - return super._doCreateInstance(params); - } - - CompatWorker(obj: IThreadSynchronizableObject, methodName: string, target: Function, params: any[]): TPromise { - return TPromise.as(null); - } - protected _registerAndInstantiateMainProcessActor(id: string, descriptor: SyncDescriptor0): T { return this._getOrCreateLocalInstance(id, descriptor); } @@ -42,18 +33,6 @@ export class NullThreadService extends abstractThreadService.AbstractThreadServi protected _registerExtHostActor(id: string, actor: T): void { throw new Error('Not supported in this runtime context!'); } - - protected _registerAndInstantiateWorkerActor(id: string, descriptor: SyncDescriptor0): T { - return this._getOrCreateProxyInstance({ - callOnRemote: (proxyId: string, path: string, args: any[]): TPromise => { - return TPromise.as(null); - } - }, id, descriptor); - } - - protected _registerWorkerActor(id: string, actor: T): void { - throw new Error('Not supported in this runtime context!'); - } } export const NULL_THREAD_SERVICE = new NullThreadService(); \ No newline at end of file diff --git a/src/vs/platform/thread/common/abstractThreadService.ts b/src/vs/platform/thread/common/abstractThreadService.ts index 4e6b083c36f..d73ed0dcab4 100644 --- a/src/vs/platform/thread/common/abstractThreadService.ts +++ b/src/vs/platform/thread/common/abstractThreadService.ts @@ -6,26 +6,18 @@ import {TPromise} from 'vs/base/common/winjs.base'; import remote = require('vs/base/common/remote'); -import {Remotable, IThreadSynchronizableObject} from 'vs/platform/thread/common/thread'; -import {THREAD_SERVICE_PROPERTY_NAME} from 'vs/platform/thread/common/threadService'; +import {Remotable} from 'vs/platform/thread/common/thread'; import instantiation = require('vs/platform/instantiation/common/instantiation'); -import {SyncDescriptor0, createSyncDescriptor, AsyncDescriptor1} from 'vs/platform/instantiation/common/descriptors'; +import {SyncDescriptor0, createSyncDescriptor} from 'vs/platform/instantiation/common/descriptors'; export abstract class AbstractThreadService implements remote.IManyHandler { - public isInMainThread: boolean; - protected _instantiationService: instantiation.IInstantiationService; - protected _boundObjects: { [id: string]: IThreadSynchronizableObject; }; - protected _pendingObjects: TPromise[]; private _localObjMap: { [id: string]: any; }; private _proxyObjMap: { [id: string]: any; }; - constructor(isInMainThread: boolean) { - this.isInMainThread = isInMainThread; - this._boundObjects = {}; - this._pendingObjects = []; + constructor() { this._localObjMap = Object.create(null); this._proxyObjMap = Object.create(null); } @@ -34,48 +26,6 @@ export abstract class AbstractThreadService implements remote.IManyHandler { this._instantiationService = service; } - createInstance(ctor: instantiation.IConstructorSignature1, a1: A1): T; - createInstance(descriptor: AsyncDescriptor1, a1: A1): TPromise; - createInstance(...params: any[]): any { - return this._doCreateInstance(params); - } - - protected _doCreateInstance(params: any[]): any { - let instanceOrPromise = this._instantiationService.createInstance.apply(this._instantiationService, params); - - if (TPromise.is(instanceOrPromise)) { - - let objInstantiated: TPromise; - objInstantiated = instanceOrPromise.then((instance: IThreadSynchronizableObject): any => { - return instance; - }); - - this._pendingObjects.push(objInstantiated); - return objInstantiated.then((instance: IThreadSynchronizableObject) => { - let r = this._finishInstance(instance); - - for (let i = 0; i < this._pendingObjects.length; i++) { - if (this._pendingObjects[i] === objInstantiated) { - this._pendingObjects.splice(i, 1); - break; - } - } - - return r; - }); - - } - - return this._finishInstance(instanceOrPromise); - } - - private _finishInstance(instance: IThreadSynchronizableObject): IThreadSynchronizableObject { - instance[THREAD_SERVICE_PROPERTY_NAME] = this; - this._boundObjects[instance.getId()] = instance; - - return instance; - } - public handle(rpcId: string, methodName: string, args: any[]): any { if (!this._localObjMap[rpcId]) { throw new Error('Unknown actor ' + rpcId); @@ -89,7 +39,6 @@ export abstract class AbstractThreadService implements remote.IManyHandler { } protected _getOrCreateProxyInstance(remoteCom: remote.IProxyHelper, id: string, descriptor: SyncDescriptor0): any { - // console.log(`_getOrCreateProxyInstance: ${id}, ${descriptor}`); if (this._proxyObjMap[id]) { return this._proxyObjMap[id]; } @@ -127,10 +76,6 @@ export abstract class AbstractThreadService implements remote.IManyHandler { return this._registerAndInstantiateExtHostActor(id, desc); } - if (Remotable.Registry.WorkerContext[id]) { - return this._registerAndInstantiateWorkerActor(id, desc); - } - throw new Error('Unknown Remotable: <<' + id + '>>'); } @@ -148,10 +93,6 @@ export abstract class AbstractThreadService implements remote.IManyHandler { return this._registerExtHostActor(id, instance); } - if (Remotable.Registry.WorkerContext[id]) { - return this._registerWorkerActor(id, instance); - } - throw new Error('Unknown Remotable: <<' + id + '>>'); } @@ -159,8 +100,6 @@ export abstract class AbstractThreadService implements remote.IManyHandler { protected abstract _registerMainProcessActor(id: string, actor: T): void; protected abstract _registerAndInstantiateExtHostActor(id: string, descriptor: SyncDescriptor0): T; protected abstract _registerExtHostActor(id: string, actor: T): void; - protected abstract _registerAndInstantiateWorkerActor(id: string, descriptor: SyncDescriptor0): T; - protected abstract _registerWorkerActor(id: string, actor: T): void; } function createProxyFromCtor(remote:remote.IProxyHelper, id:string, ctor:Function): any { diff --git a/src/vs/platform/thread/common/extHostThreadService.ts b/src/vs/platform/thread/common/extHostThreadService.ts index 01908238b33..295a6f4ab1c 100644 --- a/src/vs/platform/thread/common/extHostThreadService.ts +++ b/src/vs/platform/thread/common/extHostThreadService.ts @@ -4,27 +4,22 @@ *--------------------------------------------------------------------------------------------*/ 'use strict'; -import {TPromise} from 'vs/base/common/winjs.base'; import remote = require('vs/base/common/remote'); import descriptors = require('vs/platform/instantiation/common/descriptors'); -import abstractThreadService = require('./abstractThreadService'); -import {IThreadService, IThreadSynchronizableObject} from 'vs/platform/thread/common/thread'; +import {AbstractThreadService} from './abstractThreadService'; +import {IThreadService} from 'vs/platform/thread/common/thread'; -export class ExtHostThreadService extends abstractThreadService.AbstractThreadService implements IThreadService { +export class ExtHostThreadService extends AbstractThreadService implements IThreadService { public serviceId = IThreadService; protected _remoteCom: remote.IRemoteCom; constructor(remoteCom: remote.IRemoteCom) { - super(false); + super(); this._remoteCom = remoteCom; this._remoteCom.setManyHandler(this); } - CompatWorker(obj: IThreadSynchronizableObject, methodName: string, target: Function, params: any[]): TPromise { - return TPromise.as(null); - } - protected _registerAndInstantiateMainProcessActor(id: string, descriptor: descriptors.SyncDescriptor0): T { return this._getOrCreateProxyInstance(this._remoteCom, id, descriptor); } @@ -40,12 +35,4 @@ export class ExtHostThreadService extends abstractThreadService.AbstractThreadSe protected _registerExtHostActor(id: string, actor: T): void { this._registerLocalInstance(id, actor); } - - protected _registerAndInstantiateWorkerActor(id: string, descriptor: descriptors.SyncDescriptor0): T { - throw new Error('Not supported in this runtime context! Cannot communicate directly from Extension Host to Worker!'); - } - - protected _registerWorkerActor(id: string, actor: T): void { - throw new Error('Not supported in this runtime context!'); - } } \ No newline at end of file diff --git a/src/vs/platform/thread/common/mainThreadService.ts b/src/vs/platform/thread/common/mainThreadService.ts index 8561353b597..7cc39c2f9e2 100644 --- a/src/vs/platform/thread/common/mainThreadService.ts +++ b/src/vs/platform/thread/common/mainThreadService.ts @@ -4,25 +4,16 @@ *--------------------------------------------------------------------------------------------*/ 'use strict'; -import {TPromise} from 'vs/base/common/winjs.base'; -import abstractThreadService = require('vs/platform/thread/common/abstractThreadService'); +import {AbstractThreadService} from 'vs/platform/thread/common/abstractThreadService'; import {SyncDescriptor0} from 'vs/platform/instantiation/common/descriptors'; -import {IThreadService, IThreadSynchronizableObject} from 'vs/platform/thread/common/thread'; +import {IThreadService} from 'vs/platform/thread/common/thread'; import {IWorkspaceContextService} from 'vs/platform/workspace/common/workspace'; -export class MainThreadService extends abstractThreadService.AbstractThreadService implements IThreadService { +export abstract class CommonMainThreadService extends AbstractThreadService implements IThreadService { public serviceId = IThreadService; constructor(contextService: IWorkspaceContextService, workerModuleId: string) { - super(true); - - if (!this.isInMainThread) { - throw new Error('Incorrect Service usage: this service must be used only in the main thread'); - } - } - - CompatWorker(obj: IThreadSynchronizableObject, methodName: string, target: Function, params: any[]): TPromise { - throw new Error('Not supported in this runtime context: Cannot communicate to non-existant Worker!'); + super(); } protected _registerAndInstantiateMainProcessActor(id: string, descriptor: SyncDescriptor0): T { @@ -33,19 +24,7 @@ export class MainThreadService extends abstractThreadService.AbstractThreadServi this._registerLocalInstance(id, actor); } - protected _registerAndInstantiateExtHostActor(id: string, descriptor: SyncDescriptor0): T { - throw new Error('Not supported in this runtime context: Cannot communicate to non-existant Extension Host!'); - } - protected _registerExtHostActor(id: string, actor: T): void { throw new Error('Not supported in this runtime context!'); } - - protected _registerAndInstantiateWorkerActor(id: string, descriptor: SyncDescriptor0): T { - throw new Error('Not supported in this runtime context!'); - } - - protected _registerWorkerActor(id: string, actor: T): void { - throw new Error('Not supported in this runtime context!'); - } -} \ No newline at end of file +} diff --git a/src/vs/platform/thread/common/thread.ts b/src/vs/platform/thread/common/thread.ts index a82fdbe4e91..9eebd78f449 100644 --- a/src/vs/platform/thread/common/thread.ts +++ b/src/vs/platform/thread/common/thread.ts @@ -4,29 +4,13 @@ *--------------------------------------------------------------------------------------------*/ 'use strict'; -import {TPromise} from 'vs/base/common/winjs.base'; -import descriptors = require('vs/platform/instantiation/common/descriptors'); -import instantiation = require('vs/platform/instantiation/common/instantiation'); +import {createDecorator, ServiceIdentifier, IConstructorSignature0} from 'vs/platform/instantiation/common/instantiation'; -// --- thread service (web workers) - -export const IThreadService = instantiation.createDecorator('threadService'); +export const IThreadService = createDecorator('threadService'); export interface IThreadService { - serviceId: instantiation.ServiceIdentifier; - - // --- BEGIN deprecated methods - isInMainThread: boolean; - - CompatWorker(obj: IThreadSynchronizableObject, methodName: string, target: Function, param: any[]): TPromise; - - createInstance(ctor: instantiation.IConstructorSignature1, a1: A1): T; - createInstance(descriptor: descriptors.AsyncDescriptor1, a1: A1): TPromise; - - // --- END deprecated methods - - getRemotable(ctor: instantiation.IConstructorSignature0): T; - + serviceId: ServiceIdentifier; + getRemotable(ctor: IConstructorSignature0): T; registerRemotableInstance(ctor: any, instance: any): void; } @@ -40,8 +24,7 @@ export class Remotable { public static Registry = { MainContext: Object.create(null), - ExtHostContext: Object.create(null), - WorkerContext: Object.create(null), + ExtHostContext: Object.create(null) }; public static getId(ctor: any): string { @@ -64,25 +47,9 @@ export class Remotable { }; } - public static WorkerContext(identifier: string) { - return function(target: Function) { - Remotable._ensureUnique(identifier); - Remotable.Registry.WorkerContext[identifier] = target; - target[Remotable.PROP_NAME] = identifier; - }; - } - private static _ensureUnique(identifier: string): void { - if (Remotable.Registry.MainContext[identifier] || Remotable.Registry.ExtHostContext[identifier] || Remotable.Registry.WorkerContext[identifier]) { + if (Remotable.Registry.MainContext[identifier] || Remotable.Registry.ExtHostContext[identifier]) { throw new Error('Duplicate Remotable identifier found'); } } } - -export interface IThreadSynchronizableObject { - getId(): string; - - creationDone?: () => void; - - asyncCtor?: () => TPromise; -} diff --git a/src/vs/platform/thread/common/threadService.ts b/src/vs/platform/thread/common/threadService.ts deleted file mode 100644 index d25b25e0f60..00000000000 --- a/src/vs/platform/thread/common/threadService.ts +++ /dev/null @@ -1,33 +0,0 @@ -/*--------------------------------------------------------------------------------------------- - * Copyright (c) Microsoft Corporation. All rights reserved. - * Licensed under the MIT License. See License.txt in the project root for license information. - *--------------------------------------------------------------------------------------------*/ -'use strict'; - -import {IThreadService} from './thread'; - -export const THREAD_SERVICE_PROPERTY_NAME = '__$$__threadService'; - -function findMember(proto: any, target: any): string { - for (let i in proto) { - if (proto[i] === target) { - return i; - } - } - throw new Error('Member not found in prototype'); -} - -function findThreadService(obj: any): IThreadService { - let threadService: IThreadService = obj[THREAD_SERVICE_PROPERTY_NAME]; - if (!threadService) { - throw new Error('Objects that use thread attributes must be instantiated with the thread service'); - } - return threadService; -} - -export function CompatWorkerAttr(type: Function, target: Function): void { - let methodName = findMember(type.prototype, target); - type.prototype[methodName] = function(...param: any[]) { - return findThreadService(this).CompatWorker(this, methodName, target, param); - }; -} diff --git a/src/vs/platform/thread/common/workerThreadService.ts b/src/vs/platform/thread/common/workerThreadService.ts deleted file mode 100644 index c30195fe7fb..00000000000 --- a/src/vs/platform/thread/common/workerThreadService.ts +++ /dev/null @@ -1,78 +0,0 @@ -/*--------------------------------------------------------------------------------------------- - * Copyright (c) Microsoft Corporation. All rights reserved. - * Licensed under the MIT License. See License.txt in the project root for license information. - *--------------------------------------------------------------------------------------------*/ -'use strict'; - -import {TPromise} from 'vs/base/common/winjs.base'; -import abstractThreadService = require('vs/platform/thread/common/abstractThreadService'); -import remote = require('vs/base/common/remote'); -import {SyncDescriptor0} from 'vs/platform/instantiation/common/descriptors'; -import {IThreadService, IThreadSynchronizableObject} from 'vs/platform/thread/common/thread'; - -export class WorkerThreadService extends abstractThreadService.AbstractThreadService implements IThreadService { - public serviceId = IThreadService; - protected _remoteCom: remote.IRemoteCom; - - constructor(remoteCom: remote.IRemoteCom) { - super(false); - this._remoteCom = remoteCom; - this._remoteCom.setManyHandler(this); - } - - private _handleRequest(identifier: string, memberName: string, args: any[]): TPromise { - if (!this._boundObjects.hasOwnProperty(identifier)) { - // Wait until all objects are constructed - return TPromise.join(this._pendingObjects.slice(0)).then(() => { - if (!this._boundObjects.hasOwnProperty(identifier)) { - return TPromise.wrapError(new Error('Bound object `' + identifier + '` was not found.')); - } - // console.log(identifier + ' > ' + memberName); - let obj = this._boundObjects[identifier]; - return TPromise.as(obj[memberName].apply(obj, args)); - }); - } - // console.log(identifier + ' > ' + memberName); - let obj = this._boundObjects[identifier]; - return TPromise.as(obj[memberName].apply(obj, args)); - } - - public dispatch(data: { type: string; payload: any; }): TPromise { - try { - let args = data.payload; - let result = this._handleRequest(args[0], args[1], args[2]); - return TPromise.is(result) ? result : TPromise.as(result); - } catch (e) { - // handler error - return TPromise.wrapError(e); - } - } - - CompatWorker(obj: IThreadSynchronizableObject, methodName: string, target: Function, params: any[]): TPromise { - return target.apply(obj, params); - } - - protected _registerAndInstantiateMainProcessActor(id: string, descriptor: SyncDescriptor0): T { - throw new Error('Not supported in this runtime context! Not allowed to make requests back to the main thread!'); - } - - protected _registerMainProcessActor(id: string, actor: T): void { - throw new Error('Not supported in this runtime context!'); - } - - protected _registerAndInstantiateExtHostActor(id: string, descriptor: SyncDescriptor0): T { - throw new Error('Not supported in this runtime context: Cannot communicate from Worker directly to Extension Host!'); - } - - protected _registerExtHostActor(id: string, actor: T): void { - throw new Error('Not supported in this runtime context!'); - } - - protected _registerAndInstantiateWorkerActor(id: string, descriptor: SyncDescriptor0): T { - return this._getOrCreateLocalInstance(id, descriptor); - } - - protected _registerWorkerActor(id: string, actor: T): void { - this._registerLocalInstance(id, actor); - } -} \ No newline at end of file diff --git a/src/vs/workbench/browser/workbench.ts b/src/vs/workbench/browser/workbench.ts index f3716179ed9..bd67c00c9fc 100644 --- a/src/vs/workbench/browser/workbench.ts +++ b/src/vs/workbench/browser/workbench.ts @@ -62,7 +62,7 @@ import {ServiceCollection} from 'vs/platform/instantiation/common/serviceCollect import {ILifecycleService} from 'vs/platform/lifecycle/common/lifecycle'; import {IMessageService} from 'vs/platform/message/common/message'; import {IThreadService} from 'vs/platform/thread/common/thread'; -import {MainThreadService} from 'vs/platform/thread/common/mainThreadService'; +import {CommonMainThreadService} from 'vs/platform/thread/common/mainThreadService'; import {IStatusbarService} from 'vs/platform/statusbar/common/statusbar'; import {IMenuService} from 'vs/platform/actions/common/actions'; import {MenuService} from 'vs/platform/actions/browser/menuService'; @@ -392,8 +392,8 @@ export class Workbench implements IPartService { } // Some services need to be set explicitly after all services are created - if (this.threadService instanceof MainThreadService) { - (this.threadService).setInstantiationService(this.instantiationService); + if (this.threadService instanceof CommonMainThreadService) { + (this.threadService).setInstantiationService(this.instantiationService); } (this.keybindingService).setInstantiationService(this.instantiationService); diff --git a/src/vs/workbench/services/thread/electron-browser/threadService.ts b/src/vs/workbench/services/thread/electron-browser/threadService.ts index c7e49623e75..1912124c69f 100644 --- a/src/vs/workbench/services/thread/electron-browser/threadService.ts +++ b/src/vs/workbench/services/thread/electron-browser/threadService.ts @@ -17,7 +17,7 @@ import {findFreePort} from 'vs/base/node/ports'; import {IMainProcessExtHostIPC, create} from 'vs/platform/extensions/common/ipcRemoteCom'; import {SyncDescriptor0} from 'vs/platform/instantiation/common/descriptors'; import {IMessageService, Severity} from 'vs/platform/message/common/message'; -import {MainThreadService as CommonMainThreadService} from 'vs/platform/thread/common/mainThreadService'; +import {CommonMainThreadService} from 'vs/platform/thread/common/mainThreadService'; import {ILifecycleService, ShutdownEvent} from 'vs/platform/lifecycle/common/lifecycle'; import {IConfiguration, IWorkspaceContextService} from 'vs/platform/workspace/common/workspace'; import {IWindowService} from 'vs/workbench/services/window/electron-browser/windowService'; -- GitLab