3.2.14 • Published 4 days ago

hadron-ipc v3.2.14

Weekly downloads
328
License
SSPL
Repository
github
Last release
4 days ago

hadron-ipc npm

Simplified wrapper around Electron's IPC events.

Usage

process.env.DEBUG = 'hadron-*';

const ipc = require('hadron-ipc');
const AppRegistry = require('hadron-app-registry');

const globalAppRegistry = new AppRegistry();

// called from a renderer process:
ipc.call('compass:loading:change-status', { status: 'loading preferences' });

// responded to in the main process:
ipc.respondTo('app:loading:change-status', (evt, meta) => {
  // main process then broadcasts to its renderer processes:
  ipc.broadcast('app:loading:change-status', meta);
});

// renderer process deals with information when received
ipc.on('app:loading:change-status', (evt, meta) => {
  globalAppRegistry.emit('app:loading:change-status', meta);
});

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.

const ipc = require('hadron-ipc');

const onFindInPage = (sender, searchTerm, opt) => {
  if (!_window) return;

  opt = opt || {};
  _window.webContents.findInPage(searchTerm, opt);
};

ipc.respondTo('app:find-in-page', onFindInPage);

You can also use broadcast as part of the response:

const ipc = require('hadron-ipc');

ipc.respondTo('app:loading:change-status', (evt, meta) => {
  ipc.broadcast('app:loading:change-status', meta);
});

ipc.broadcast(methodName, ...args)

Broadcast an event to renderer process(es).

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

const ipc = require('hadron-ipc');

function viewSubMenu() {
  return {
    label: '&View',
      {
        label: '&Reload Data',
        accelerator: 'CmdOrCtrl+R',
        click: function() {
          ipc.broadcast('app:refresh-data'); // renderer processes will be
listening to this event
        }
      }
    ]
  };
}

ipc.broadcastFocused(methodName, ...args)

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

ipc.broadcastFocused('app:disconnect'); 

ipc.remove(channel, listener)

Remove a listener from the main process' ipc.

const ipc = require('hadron-ipc');

const onFindInPage = (sender, searchTerm, opt) => {
  if (!_window) return;

  opt = opt || {};
  _window.webContents.findInPage(searchTerm, opt);
};

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.

const ipc = require('hadron-ipc');

const args = {
  query: {
    filter: {},
    project: { field: 1 }
  }
};
ipc.call('app:open-export', args, (res) = {
  console.log('callback from renderer process', res) 
});

ipc.on(methodName, handler)

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

const ipc = require('hadron-ipc');
const app = require('hadron-app')
global.hadronApp = app;

ipc.on('app:refresh-data', () => global.hadronApp.appRegistry.emit('refresh-data'));

Install

npm install hadron-ipc

Related Content

3.2.14

8 days ago

3.2.13

29 days ago

3.2.12

2 months ago

3.2.10

4 months ago

3.2.9

4 months ago

3.2.8

4 months ago

3.2.7

5 months ago

3.2.6

5 months ago

3.2.5

5 months ago

3.2.4

6 months ago

3.2.3

6 months ago

3.2.2

7 months ago

3.2.1

7 months ago

3.2.0

8 months ago

3.1.5

8 months ago

3.1.3

11 months ago

3.1.4

9 months ago

3.1.2

1 year ago

3.1.1

1 year ago

3.1.0

2 years ago

3.0.0

2 years ago

2.10.0

2 years ago

2.9.0

2 years ago

2.8.0

2 years ago

2.7.0

2 years ago

2.6.0

2 years ago

2.5.0

3 years ago

2.4.0

3 years ago

2.3.1

3 years ago

2.3.0

3 years ago

2.2.1

3 years ago

2.2.0

3 years ago

1.2.0

3 years ago

1.2.1

3 years ago

2.1.0

3 years ago

2.0.0

3 years ago

1.1.1

4 years ago

1.1.0

6 years ago

1.0.0

6 years ago

0.0.8

8 years ago

0.0.7

8 years ago

0.0.6

8 years ago

0.0.5

8 years ago

0.0.4

8 years ago

0.0.3

8 years ago

0.0.2

8 years ago

0.0.1

8 years ago