Path: blob/main/src/vs/base/parts/ipc/electron-browser/ipc.mp.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 { mainWindow } from '../../../browser/window.js';6import { Event } from '../../../common/event.js';7import { generateUuid } from '../../../common/uuid.js';8import { ipcMessagePort, ipcRenderer } from '../../sandbox/electron-browser/globals.js';910interface IMessageChannelResult {11nonce: string;12port: MessagePort;13source: unknown;14}1516export async function acquirePort(requestChannel: string | undefined, responseChannel: string, nonce = generateUuid()): Promise<MessagePort> {1718// Get ready to acquire the message port from the19// provided `responseChannel` via preload helper.20ipcMessagePort.acquire(responseChannel, nonce);2122// If a `requestChannel` is provided, we are in charge23// to trigger acquisition of the message port from main24if (typeof requestChannel === 'string') {25ipcRenderer.send(requestChannel, nonce);26}2728// Wait until the main side has returned the `MessagePort`29// We need to filter by the `nonce` to ensure we listen30// to the right response.31const onMessageChannelResult = Event.fromDOMEventEmitter<IMessageChannelResult>(mainWindow, 'message', (e: MessageEvent) => ({ nonce: e.data, port: e.ports[0], source: e.source }));32const { port } = await Event.toPromise(Event.once(Event.filter(onMessageChannelResult, e => e.nonce === nonce && e.source === mainWindow)));3334return port;35}363738