-
Notifications
You must be signed in to change notification settings - Fork 29.2k
/
ipc.mp.ts
88 lines (70 loc) · 2.94 KB
/
ipc.mp.ts
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { MessagePortMain, isUtilityProcess, MessageEvent } from 'vs/base/parts/sandbox/node/electronTypes';
import { VSBuffer } from 'vs/base/common/buffer';
import { ClientConnectionEvent, IMessagePassingProtocol, IPCServer } from 'vs/base/parts/ipc/common/ipc';
import { Emitter, Event } from 'vs/base/common/event';
import { assertType } from 'vs/base/common/types';
import { firstOrDefault } from 'vs/base/common/arrays';
/**
* The MessagePort `Protocol` leverages MessagePortMain style IPC communication
* for the implementation of the `IMessagePassingProtocol`.
*/
class Protocol implements IMessagePassingProtocol {
readonly onMessage = Event.fromNodeEventEmitter<VSBuffer>(this.port, 'message', (e: MessageEvent) => VSBuffer.wrap(e.data));
constructor(private port: MessagePortMain) {
// we must call start() to ensure messages are flowing
port.start();
}
send(message: VSBuffer): void {
this.port.postMessage(message.buffer);
}
disconnect(): void {
this.port.close();
}
}
/**
* An implementation of a `IPCServer` on top of MessagePort style IPC communication.
* The clients register themselves via Electron Utility Process IPC transfer.
*/
export class Server extends IPCServer {
private static getOnDidClientConnect(): Event<ClientConnectionEvent> {
assertType(isUtilityProcess(process), 'Electron Utility Process');
const onCreateMessageChannel = new Emitter<MessagePortMain>();
process.parentPort.on('message', (e: Electron.MessageEvent) => {
const port = firstOrDefault(e.ports);
if (port) {
onCreateMessageChannel.fire(port);
}
});
return Event.map(onCreateMessageChannel.event, port => {
const protocol = new Protocol(port);
const result: ClientConnectionEvent = {
protocol,
// Not part of the standard spec, but in Electron we get a `close` event
// when the other side closes. We can use this to detect disconnects
// (https://github.com/electron/electron/blob/11-x-y/docs/api/message-port-main.md#event-close)
onDidClientDisconnect: Event.fromNodeEventEmitter(port, 'close')
};
return result;
});
}
constructor() {
super(Server.getOnDidClientConnect());
}
}
interface INodeMessagePortFragment {
on(event: 'message', listener: (messageEvent: MessageEvent) => void): this;
removeListener(event: 'message', listener: (messageEvent: MessageEvent) => void): this;
}
export function once(port: INodeMessagePortFragment, message: unknown, callback: () => void): void {
const listener = (e: MessageEvent) => {
if (e.data === message) {
port.removeListener('message', listener);
callback();
}
};
port.on('message', listener);
}