abstractThreadService.ts 9.0 KB
Newer Older
E
Erich Gamma 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
/*---------------------------------------------------------------------------------------------
 *  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 winjs = require('vs/base/common/winjs.base');
import Browser = require('vs/base/browser/browser');
import remote = require('vs/base/common/remote');
import Types = require('vs/base/common/types');
import {IThreadServiceStatusListener, ThreadAffinity, Remotable, IRemotableCtorMap, IThreadSynchronizableObject, IDynamicProxy} from 'vs/platform/thread/common/thread';
import {THREAD_SERVICE_PROPERTY_NAME} from 'vs/platform/thread/common/threadService';
import instantiation = require('vs/platform/instantiation/common/instantiation');
import {SyncDescriptor, SyncDescriptor0, createSyncDescriptor, AsyncDescriptor0, AsyncDescriptor1, AsyncDescriptor2, AsyncDescriptor3} from 'vs/platform/instantiation/common/descriptors';

export interface IThreadServiceData {
	[id:string]:any;
}

class DynamicProxy<T> implements IDynamicProxy<T> {

	private _proxyDefinition: T;
	private _disposeDelegate: ()=>void;

	constructor(proxyDefinition:T, disposeDelegate:()=>void) {
		this._proxyDefinition = proxyDefinition;
		this._disposeDelegate = disposeDelegate;
	}

	public dispose(): void {
		return this._disposeDelegate();
	}

	public getProxyDefinition(): T {
		return this._proxyDefinition;
	}
}

export abstract class AbstractThreadService implements remote.IManyHandler {

	private static _LAST_DYNAMIC_PROXY_ID:number = 0;
	private static generateDynamicProxyId(): string {
		return String(++this._LAST_DYNAMIC_PROXY_ID);
	}

	public isInMainThread:boolean;

J
Johannes Rieken 已提交
48 49
	protected _instantiationService: instantiation.IInstantiationService;
	
E
Erich Gamma 已提交
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
	_boundObjects:{[id:string]:IThreadSynchronizableObject<any>;};
	_pendingObjects:winjs.Promise[];
	private _localObjMap: { [id:string]: any; };
	private _proxyObjMap: { [id:string]: any; };

	constructor(isInMainThread: boolean) {
		this.isInMainThread = isInMainThread;
		this._boundObjects = {};
		this._pendingObjects = [];
		this._localObjMap = Object.create(null);
		this._proxyObjMap = Object.create(null);
	}

	setInstantiationService(service:instantiation.IInstantiationService): void {
		this._instantiationService = service;
	}

	createInstance<T extends IThreadSynchronizableObject<any>>(ctor:instantiation.IConstructorSignature0<T>):T;
	createInstance<A1, T extends IThreadSynchronizableObject<any>>(ctor:instantiation.IConstructorSignature1<A1, T>, a1:A1):T;
	createInstance<A1, A2, T extends IThreadSynchronizableObject<any>>(ctor:instantiation.IConstructorSignature2<A1, A2, T>, a1:A1, a2:A2):T;
	createInstance<A1, A2, A3, T extends IThreadSynchronizableObject<any>>(ctor:instantiation.IConstructorSignature3<A1, A2, A3, T>, a1:A1, a2:A2, a3:A3):T;

	createInstance<T extends IThreadSynchronizableObject<any>>(descriptor: AsyncDescriptor0<T>): T;
	createInstance<A1, T extends IThreadSynchronizableObject<any>>(descriptor: AsyncDescriptor1<A1, T>, a1: A1): T;
	createInstance<A1, A2, T extends IThreadSynchronizableObject<any>>(descriptor: AsyncDescriptor2<A1, A2, T>, a1: A1, a2: A2): T;
	createInstance<A1, A2, A3, T extends IThreadSynchronizableObject<any>>(descriptor: AsyncDescriptor3<A1, A2, A3, T>, a1: A1, a2: A2, a3: A3): T;

	createInstance(...params:any[]):any {
		return this._doCreateInstance(params);
	}

	protected _doCreateInstance(params:any[]): any {
		var instanceOrPromise = this._instantiationService.createInstance.apply(this._instantiationService, params);

		if (winjs.Promise.is(instanceOrPromise)) {

			var objInstantiated = instanceOrPromise.then((instance: IThreadSynchronizableObject<any>): any => {
				if (instance.asyncCtor) {
					var initPromise = instance.asyncCtor();
					if (winjs.Promise.is(initPromise)) {
						return initPromise.then(() => {
							return instance;
						});
					}
				}
				return instance;
			});

			this._pendingObjects.push(objInstantiated);
			return objInstantiated.then((instance: IThreadSynchronizableObject<any>) => {
				var r = this._finishInstance(instance);

				for (var i = 0; i < this._pendingObjects.length; i++) {
					if (this._pendingObjects[i] === objInstantiated) {
						this._pendingObjects.splice(i, 1);
						break;
					}
				}

				return r;
			});

		}

		return this._finishInstance(<IThreadSynchronizableObject<any>>instanceOrPromise);
	}

	_finishInstance(instance:IThreadSynchronizableObject<any>): IThreadSynchronizableObject<any> {
		instance[THREAD_SERVICE_PROPERTY_NAME] = this;
		this._boundObjects[instance.getId()] = instance;

		if (instance.creationDone) {
			instance.creationDone();
		}

		return instance;
	}

	registerInstance<T extends IThreadSynchronizableObject<any>>(instance: T): void {
		this._finishInstance(instance);
	}

	public handle(rpcId:string, method:string, args:any[]): any {
		if (!this._localObjMap[rpcId]) {
			throw new Error('Unknown actor ' + rpcId);
		}
		var actor = this._localObjMap[rpcId];
		return actor[method].apply(actor, args);
	}

	protected _getOrCreateProxyInstance(remoteCom: remote.IProxyHelper, id: string, descriptor: SyncDescriptor0<any>): any {
		if (this._proxyObjMap[id]) {
			return this._proxyObjMap[id];
		}
		var result = remote.createProxyFromCtor(remoteCom, id, descriptor.ctor);
		this._proxyObjMap[id] = result;
		return result;
	}

	protected _registerLocalInstance(id:string, obj:any): any {
		this._localObjMap[id] = obj;
	}

	protected _getOrCreateLocalInstance(id: string, descriptor: SyncDescriptor0<any>): any {
		if (this._localObjMap[id]) {
			return this._localObjMap[id];
		}
		var result = this._instantiationService.createInstance(descriptor);
		this._registerLocalInstance(id, result);
		return result;
	}

	public createDynamicProxyFromMethods<T>(obj:T): IDynamicProxy<T> {
		let id = AbstractThreadService.generateDynamicProxyId();
		let proxyDefinition = this._proxifyMethods(id, obj);
		return new DynamicProxy(proxyDefinition, () => {
			delete this._localObjMap[id];
		});
	}

	public createDynamicProxyFromMembers<T>(obj:T, allowedMembers:string[]): IDynamicProxy<T> {
		let id = AbstractThreadService.generateDynamicProxyId();
		let proxyDefinition = this._proxifyMembers(id, obj, allowedMembers);
		return new DynamicProxy(proxyDefinition, () => {
			delete this._localObjMap[id];
		});
	}

	private _proxifyMethods<T>(uniqueIdentifier: string, obj:T): T {
		if (!Types.isObject(obj)) {
			return null;
		}
		this._localObjMap[uniqueIdentifier] = obj;
		var r: any = {
			$__CREATE__PROXY__REQUEST: uniqueIdentifier
		};
		for (var prop in obj) {
			if (typeof obj[prop] === 'function') {
				r[prop] = obj[prop].bind(obj);
			}
		}
		return r;
	}

	private _proxifyMembers<T>(uniqueIdentifier: string, obj:T, allowedMembers:string[]): T {
		if (!Types.isObject(obj)) {
			return null;
		}
		this._localObjMap[uniqueIdentifier] = obj;
		var r: any = {
			$__CREATE__PROXY__REQUEST: uniqueIdentifier
		};
		for (var prop in obj) {
			if (allowedMembers.indexOf(prop) === -1) {
				continue;
			}
			if (typeof obj[prop] === 'function') {
				r[prop] = obj[prop].bind(obj);
			} else {
				r[prop] = obj[prop];
			}
		}
		return r;
	}

	public isProxyObject<T>(obj: T): boolean {
		return obj && !!((<any>obj).$__IS_REMOTE_OBJ);
	}

	getRemotable<T>(ctor: instantiation.INewConstructorSignature0<T>): T {
		var id = Remotable.getId(ctor);
		if (!id) {
			throw new Error('Unknown Remotable: <<' + id + '>>');
		}

		var desc = createSyncDescriptor<T>(ctor);

		if (Remotable.Registry.MainContext[id]) {
			return this._registerAndInstantiateMainProcessActor(id, desc);
		}

		if (Remotable.Registry.PluginHostContext[id]) {
			return this._registerAndInstantiatePluginHostActor(id, desc);
		}

		if (Remotable.Registry.WorkerContext[id]) {
			return this._registerAndInstantiateWorkerActor(id, desc, Remotable.Registry.WorkerContext[id].affinity);
		}

		throw new Error('Unknown Remotable: <<' + id + '>>');
	}

	registerRemotableInstance(ctor: any, instance: any): void {
		var id = Remotable.getId(ctor);
		if (!id) {
			throw new Error('Unknown Remotable: <<' + id + '>>');
		}

		if (Remotable.Registry.MainContext[id]) {
			return this._registerMainProcessActor(id, instance);
		}

		if (Remotable.Registry.PluginHostContext[id]) {
			return this._registerPluginHostActor(id, instance);
		}

		if (Remotable.Registry.WorkerContext[id]) {
			return this._registerWorkerActor(id, instance);
		}

		throw new Error('Unknown Remotable: <<' + id + '>>');
	}

	protected abstract _registerAndInstantiateMainProcessActor<T>(id: string, descriptor: SyncDescriptor0<T>): T;
	protected abstract _registerMainProcessActor<T>(id: string, actor:T): void;
	protected abstract _registerAndInstantiatePluginHostActor<T>(id: string, descriptor: SyncDescriptor0<T>): T;
	protected abstract _registerPluginHostActor<T>(id: string, actor:T): void;
	protected abstract _registerAndInstantiateWorkerActor<T>(id: string, descriptor: SyncDescriptor0<T>, whichWorker:ThreadAffinity): T;
	protected abstract _registerWorkerActor<T>(id: string, actor:T): void;
}