/*--------------------------------------------------------------------------------------------- * Copyright (c) Microsoft Corporation. All rights reserved. * Licensed under the Source EULA. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ 'use strict'; import { TPromise, PPromise } from 'vs/base/common/winjs.base'; import { IChannel, eventToCall, eventFromCall } from 'vs/base/parts/ipc/common/ipc'; import { Event, Emitter } from 'vs/base/common/event'; export interface IMarcoPoloEvent { answer: string; } export interface ITestService { onMarco: Event; marco(): TPromise; pong(ping: string): TPromise<{ incoming: string, outgoing: string }>; cancelMe(): TPromise; batchPerf(batches: number, size: number, dataSize: number): PPromise; } export class TestService implements ITestService { private _onMarco = new Emitter(); onMarco: Event = this._onMarco.event; private _data = 'abcdefghijklmnopqrstuvwxyz'; marco(): TPromise { this._onMarco.fire({ answer: 'polo' }); return TPromise.as('polo'); } pong(ping: string): TPromise<{ incoming: string, outgoing: string }> { return TPromise.as({ incoming: ping, outgoing: 'pong' }); } cancelMe(): TPromise { return TPromise.timeout(100).then(() => true); } batchPerf(batches: number, size: number, dataSize: number): PPromise { while (this._data.length < dataSize) { this._data += this._data; } const self = this; return new PPromise((complete, error, progress) => { let j = 0; function send() { if (j >= batches) { complete(null); return; } j++; const batch = []; for (let i = 0; i < size; i++) { batch.push({ prop: `${i}${self._data}`.substr(0, dataSize) }); } progress(batch); process.nextTick(send); } process.nextTick(send); }); } } export interface ITestChannel extends IChannel { call(command: 'marco'): TPromise; call(command: 'pong', ping: string): TPromise; call(command: 'cancelMe'): TPromise; call(command: 'batchPerf', args: { batches: number; size: number; dataSize: number; }): PPromise; call(command: string, ...args: any[]): TPromise; } export class TestChannel implements ITestChannel { constructor(private testService: ITestService) { } call(command: string, ...args: any[]): TPromise { switch (command) { case 'pong': return this.testService.pong(args[0]); case 'cancelMe': return this.testService.cancelMe(); case 'marco': return this.testService.marco(); case 'event:marco': return eventToCall(this.testService.onMarco); case 'batchPerf': return this.testService.batchPerf(args[0].batches, args[0].size, args[0].dataSize); default: return TPromise.wrapError(new Error('command not found')); } } } export class TestServiceClient implements ITestService { private _onMarco: Event; get onMarco(): Event { return this._onMarco; } constructor(private channel: ITestChannel) { this._onMarco = eventFromCall(channel, 'event:marco'); } marco(): TPromise { return this.channel.call('marco'); } pong(ping: string): TPromise<{ incoming: string, outgoing: string }> { return this.channel.call('pong', ping); } cancelMe(): TPromise { return this.channel.call('cancelMe'); } batchPerf(batches: number, size: number, dataSize: number): PPromise { return this.channel.call('batchPerf', { batches, size, dataSize }); } }