electron-ipcfy ([aɪ'pɪsɪfaɪ]) is an electron ipc typescript framework, it has following features:
- Simplified calling routing
- Strong typed
- Support asynchronous return values / error handling from ipc
import { ipcfy } from "electron-ipcfy";
interface TestService {
greet(name: string);
}
const testService = ipcfy<TestService>('test'); // 'test' is ipc topic id
if (process.type == 'browser') {
// Attach implementation in main process
testService.__attachImpl(
new class implements TestService {
greet(name: string) {
console.log(`Hello, ${name}!`);
}
});
}
// Then you can call it in any process
testService.greet('world');
In electron, main process and renderer process are isolated, the only way to communicate is via IPC. IPC has thress forms in practise:
- Main process to renderer process
- Renderer process to main process
- Renderer process to another renderer process
Each form's implementation routine are different, which make it more complicated to maintain and refract.
Also, when the number of IPCs increases, code maintaining and refractoring are becoming a nightmare:
- When refractoring
topic
, must make sure thatipcMain.on
andipcRenderer.send
changed at the same time. - When refractoring arguments (type or order),
ipcMain.on
andipcRenderer.send
changed at the same time too. - Refractoring is hard to be validated since bugs won't appear until it is called at runtime.
- Asynchronous return value / error handling can't be done at same time.
npm install --save electron-ipcfy
- Topic and method signature is only defined in one source file. Once any method signature is changed, both implementation side and invocation side will trigger typescript compile error. So refract friendly.
- Builtin return value support. All value returned by
ipc
on invocation side is aPromise
, so return value and error handling is pretty simple and elegant viaasync
/await
- It only make sense when used together with typescript.
- Arguments and return value must be simple objects (json serializable), so functions and prototypes won't be passed to main process. (Limited by electron ipc)