eipfork v0.0.0-pr-20220717211924
Eipfork - async messaging for electron
Eipfork is fork of Eiphop rewritten in typescript.
Installation
npm i eipfork
# or
yarn add eipfork
Getting started
Actions are defined in main process and called from renderer with async response.
Define actions
Imagine you have the following actions in your main
process:
const pingActions = {
ping: (req, res) => {
const { payload } = req;
res.send({ msg: 'pong' });
},
};
const hipActions = {
hip: async (req, res) => {
const { payload } = req;
res.notify('Sleeping for 800ms, BRB');
// sleep for 800ms
await new Promise((done) => setTimeout(done, 800));
res.send({ msg: 'hop' });
// or res.error({msg: 'failed'})
},
};
Setup main handler
Actions from different domain objects need to be combined to one global map and passed to eiphop's setupMainHandler
function.
import { setupMainHandler } from 'eiphop';
import electron from 'electron';
setupMainHandler({ ...hipActions, ...pingActions });
Setup Renderer Listener
In your renderer’s index.js file, setup the listener as follows:
import { setupFrontendListener } from 'eiphop';
setupFrontendListener();
Now your channels are ready. All you need to do is trigger actions.
Emit actions and expect response
Use the emit
function to call actions defined in the main
action map.
import { emit } from 'eiphop';
emit('ping', { you: 'can', pass: 'data', to: 'main' }, (msg) => {
console.log(msg);
})
.then((res) => console.log(res)) // will log {msg: 'pong'}
.catch((err) => console.log(err));
emit('hip', { empty: 'payload' }, (msg) => {
console.log(msg);
})
.then((res) => console.log(res)) // will log {msg: 'hop'}
.catch((err) => console.log(err));
emit
takes up to three arguments:
- The name of the action to call (this was defined is actions map in
main
) - The payload to send (this can be an object, string, list etc)
- Optional A callback function called by the main process to notify the render process with a message.
Using Notifiers
For example, sometimes there is a long running operation on the main process and you may want to provide the render process with an update as to it's progress.
You can use notifiers to send a message to the emiter on the render process by using res.notify
without resolving the promise.
import { emit } from 'eiphop';
emit(
'download',
{
/* fileIds, etc */
},
(msg) => {
console.log(msg);
}
)
.then((res) => console.log(res)) // will contain the downloaded file
.catch((err) => console.log(err));
// Main process
const pingActions = {
ping: (req, res) => {
const { payload } = req;
payload.filesToDownload.forEach((fileId) => {
// Tell the render process what is happening so it can inform the user
res.notify(`Downloading file ${fileId}`);
downloadFile(payload.fileId);
});
res.send({ msg: 'pong' });
},
};
2 years ago
2 years ago
2 years ago