testThreadService.ts 2.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
/*---------------------------------------------------------------------------------------------
 *  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 {NullThreadService} from 'vs/platform/test/common/nullThreadService';
import {create} from 'vs/base/common/types';
import {SyncDescriptor0} from 'vs/platform/instantiation/common/descriptors';
import {TPromise} from 'vs/base/common/winjs.base';
J
Johannes Rieken 已提交
12
import {IInstantiationService} from 'vs/platform/instantiation/common/instantiation';
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

export class TestThreadService extends NullThreadService {

	private _callCountValue: number = 0;
	private _idle: TPromise<any>;
	private _completeIdle: Function;

	private get _callCount(): number {
		return this._callCountValue;
	}

	private set _callCount(value:number) {
		this._callCountValue = value;
		if (this._callCountValue === 0) {
			this._completeIdle();
			this._idle = undefined;
		}
	}

	sync(): TPromise<any> {
		if (this._callCount === 0) {
			return TPromise.as(undefined);
		}
		if (!this._idle) {
			this._idle = new TPromise<any>((c, e) => {
				this._completeIdle = c;
			}, function() {
				// no cancel
			});
		}
		return this._idle;
	}

	protected _registerAndInstantiateMainProcessActor<T>(id: string, descriptor: SyncDescriptor0<T>): T {

J
Johannes Rieken 已提交
48 49
		let _calls:{path: string; args: any[] }[] = [];
		let _instance: any;
50 51

		return this._getOrCreateProxyInstance({
J
Johannes Rieken 已提交
52 53


54 55 56
			callOnRemote: (proxyId: string, path: string, args: any[]): TPromise<any> => {

				this._callCount++;
J
Johannes Rieken 已提交
57
				_calls.push({path, args});
58 59

				return TPromise.timeout(0).then(() => {
J
Johannes Rieken 已提交
60
					if (!_instance) {
J
Johannes Rieken 已提交
61
						_instance = this._instantiationService.createInstance(descriptor.ctor);
62 63 64
					}
					let p: TPromise<any>;
					try {
J
Johannes Rieken 已提交
65 66
						let {path, args} = _calls.shift();
						let result = (<Function>_instance[path]).apply(_instance, args);
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
						p = TPromise.is(result) ? result : TPromise.as(result);
					} catch (err) {
						p = TPromise.wrapError(err);
					}

					return p.then(result => {
						this._callCount--;
						return result;
					}, err => {
						this._callCount--;
						return TPromise.wrapError(err);
					});
				});
			}
		}, id, descriptor)
	}

	protected _registerAndInstantiatePluginHostActor<T>(id: string, descriptor: SyncDescriptor0<T>): T {
		return this._getOrCreateLocalInstance(id, descriptor);
	}
}

const Instance = new TestThreadService();
export default Instance;