npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

electron-event-dispatcher

v0.1.0

Published

Event Dispatcher Concept for Electron Apps

Downloads

9

Readme

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