Socket
Socket
Sign inDemoInstall

electron-event-dispatcher

Package Overview
Dependencies
0
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    electron-event-dispatcher

Event Dispatcher Concept for Electron Apps


Version published
Weekly downloads
4
increased by100%
Maintainers
1
Install size
48.0 kB
Created
Weekly downloads
 

Readme

Source

Electron Event Dispatcher

Build Status npm version dependencies Status

Helps you to organize communication between BrowserWindows, main/renderer IPCs, Sockets or any other EventEmitters 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 = require('electron-event-dispatcher');

class DeviceEventsDispatcher extends EventDispatcher {
  constructor(driver, ipcMain) {
    super();
    
    // driver event handlers
    this.connect(driver, {
      connected: () => {
        // send `device-connected` event to all bound windows
        this.broadcast('device-connected');
      },
      disconnected: () => {
        // send `device-disconnected` event to all bound windows
        this.broadcast('device-disconnected');
      },
      data: (data) => {
        // send device data to all bound windows
        this.broadcast('device-data-received', data);
      }
    });
    
    // windows' (renderer) event handlers
    this.connect(ipcMain, {
      'get-device-state': (event) => {
        // respond to the requesting browser window
        event.sender.send('device-state', {
          connected: driver.connected || false
        });
      }
    });
  }
  
  /**
   * Override start/stop if you want to add some pre/post hooks.
   * @return {Promise}
   */
  start() {
    // add some pre-start logic...
    
    return super.start()
      .then(() => {
        // add some post-start logic...
      });
  }
  
  /**
   * Override start/stop if you want to add some pre/post hooks.
   * @return {Promise}
   */
  stop() {
    // add some pre-stop logic...
    
    return super.stop()
        .then(() => {
          // add some post-stop logic...
        });
    }
}

module.exports = DeviceEventsDispatcher;

app/main.js

const {app, BrowserWindow, ipcMain} = require('electron');
const DeviceEventsDispatcher = require('../lib/device-events-dispatcher');
const DeviceDriver = require('../lib/device-driver'); // an example event emitter

let mainWindow;
let settingsWindow;
let deviceEventsDispatcher;

function createMainWindow() {
  mainWindow = new BrowserWindow({ ... });
  mainWindow.loadUrl('../main.html');
  
  deviceEventsDispatcher.attach(mainWindow);
  
  mainWindow.on('closed', () => {
    mainWindow = null;
  });
}

function createSettingsWindow() {
  settingsWindow = new BrowserWindow({ ... });
  settingsWindow.loadUrl('../settings.html');
    
  deviceEventsDispatcher.attach(settingsWindow);
    
  settingsWindow.on('closed', () => {
    settingsWindow = null;
  });
}

app.on('ready', () => {
  const driver = new DeviceDriver();
  deviceEventsDispatcher = new DeviceEventsDispatcher(driver, ipcMain);
  
  deviceEventsDispatcher.start()
    .then(() => createMainWindow());
});

app.on('window-all-closed', () => {
  deviceEventsDispatcher.stop()
    .then(() => {
      if (process.platform !== 'darwin') {
        app.quit();
      }
    });
});

app.on('activate', () => {
  deviceEventsDispatcher.start()
    .then(() => createMainWindow());
});

API

Class: EventDispatcher

An instance of EventDispatcher is a unit that represents an event hub between electron's BrowserWindows and any EventEmitters (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.

ParamTypeDescription
windowBrowserWindowElectron'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.

ParamTypeDescription
windowBrowserWindowElectron's BrowserWindow instance.
#broadcast(event, [...args])

Broadcast an event to all attached windows.

ParamTypeDescription
eventStringThe name of the event to broadcast.
[...args]EventEmitterAny number of event-related arguments.
#connect(emitter, handlers)

Connect event emitter handlers in the context of this dispatcher.

ParamTypeDescription
emitterEventEmitterAn event emitter instance.
handlersObject<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.

ParamTypeDescription
emitterEventEmitterAn 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.

#isRunningboolean

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

Keywords

FAQs

Last updated on 12 Sep 2017

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc