From 25535c0aab0a8916d6a51d771d6123c09e692127 Mon Sep 17 00:00:00 2001 From: Gregor MacLennan Date: Wed, 11 Dec 2024 22:41:07 +0000 Subject: [PATCH 1/3] chore(WIP): synchronous init of comapeo client --- src/main/app.js | 9 ++--- src/preload/main-window.js | 24 +++++------ src/preload/runtime.d.ts | 1 - src/renderer/src/comapeo-client.ts | 9 +++++ src/renderer/src/contexts/ApiContext.tsx | 51 ++++-------------------- src/renderer/src/routes/__root.tsx | 4 +- 6 files changed, 32 insertions(+), 66 deletions(-) create mode 100644 src/renderer/src/comapeo-client.ts diff --git a/src/main/app.js b/src/main/app.js index e5ef2af..892ba01 100644 --- a/src/main/app.js +++ b/src/main/app.js @@ -4,7 +4,6 @@ import { defineMessages } from '@formatjs/intl' import debug from 'debug' import { BrowserWindow, - MessageChannelMain, app, ipcMain, safeStorage, @@ -213,17 +212,17 @@ function initMainWindow({ appMode, services }) { // Set up communication channel between window and core service // https://www.electronjs.org/docs/latest/tutorial/message-ports/#messageports-in-the-main-process - mainWindow.webContents.ipc.on('request-comapeo-port', (event) => { - const { port1, port2 } = new MessageChannelMain() + mainWindow.webContents.ipc.on('comapeo-port', (event) => { + const [port] = event.ports + if (!port) return // TODO: throw/report error services.core.postMessage( /** @satisfies {NewClientMessage} */ { type: 'core:new-client', payload: { clientId: `window-${mainWindow.id}` }, }, - [port1], + [port], ) - event.senderFrame?.postMessage('provide-comapeo-port', null, [port2]) }) APP_STATE.browserWindows.set(mainWindow, { diff --git a/src/preload/main-window.js b/src/preload/main-window.js index b282182..eb41855 100644 --- a/src/preload/main-window.js +++ b/src/preload/main-window.js @@ -1,25 +1,19 @@ const { contextBridge, ipcRenderer } = require('electron/renderer') -// We need to wait until the main world is ready to receive the message before -// sending the port. We create this promise in the preload so it's guaranteed -// to register the onload listener before the load event is fired. -const windowLoaded = new Promise((resolve) => { - window.onload = resolve -}) +window.onmessage = (event) => { + // event.source === window means the message is coming from the preload + // script, as opposed to from an