/*--------------------------------------------------------------------------------------------- * Copyright (c) Microsoft Corporation. All rights reserved. * Licensed under the Source EULA. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ import { IChannel, IServerChannel } from 'vs/base/parts/ipc/node/ipc'; import { Event, Emitter } from 'vs/base/common/event'; import { timeout } from 'vs/base/common/async'; export interface IMarcoPoloEvent { answer: string; } export interface ITestService { onMarco: Event; marco(): Promise; pong(ping: string): Promise<{ incoming: string, outgoing: string }>; cancelMe(): Promise; } export class TestService implements ITestService { private _onMarco = new Emitter(); onMarco: Event = this._onMarco.event; marco(): Promise { this._onMarco.fire({ answer: 'polo' }); return Promise.resolve('polo'); } pong(ping: string): Promise<{ incoming: string, outgoing: string }> { return Promise.resolve({ incoming: ping, outgoing: 'pong' }); } cancelMe(): Promise { return Promise.resolve(timeout(100)).then(() => true); } } export class TestChannel implements IServerChannel { constructor(private testService: ITestService) { } listen(_, event: string): Event { switch (event) { case 'marco': return this.testService.onMarco; } throw new Error('Event not found'); } call(_, command: string, ...args: any[]): Promise { switch (command) { case 'pong': return this.testService.pong(args[0]); case 'cancelMe': return this.testService.cancelMe(); case 'marco': return this.testService.marco(); default: return Promise.reject(new Error(`command not found: ${command}`)); } } } export class TestServiceClient implements ITestService { get onMarco(): Event { return this.channel.listen('marco'); } constructor(private channel: IChannel) { } marco(): Promise { return this.channel.call('marco'); } pong(ping: string): Promise<{ incoming: string, outgoing: string }> { return this.channel.call('pong', ping); } cancelMe(): Promise { return this.channel.call('cancelMe'); } }