testThreadService.ts 2.4 KB
Newer Older
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
/*---------------------------------------------------------------------------------------------
 *  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';

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 已提交
47 48
		let _calls:{path: string; args: any[] }[] = [];
		let _instance: any;
49 50

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


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

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

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