thread.ts 3.4 KB
Newer Older
E
Erich Gamma 已提交
1 2 3 4
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/
A
tslint  
Alex Dima 已提交
5
'use strict';
E
Erich Gamma 已提交
6

A
Alex Dima 已提交
7
import {TPromise} from 'vs/base/common/winjs.base';
E
Erich Gamma 已提交
8 9 10 11 12
import descriptors = require('vs/platform/instantiation/common/descriptors');
import instantiation = require('vs/platform/instantiation/common/instantiation');

// --- thread service (web workers)

B
Benjamin Pasero 已提交
13
export const IThreadService = instantiation.createDecorator<IThreadService>('threadService');
E
Erich Gamma 已提交
14 15

export interface IThreadService {
B
Benjamin Pasero 已提交
16
	serviceId: instantiation.ServiceIdentifier<any>;
E
Erich Gamma 已提交
17 18 19 20

	// --- BEGIN deprecated methods
	isInMainThread: boolean;

21 22
	OneWorker(obj: IThreadSynchronizableObject, methodName: string, target: Function, param: any[], affinity: ThreadAffinity): TPromise<any>;
	AllWorkers(obj: IThreadSynchronizableObject, methodName: string, target: Function, param: any[]): TPromise<any>;
E
Erich Gamma 已提交
23

24
	createInstance<A1, T extends IThreadSynchronizableObject>(ctor: instantiation.IConstructorSignature1<A1, T>, a1: A1): T;
A
Alex Dima 已提交
25
	createInstance<A1, T extends IThreadSynchronizableObject>(descriptor: descriptors.AsyncDescriptor1<A1, T>, a1: A1): TPromise<T>;
E
Erich Gamma 已提交
26 27 28

	// --- END deprecated methods

29
	getRemotable<T>(ctor: instantiation.IConstructorSignature0<T>): T;
E
Erich Gamma 已提交
30 31 32 33 34

	registerRemotableInstance(ctor: any, instance: any): void;
}

export class IRemotableCtorMap {
B
Benjamin Pasero 已提交
35
	[identifier: string]: Function;
E
Erich Gamma 已提交
36 37 38
}

export class IRemotableCtorAffinityMap {
B
Benjamin Pasero 已提交
39
	[identifier: string]: {
E
Erich Gamma 已提交
40 41 42 43 44 45 46 47 48 49 50
		ctor: Function;
		affinity: ThreadAffinity;
	};
}

export class Remotable {

	private static PROP_NAME = '$__REMOTABLE_ID';

	public static Registry = {
		MainContext: <IRemotableCtorMap>Object.create(null),
A
Alex Dima 已提交
51
		ExtHostContext: <IRemotableCtorMap>Object.create(null),
E
Erich Gamma 已提交
52 53 54 55 56 57 58 59
		WorkerContext: <IRemotableCtorAffinityMap>Object.create(null),
	};

	public static getId(ctor: any): string {
		return (ctor[Remotable.PROP_NAME] || null);
	}

	public static MainContext(identifier: string) {
B
Benjamin Pasero 已提交
60
		return function(target: Function) {
E
Erich Gamma 已提交
61 62 63
			Remotable._ensureUnique(identifier);
			Remotable.Registry.MainContext[identifier] = target;
			target[Remotable.PROP_NAME] = identifier;
A
tslint  
Alex Dima 已提交
64
		};
E
Erich Gamma 已提交
65 66
	}

A
Alex Dima 已提交
67
	public static ExtHostContext(identifier: string) {
B
Benjamin Pasero 已提交
68
		return function(target: Function) {
E
Erich Gamma 已提交
69
			Remotable._ensureUnique(identifier);
A
Alex Dima 已提交
70
			Remotable.Registry.ExtHostContext[identifier] = target;
E
Erich Gamma 已提交
71
			target[Remotable.PROP_NAME] = identifier;
A
tslint  
Alex Dima 已提交
72
		};
E
Erich Gamma 已提交
73 74
	}

B
Benjamin Pasero 已提交
75 76
	public static WorkerContext(identifier: string, whichWorker: ThreadAffinity) {
		return function(target: Function) {
E
Erich Gamma 已提交
77 78 79 80 81 82
			Remotable._ensureUnique(identifier);
			Remotable.Registry.WorkerContext[identifier] = {
				ctor: target,
				affinity: whichWorker
			};
			target[Remotable.PROP_NAME] = identifier;
A
tslint  
Alex Dima 已提交
83
		};
E
Erich Gamma 已提交
84 85
	}

B
Benjamin Pasero 已提交
86
	private static _ensureUnique(identifier: string): void {
A
Alex Dima 已提交
87
		if (Remotable.Registry.MainContext[identifier] || Remotable.Registry.ExtHostContext[identifier] || Remotable.Registry.WorkerContext[identifier]) {
E
Erich Gamma 已提交
88 89 90 91 92
			throw new Error('Duplicate Remotable identifier found');
		}
	}
}

93
export interface IThreadSynchronizableObject {
B
Benjamin Pasero 已提交
94
	getId(): string;
E
Erich Gamma 已提交
95

B
Benjamin Pasero 已提交
96
	creationDone?: () => void;
E
Erich Gamma 已提交
97

B
Benjamin Pasero 已提交
98
	asyncCtor?: () => TPromise<void>;
E
Erich Gamma 已提交
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
}

export enum ThreadAffinity {
	None = 0,
	Group1 = 1,
	Group2 = 2,
	Group3 = 3,
	Group4 = 4,
	Group5 = 5,
	Group6 = 6,
	Group7 = 7,
	Group8 = 8,
	Group9 = 9,
	All = 10
}