Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

hadron-ipc

Package Overview
Dependencies
Maintainers
1
Versions
495
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

hadron-ipc - npm Package Compare versions

Comparing version 0.0.2 to 0.0.3

lib/common.js

79

index.js
'use strict';
const _ = require('lodash');
const electron = require('electron');
const isRenderer = require('is-electron-renderer');
const isPromise = require('is-promise');
let debug = null;
let ipcRenderer = null;
let ipcMain = null;
let BrowserWindow = null;
if (isRenderer) {
ipcRenderer = electron.ipcRenderer;
debug = require('debug')('hadron-ipc:renderer');
module.exports = require('./lib/renderer');
} else {
ipcMain = electron.ipcMain;
BrowserWindow = electron.BrowserWindow;
debug = require('debug')('hadron-ipc:main');
module.exports = require('./lib/main');
}
function getResponseChannel(methodName) {
return 'hadron-ipc-' + methodName + '-response';
}
exports.call = function(methodName, ...args) {
debug(`calling ${methodName} with args`, args);
const responseChannel = getResponseChannel(methodName);
const errorResponseChannel = `${responseChannel}-error`;
return new Promise(function(resolve, reject) {
ipcRenderer.on(responseChannel, function(event, result) {
debug(`got response for ${methodName} from main`, result);
ipcRenderer.removeAllListeners(responseChannel);
ipcRenderer.removeAllListeners(errorResponseChannel);
resolve(result);
});
ipcRenderer.on(errorResponseChannel, function(event, err) {
debug(`error for ${methodName} from main`, err);
ipcRenderer.removeAllListeners(responseChannel);
ipcRenderer.removeAllListeners(errorResponseChannel);
reject(err);
});
ipcRenderer.send(methodName, ...args);
});
};
exports.respondTo = (methodName, handler) => {
if (_.isPlainObject(methodName)) {
_.forIn(methodName, (methodHandler, name) => {
exports.respondTo(name, methodHandler);
});
return exports;
}
const responseChannel = getResponseChannel(methodName);
const errorResponseChannel = `${responseChannel}-error`;
ipcMain.on(methodName, (event, ...args) => {
const browserWindow = BrowserWindow.fromWebContents(event.sender);
const resolve = (result) => {
debug(`responding with result for ${methodName}`, result);
event.sender.send(responseChannel, result);
};
const reject = (err) => {
debug(`responding with error for ${methodName}`, err);
event.sender.send(errorResponseChannel, err);
};
debug(`calling ${methodName} handler with args`, args);
const res = handler(browserWindow, ...args);
if (isPromise(res)) {
res.then(resolve).catch(reject);
} else if (res instanceof Error) {
reject(res);
} else {
resolve(res);
}
});
return exports;
};
{
"name": "hadron-ipc",
"description": "Simplified IPC for electron apps.",
"version": "0.0.2",
"version": "0.0.3",
"scripts": {

@@ -6,0 +6,0 @@ "fmt": "mongodb-js-fmt",

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc