Skip to content

Commit

Permalink
Expose the desktop version & auth token synchronously
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
pimterry committed Aug 30, 2023
1 parent cb0b514 commit 92faaf6
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions src/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,29 @@ import { contextBridge, ipcRenderer } from 'electron';

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

// These are technically asynchronous, but they're so fast that
// they're effectively sychronously available - this seems to
// run before inline scripts in the page itself, let alone the
// main app code. Nonetheless, to be safe the UI can wait for
// the preload promise here to confirm it's definitely ready.
let desktopVersion: string | undefined;
let authToken: string | undefined;

const preloadPromise = Promise.all([
ipcRenderer.invoke('get-desktop-version').then(result => {
desktopVersion = result;
}),
ipcRenderer.invoke('get-server-auth-token').then(result => {
authToken = result;
})
]);

contextBridge.exposeInMainWorld('desktopApi', {
desktopVersion: () =>
ipcRenderer.invoke('get-desktop-version'),
serverAuthToken: () =>
ipcRenderer.invoke('get-server-auth-token'),
waitUntilDesktopApiReady: () => preloadPromise.then(() => {}),

getDesktopVersion: () => desktopVersion,
getServerAuthToken: () => authToken,

selectApplication: () =>
ipcRenderer.invoke('select-application'),
openContextMenu: (options: ContextMenuDefinition) =>
Expand Down

0 comments on commit 92faaf6

Please sign in to comment.