Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/base/parts/ipc/electron-browser/ipc.mp.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 { mainWindow } from '../../../browser/window.js';
7
import { Event } from '../../../common/event.js';
8
import { generateUuid } from '../../../common/uuid.js';
9
import { ipcMessagePort, ipcRenderer } from '../../sandbox/electron-browser/globals.js';
10
11
interface IMessageChannelResult {
12
nonce: string;
13
port: MessagePort;
14
source: unknown;
15
}
16
17
export async function acquirePort(requestChannel: string | undefined, responseChannel: string, nonce = generateUuid()): Promise<MessagePort> {
18
19
// Get ready to acquire the message port from the
20
// provided `responseChannel` via preload helper.
21
ipcMessagePort.acquire(responseChannel, nonce);
22
23
// If a `requestChannel` is provided, we are in charge
24
// to trigger acquisition of the message port from main
25
if (typeof requestChannel === 'string') {
26
ipcRenderer.send(requestChannel, nonce);
27
}
28
29
// Wait until the main side has returned the `MessagePort`
30
// We need to filter by the `nonce` to ensure we listen
31
// to the right response.
32
const onMessageChannelResult = Event.fromDOMEventEmitter<IMessageChannelResult>(mainWindow, 'message', (e: MessageEvent) => ({ nonce: e.data, port: e.ports[0], source: e.source }));
33
const { port } = await Event.toPromise(Event.once(Event.filter(onMessageChannelResult, e => e.nonce === nonce && e.source === mainWindow)));
34
35
return port;
36
}
37
38