Private package providing strongly typed DesktopApi
used in @trezor/suite
and @trezor/suite-desktop
. DesktopApi
handles inter-process comumunication inside Electron between the main
context, i.e. native processes running in NodeJS, and renderer
context, i.e. browser-like processes running on Chromium.
Exported modules:
main
(default) used in@trezor/suite-desktop/src
in main (NodeJS) context.renderer
(browser) used in@trezor/suite
and@trezor/suite-desktop-ui
in renderer context.
export function getDesktopApi(ipcRenderer?: Electron.IpcRenderer): DesktopApi;
export const desktopApi: DesktopApi;
-
Overload
electron
types. See typed-electron.ts -
Create
DesktopApi
instance and expose it to rendererwindow.desktopApi
object. See preload.ts -
Receive invoke messages from renderer. See metadata module
-
Receive event messages from renderer. See theme module
-
Send event messages to renderer. See
mainWindow.webContents.send
in autoupdater module
-
DesktopApi.invoke
. See metadata module -
DesktopApi.on
. See AutoUpdater component
To invoke a method on the main
process and return an asynchronous result to the renderer
process
- add a channel to
./src/api.ts InvokeChannels
- add a method to
./src/api.ts DesktopApi
asDesktopApiInvoke<'your-new-channel'>
- process incoming request in
@trezor/suite-desktop/src/modules/*
usingipcMain.handle('your-new-channel', (arg?: string) => { return 1; })
- trigger it from
@trezor/suite
usingconst r = await desktopApi.yourNewFunction()
To receive an asynchronous event in renderer
process
- add a channel to
./src/api.ts RendererChannels
- set a listener in
@trezor/suite
usingawait desktopApi.on('your-new-channel', (payload) => {})
- trigger an event from
@trezor/suite-desktop/src/modules/*
usingmainWindow.webContents.send('your-new-channel', { foo: 'bar' })
To receive an asynchronous event in main
process
- add a channel to
./src/api.ts MainChannels
- add a method to
./src/api.ts DesktopApi
asDesktopApiSend<'your-new-channel'>
- set a listener in
@trezor/suite-desktop/src/modules/*
usingipcMain.on('your-new-channel', (_, { foo }) => {})
- trigger an event from
@trezor/suite
usingdesktopApi.yourNewFunction({ foo: 'bar' })