Skip to content

Instantly share code, notes, and snippets.

@YurySolovyov
Last active April 24, 2018 17:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save YurySolovyov/bd39405a53b719eae0509ea80aa0fcb7 to your computer and use it in GitHub Desktop.
Save YurySolovyov/bd39405a53b719eae0509ea80aa0fcb7 to your computer and use it in GitHub Desktop.
async-ipc API proposal

async-ipc API

API consists of 2 parts: EventEmitter-like events passing with .on/.emit and request/response with .request/.respondTo.

EventEmitter one is quite obvious - EventEmitter over the wire/processes.

Request/Response one allows to register a request handler for a particular request type instead of using events to implement same functionality.

Result should be anything else but undefined (print warning otherwise) - Promises are awaited to be resolved and plain values are sent as is. There is one constraint - value should be serializable via JSON.stringify.

Open questions so far:

  • is that a good idea?
  • is that worth doing the way described here?
  • how to handle binary data while keeping ergonomics nice?
  • should we handle binary data at all?
const ipc = require('async-ipc'); // symmetrical in both main and renderer
const SomeAPI = require('some-api');
// events:
ipc.on('create-new-window', (...args) => {
// create new BrowserWindow(...args);
});
ipc.emit('ready', 'some-important-string', 'other-important-arg');
// request/(response)
ipc.respondTo('request-type-string', request => {
return SomeAPI.fetchResult(request);
});
const ipc = require('async-ipc'); // symmetrical in both main and renderer
// events:
ipc.on('ready', (...args) => {
SomeOtherAPI.doStuff(...args).then(res => {
ipc.emit('create-new-window', res);
});
});
// (request)/response
const fetchDataFromMain = async () => {
const requestData = {
importantArgument: 42,
otherStuff: [1,2,3]
};
const result = await ipc.request('request-type-string', requestData);
doSomethingWith(result);
};
ipc.request('unkown-request-type', { ... }) // throws or rejects ?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment