Comlink provides way to interface between worker-like endpoint via ES proxy. This module allows to create custom endpoints around Electron's IPC (https://electronjs.org/docs/api/ipc-main) for proxy object across Electron's processes.
This has a peer dependencies of comlink@4
, which will have to be installed as well
npm install comlink-electron-adapter
There are separate endpoint creation method per object's origin.
// main process
import { ipcMain } from 'electron';
import { expose } from 'comlink';
import { mainProcObjectEndpoint } from 'comlink-electron-adapter';
const mainProcessObject = {...};
expose(mainProcessObject, mainProcObjectEndpoint(ipcMain));
// renderer process
import { wrap } from 'comlink';
import { ipcRenderer } from 'electron';
// `proxied` is proxy object to `mainProcessObject` in main process
const proxied = wrap(mainProcObjectEndpoint(ipcRenderer));
It is also possible to access renderer process's object in main process.
// renderer process
import { expose } from 'comlink';
import { ipcRenderer } from 'electron';
import { rendererProcObjectEndpoint } from 'comlink-electron-adapter';
const rendererProcessObject = {...};
expose(rendererProcessObject, rendererProcObjectEndpoint(ipcRenderer));
// main process
import { wrap } from 'comlink';
import { ipcMain } from 'electron';
import { rendererProcObjectEndpoint } from 'comlink-electron-adapter';
const rendererProcessWindow = new BrowserWindow();
rendererProcessWindow.loadFile('.../renderer.js');
const proxied = wrap(requestRendererProcessEndpoint(ipcMain, rendererProcessWindow.webContents));
Unlike renderer process main process does not aware target renderer process to send message and target webContents should be specified. Also main process should wait call proxy object until renderer process expose its object.
Due to Electron IPC's design comlink-electron-adapter
do not support transferable
. Using Comlink.transfer
will raise not supported exception.
Few npm scripts are supported for build / test code.
-
build
: Transpiles code to ES5 commonjs todist
. -
test
: Run test cases. -
lint
: Run lint over all codebases -
lint:staged
: Run lint only for staged changes. This'll be executed automatically with precommit hook. -
commit
: Commit wizard to write commit message