Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/base/parts/ipc/test/node/testService.ts
4780 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import { timeout } from '../../../../common/async.js';
7
import { Emitter, Event } from '../../../../common/event.js';
8
import { IChannel, IServerChannel } from '../../common/ipc.js';
9
10
export interface IMarcoPoloEvent {
11
answer: string;
12
}
13
14
export interface ITestService {
15
readonly onMarco: Event<IMarcoPoloEvent>;
16
marco(): Promise<string>;
17
pong(ping: string): Promise<{ incoming: string; outgoing: string }>;
18
cancelMe(): Promise<boolean>;
19
}
20
21
export class TestService implements ITestService {
22
23
private readonly _onMarco = new Emitter<IMarcoPoloEvent>();
24
readonly onMarco: Event<IMarcoPoloEvent> = this._onMarco.event;
25
26
marco(): Promise<string> {
27
this._onMarco.fire({ answer: 'polo' });
28
return Promise.resolve('polo');
29
}
30
31
pong(ping: string): Promise<{ incoming: string; outgoing: string }> {
32
return Promise.resolve({ incoming: ping, outgoing: 'pong' });
33
}
34
35
cancelMe(): Promise<boolean> {
36
return Promise.resolve(timeout(100)).then(() => true);
37
}
38
}
39
40
export class TestChannel implements IServerChannel {
41
42
constructor(private testService: ITestService) { }
43
44
listen(_: unknown, event: string): Event<any> {
45
switch (event) {
46
case 'marco': return this.testService.onMarco;
47
}
48
49
throw new Error('Event not found');
50
}
51
52
call(_: unknown, command: string, ...args: any[]): Promise<any> {
53
switch (command) {
54
case 'pong': return this.testService.pong(args[0]);
55
case 'cancelMe': return this.testService.cancelMe();
56
case 'marco': return this.testService.marco();
57
default: return Promise.reject(new Error(`command not found: ${command}`));
58
}
59
}
60
}
61
62
export class TestServiceClient implements ITestService {
63
64
get onMarco(): Event<IMarcoPoloEvent> { return this.channel.listen('marco'); }
65
66
constructor(private channel: IChannel) { }
67
68
marco(): Promise<string> {
69
return this.channel.call('marco');
70
}
71
72
pong(ping: string): Promise<{ incoming: string; outgoing: string }> {
73
return this.channel.call('pong', ping);
74
}
75
76
cancelMe(): Promise<boolean> {
77
return this.channel.call('cancelMe');
78
}
79
}
80
81