项目作者: mongodb-js

项目描述 :
Simplified IPC for electron apps.
高级语言: JavaScript
项目地址: git://github.com/mongodb-js/hadron-ipc.git
创建时间: 2016-04-14T16:02:27Z
项目社区:https://github.com/mongodb-js/hadron-ipc

开源协议:Apache License 2.0

下载


hadron-ipc travis npm

Simplified wrapper around Electron’s IPC events.

Usage

  1. process.env.DEBUG = 'hadron-*';
  2. const ipc = require('hadron-ipc');
  3. cosnt AppRegistry = require('hadron-app-registry');
  4. const globalAppRegistry = new AppRegistry();
  5. // called from a renderer process:
  6. ipc.call('compass:loading:change-status', { status: 'loading preferences' });
  7. // responded to in the main process:
  8. ipc.respondTo('app:loading:change-status', (evt, meta) => {
  9. // main process then broadcasts to its renderer processes:
  10. ipc.broadcast('app:loading:change-status', meta);
  11. });
  12. // renderer process deals with information when received
  13. ipc.on('app:loading:change-status', (evt, meta) => {
  14. globalAppRegistry.emit('app:loading:change-status', meta);
  15. });

API - from Main Process

Communication from the main process to a renderer process.

ipc.respondTo(methodName, handler)

Respond to an event sent from a renderer process. handler keeps track of
BrowserWindow instance and any of the args.

  1. const ipc = require('hadron-ipc');
  2. const onFindInPage = (sender, searchTerm, opt) => {
  3. if (!_window) return;
  4. opt = opt || {};
  5. _window.webContents.findInPage(searchTerm, opt);
  6. };
  7. ipc.respondTo('app:find-in-page', onFindInPage);

You can also use broadcast as part of the response:

  1. const ipc = require('hadron-ipc');
  2. ipc.respondTo('app:loading:change-status', (evt, meta) => {
  3. ipc.broadcast('app:loading:change-status', meta);
  4. });

ipc.broadcast(methodName, […args])

Broadcast an event to renderer process(es).

For example, here is a broadcast from a Menu Item:

  1. const ipc = require('hadron-ipc');
  2. function viewSubMenu() {
  3. return {
  4. label: '&View',
  5. {
  6. label: '&Reload Data',
  7. accelerator: 'CmdOrCtrl+R',
  8. click: function() {
  9. ipc.broadcast('app:refresh-data'); // renderer processes will be
  10. listening to this event
  11. }
  12. }
  13. ]
  14. };
  15. }

ipc.broadcastFocused(methodName, […args])

Broadcast to renderer process(es) only if the current window is focused.

  1. ipc.broadcastFocused('app:disconnect');

ipc.remove(channel, listener)

Remove a listener from the main process’ ipc.

  1. const ipc = require('hadron-ipc');
  2. const onFindInPage = (sender, searchTerm, opt) => {
  3. if (!_window) return;
  4. opt = opt || {};
  5. _window.webContents.findInPage(searchTerm, opt);
  6. };
  7. ipc.remove('app:stop-find-in-page', onStopFindInPage);

API - from Renderer process

Communication from a renderer proces to the main process. All of the
ipcRenderer events are kept as
is, ipc.call is added as an additional method.

ipc.call(methodName, […args])

Call the main process under the provided methodName. Under the hood args
are serialised as JSON.

  1. const ipc = require('hadron-ipc');
  2. const args = {
  3. query: {
  4. filter: {},
  5. project: { field: 1 }
  6. }
  7. };
  8. ipc.call('app:open-export', args, (res) = {
  9. console.log('callback from renderer process', res)
  10. });

ipc.on(methodName, handler)

From Electron’s ipcRenderer API. Useful for when replying to Main process’
ipc.broadcast events.

  1. const ipc = require('hadron-ipc');
  2. const app = require('hadron-app')
  3. global.hadronApp = app;
  4. ipc.on('app:refresh-data', () => global.hadronApp.appRegistry.emit('refresh-data'));

Install

  1. npm install hadron-ipc

Related Content

License

Apache 2.0