Skip to content

Commit 92faaf6

Browse files
committed
Expose the desktop version & auth token synchronously
Being able to wait once and then directly access this data synchronously is much better than needing to async every possible access to this data.
1 parent cb0b514 commit 92faaf6

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

src/preload.ts

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,29 @@ import { contextBridge, ipcRenderer } from 'electron';
22

33
import type { ContextMenuDefinition } from './context-menu';
44

5+
// These are technically asynchronous, but they're so fast that
6+
// they're effectively sychronously available - this seems to
7+
// run before inline scripts in the page itself, let alone the
8+
// main app code. Nonetheless, to be safe the UI can wait for
9+
// the preload promise here to confirm it's definitely ready.
10+
let desktopVersion: string | undefined;
11+
let authToken: string | undefined;
12+
13+
const preloadPromise = Promise.all([
14+
ipcRenderer.invoke('get-desktop-version').then(result => {
15+
desktopVersion = result;
16+
}),
17+
ipcRenderer.invoke('get-server-auth-token').then(result => {
18+
authToken = result;
19+
})
20+
]);
21+
522
contextBridge.exposeInMainWorld('desktopApi', {
6-
desktopVersion: () =>
7-
ipcRenderer.invoke('get-desktop-version'),
8-
serverAuthToken: () =>
9-
ipcRenderer.invoke('get-server-auth-token'),
23+
waitUntilDesktopApiReady: () => preloadPromise.then(() => {}),
24+
25+
getDesktopVersion: () => desktopVersion,
26+
getServerAuthToken: () => authToken,
27+
1028
selectApplication: () =>
1129
ipcRenderer.invoke('select-application'),
1230
openContextMenu: (options: ContextMenuDefinition) =>

0 commit comments

Comments
 (0)