@terasum/electron-call
v0.0.8
Published
The easiest main-renderer IPC communication
Downloads
1
Readme
electron-call
The easiest main-renderer IPC communication. Now calling a method/function in a remote process looks the sames as calling a local. Supports both main → renderer and renderer → main calls. Renderer → renderer is on the roadmap.
Warning: API could be changes frequently until v0.1.0 release.
Key features
- Very simple API
- Typescript friendly
- Lightweight and fast
- No dependencies
// MainApi.ts
import { app } from 'electron';
import call from 'electron-call';
export class MainApi {
async getAppName() {
return app.getName();
}
}
call.provide('MainApi', MainApi);
// renderer.ts
import call from 'electron-call';
import type { MainApi } from '../main/MainApi';
const mainApi = call.use<MainApi>('MainApi')
console.log(await mainApi.getAppName());
Installation
Install with npm:
npm install electron-call
Usage
Provide API
There are 3 ways of defining API:
Using a class
Preferred way, since it provides the best type support
export class FsApi {
async selectDirectory(defaultPath: string) {
return dialog.showOpenDialog({
defaultPath,
properties: ['openDirectory'],
});
}
}
call.provide('FsApi', new FsApi());
Using an object
It works the same as above, but there's a lack of types. That's fine if you don't use TypeScript or prefer a separated interface for ApiName
call.provide('FsApi', {
async selectDirectory(defaultPath) {
return dialog.showOpenDialog({
defaultPath,
properties: ['openDirectory'],
});
},
});
Using a function
call.provideFunction('selectDirectory', async (defaultPath) => {
return dialog.showOpenDialog({
defaultPath,
properties: ['openDirectory'],
});
});
Use API
Using a class/object
You can omit using FsApi generic if you don't need type support
const fsApi = call.use<FsApi>('FsApi')
console.log(await fsApi.selectDirectory(defaultPath));
Using a function
const selectDirectory = call.use('selectDirectory')
console.log(await selectDirectory(defaultPath));