diff --git a/apps/stage-tamagotchi/src/preload/shared.ts b/apps/stage-tamagotchi/src/preload/shared.ts index 10353bcba..8150b9f9e 100644 --- a/apps/stage-tamagotchi/src/preload/shared.ts +++ b/apps/stage-tamagotchi/src/preload/shared.ts @@ -5,7 +5,7 @@ import { contextIsolated, platform } from 'node:process' import { electronAPI } from '@electron-toolkit/preload' import { contextBridge, ipcRenderer } from 'electron' -export function expose(customApi: CustomApi = undefined as CustomApi) { +export function expose() { // TODO: once we refactored eventa to support window-namespaced contexts, // we can remove the setMaxListeners call below since eventa will be able to dispatch and // manage events within eventa's context system. @@ -18,7 +18,6 @@ export function expose(customApi: CustomApi = undefined as try { contextBridge.exposeInMainWorld('electron', electronAPI) contextBridge.exposeInMainWorld('platform', platform) - contextBridge.exposeInMainWorld('api', customApi) } catch (error) { console.error(error) @@ -27,6 +26,24 @@ export function expose(customApi: CustomApi = undefined as else { window.electron = electronAPI window.platform = platform - ;(window as ElectronWindow).api = customApi + } +} + +export function exposeWithCustomAPI(customAPI: CustomAPI) { + expose() + + // Use `contextBridge` APIs to expose Electron APIs to + // renderer only if context isolation is enabled, otherwise + // just add to the DOM global. + if (contextIsolated) { + try { + contextBridge.exposeInMainWorld('api', customAPI) + } + catch (error) { + console.error(error) + } + } + else { + (window as ElectronWindow).api = customAPI } }