Skip to content

Event Dispatcher Concept for Electron Apps

License

Notifications You must be signed in to change notification settings

hk-labs/electron-event-dispatcher

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

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.

#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

About

Event Dispatcher Concept for Electron Apps

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published