electron-re
Using electron-re
to generate service process and send data from main process to service, from render process to service, and service to service.
Instruction
The service
process is a customized render process that works in the background, receiving path
, options
as arguments:
- path -- the absolute path to a js file
- options -- the same as
new BrowserWindow()
options
In order to send data from main or other process to a service you need use MesssageChannel
, such as: MessageChannel.send('service-name', 'channel', 'params')
Usage
- service
The service is a customized BrowserWindow
instance, it has only method connected()
which return a resolved Promise
when service is ready, suggest to put some business-related code into a service.
const { BrowserService } = require('electron-re');
...
app.whenReady().then(() => {
const myService = new BrowserService('app', 'path/to/app.service.js');
myService.connected().then(() => {
mhyService.webContents.send('channel1', { value: 'test1' });
...
})
});
const { ipcRenderer } = require('electron');
ipcRenderer.on('channel1', (event, result) => {
...
});
- MessageChannel
A messaging tool expanding some method from electron build-in ipc:
const { BrowserService, MessageChannel } = require('electron-re');
...
app.whenReady().then(() => {
const myService = new BrowserService('app', 'path/to/app.service.js');
myService.connected().then(() => {
MessageChannel.send('app', 'channel1', { value: 'test1' });
MessageChannel.invoke('app', 'channel2', { value: 'test1' }).then((response) => {
console.log(response);
});
MessageChannel.on('channel3', (event, response) => {
console.log(response);
});
MessageChannel.handle('channel4', (event, response) => {
console.log(response);
return { res: 'channel4-res' };
});
})
});
const { ipcRenderer } = require('electron');
const { MessageChannel } = rrequire('electron-re');
MessageChannel.on('channel1', (event, result) => {
console.log(result);
});
MessageChannel.handle('channel2', (event, result) => {
console.log(result);
return { response: 'channel2-response' }
});
MessageChannel.invoke('app2', 'channel3' (event, result) => {
console.log(result);
});
MessageChannel.send('app', 'channel4', { value: 'channel4' });
MessageChannel.handle('channel3', (event, result) => {
console.log(result);
return { response: 'channel3-response' }
});
MessageChannel.once('channel4', (event, result) => {
console.log(result);
});
MessageChannel.send('main', 'channel3', { value: 'channel3' });
MessageChannel.invoke('main', 'channel4', { value: 'channel4' });
const { ipcRenderer } = require('electron');
const { MessageChannel } = rrequire('electron-re');
MessageChannel.send('app', ....);
MessageChannel.invoke('app', ....);
MessageChannel.send('main', ....);
MessageChannel.invoke('main', ....);
Example
check the index.test.js
and test
dir in root, there are some cases, you can run npm run test
to see the test result of the library.