electron-pubsub

0.2.2 • Public • Published

electron-pubsub

Pubsub for Electron.

You can use it in main or renderer process.

Usage

Initialization

npm install --save electron-pubsub

// in main process
const pubsub = require('electron-pubsub');

// in renderer process
const pubsub = require('electron').remote.require('electron-pubsub');

Subscribe

pubsub.subscribe('myTopic', (event, ...args) => {
    // do something here.
});
 
pubsub.once('anotherTopic', someCallback);

Publish

// in any process
pubsub.publish('myTopic', 'some arguments');

Unsubscribe

// in any process
pubsub.unsubscribe('myTopic', someCallback);
pubsub.unsubscribe('myTopic');
 
pubsub.clearAllSubscriptions();

Hierarchical addressing

pubsub.subscribe('a', myFunc1);
pubsub.subscribe('a.b', myFunc2);
pubsub.subscribe('a.b.c', myFunc3);
 
pubsub.unsubscribe('a.b');

Use promises

pubsub.publish('myTopic', 'some arguments')
    .then(onSuccess)
    .catch(onFailure);
 
pubsub.subscribe('myTopic', (event, ...args) => {
    event.resolve('success!');
});

TODO

  • Support hierarchy of topics.

Readme

Keywords

Package Sidebar

Install

npm i electron-pubsub

Weekly Downloads

5

Version

0.2.2

License

MIT

Last publish

Collaborators

  • jianfei