fix(stage-tamagotchi): better type checks with exposeWithCustomAPI
This commit is contained in:
@@ -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 = unknown>(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 = unknown>(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 = unknown>(customApi: CustomApi = undefined as
|
||||
else {
|
||||
window.electron = electronAPI
|
||||
window.platform = platform
|
||||
;(window as ElectronWindow<CustomApi>).api = customApi
|
||||
}
|
||||
}
|
||||
|
||||
export function exposeWithCustomAPI<CustomAPI>(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<CustomAPI>).api = customAPI
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user