Path: blob/main/src/vs/base/parts/ipc/test/node/testService.ts
4780 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import { timeout } from '../../../../common/async.js';6import { Emitter, Event } from '../../../../common/event.js';7import { IChannel, IServerChannel } from '../../common/ipc.js';89export interface IMarcoPoloEvent {10answer: string;11}1213export interface ITestService {14readonly onMarco: Event<IMarcoPoloEvent>;15marco(): Promise<string>;16pong(ping: string): Promise<{ incoming: string; outgoing: string }>;17cancelMe(): Promise<boolean>;18}1920export class TestService implements ITestService {2122private readonly _onMarco = new Emitter<IMarcoPoloEvent>();23readonly onMarco: Event<IMarcoPoloEvent> = this._onMarco.event;2425marco(): Promise<string> {26this._onMarco.fire({ answer: 'polo' });27return Promise.resolve('polo');28}2930pong(ping: string): Promise<{ incoming: string; outgoing: string }> {31return Promise.resolve({ incoming: ping, outgoing: 'pong' });32}3334cancelMe(): Promise<boolean> {35return Promise.resolve(timeout(100)).then(() => true);36}37}3839export class TestChannel implements IServerChannel {4041constructor(private testService: ITestService) { }4243listen(_: unknown, event: string): Event<any> {44switch (event) {45case 'marco': return this.testService.onMarco;46}4748throw new Error('Event not found');49}5051call(_: unknown, command: string, ...args: any[]): Promise<any> {52switch (command) {53case 'pong': return this.testService.pong(args[0]);54case 'cancelMe': return this.testService.cancelMe();55case 'marco': return this.testService.marco();56default: return Promise.reject(new Error(`command not found: ${command}`));57}58}59}6061export class TestServiceClient implements ITestService {6263get onMarco(): Event<IMarcoPoloEvent> { return this.channel.listen('marco'); }6465constructor(private channel: IChannel) { }6667marco(): Promise<string> {68return this.channel.call('marco');69}7071pong(ping: string): Promise<{ incoming: string; outgoing: string }> {72return this.channel.call('pong', ping);73}7475cancelMe(): Promise<boolean> {76return this.channel.call('cancelMe');77}78}798081