Skip to content

Latest commit

 

History

History
 
 

suite-desktop-api

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 

@trezor/suite-desktop-api

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;

Usage examples in main process

Usage examples in renderer process

How to add new method/channel

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 as DesktopApiInvoke<'your-new-channel'>
  • process incoming request in @trezor/suite-desktop/src/modules/* using ipcMain.handle('your-new-channel', (arg?: string) => { return 1; })
  • trigger it from @trezor/suite using const 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 using await desktopApi.on('your-new-channel', (payload) => {})
  • trigger an event from @trezor/suite-desktop/src/modules/* using mainWindow.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 as DesktopApiSend<'your-new-channel'>
  • set a listener in @trezor/suite-desktop/src/modules/* using ipcMain.on('your-new-channel', (_, { foo }) => {})
  • trigger an event from @trezor/suite using desktopApi.yourNewFunction({ foo: 'bar' })