Electron Event Dispatcher
Helps you to organize communication between BrowserWindow
s, main/renderer IPCs,
Sockets or any other EventEmitter
s in your Electron applications.
Installation
NPM:
$ npm install electron-event-dispatcher
Yarn:
$ yarn add electron-event-dispatcher
Usage
lib/device-events-dispatcher.js
const EventDispatcher = ; { super; // driver event handlers this; // windows' (renderer) event handlers this; } /** * Override start/stop if you want to add some pre/post hooks. * @return */ { // add some pre-start logic... return superstart ; } /** * Override start/stop if you want to add some pre/post hooks. * @return */ { // add some pre-stop logic... return super ; } moduleexports = DeviceEventsDispatcher;
app/main.js
const app BrowserWindow ipcMain = ;const DeviceEventsDispatcher = ;const DeviceDriver = ; // an example event emitter let mainWindow;let settingsWindow;let deviceEventsDispatcher; { mainWindow = ... ; mainWindow; deviceEventsDispatcher; mainWindow;} { settingsWindow = ... ; settingsWindow; deviceEventsDispatcher; settingsWindow;} app; app; app;
API
Class: EventDispatcher
An instance of EventDispatcher
is a unit that represents an event hub between
electron's BrowserWindow
s and any EventEmitter
s (electron's ipc, tcp/web sockets, device drivers, etc.).
#attach(window)
Attach a BrowserWindow
instance to the event dispatcher.
Attached windows will receive all #broadcast()
ing events.
Param | Type | Description |
---|---|---|
window | BrowserWindow |
Electron's BrowserWindow instance. |
#detach(window)
Detach a given window
from the event dispatcher.
Technically, removes a given window
reference from
the attached windows collection to give'em be garbage collected.
Note that windows are detached automatically on
closed
.
Param | Type | Description |
---|---|---|
window | BrowserWindow |
Electron's BrowserWindow instance. |
#broadcast(event, [...args])
Broadcast an event to all attached windows.
Param | Type | Description |
---|---|---|
event | String |
The name of the event to broadcast. |
[...args] | EventEmitter |
Any number of event-related arguments. |
#connect(emitter, handlers)
Connect event emitter handlers in the context of this dispatcher.
Param | Type | Description |
---|---|---|
emitter | EventEmitter |
An event emitter instance. |
handlers | Object<String, Function> |
An event => function object map, where event is a name of event to handle and function is a handler of this event. |
#disconnect(emitter)
Disconnect event emitter handlers from this dispatcher.
Param | Type | Description |
---|---|---|
emitter | EventEmitter |
An event emitter instance. |
#start()
⇒ Promise
Start the event dispatcher. Binds all #connect()
ed event emitters' handlers.
#stop()
⇒ Promise
Stop the event dispatcher. Unbinds all #connect()
ed event emitters' handlers.
#restart()
⇒ Promise
Restart the event dispatcher.
#isRunning
⇒ boolean
Returns true
if the event dispatcher is running.
Contributing
Feel free to dive in! Open an issue or submit PRs.
Running tests
In your terminal run:
$ npm test
or
$ yarn test
License
Licensed under MIT © 2017 Holy Krab Labs