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

process-eventemitter

v1.0.1

Published

Implementation of the 'distributed-eventemitter' module API for the browser or single node processes!

Downloads

8

Readme

Why?

This module implements the 'distributed-eventemitter' API for the browser or single node processes. Intended for non-distributed deployments or development.

Quick Start

var EventEmitter = require('process-eventemitter');
var events = new EventEmitter(); 

events.on('email.send', (message, resolve, reject) => {
  console.log('sending email...');
  //... send email
  // ...

  resolve('sent');
});


// somewhere else in your code
events.emitToOne('email.send', {
  to: '[email protected]',
  subject: 'Hello Node.js',
  body: 'Request/response feature is just awesome...'
}, 3000).then((response) => {
  if ('sent' === response) {
    console.log('email was sent!');
  }
}).catch(console.log.bind());

Example using ES6 generators

"use strict";

const co = require('co');
co(function* () {
    try {
        let response = yield events.emitToOne('email.send', {
            to: '[email protected]',
            subject: 'Hello Node.js',
            body: 'Request/response feature is just awesome...'
        }, 3000);

        if ('sent' === response) {
            console.log('email was sent!');
        }
    } catch (error) {
        console.log('error: ' + error);
    }
});

Requirements

  • None

Installation

$ npm install process-eventemitter

Features

The same 'distributed-eventemitter' module API, but restricted to a single process and emitter instance. The methods 'connect', 'disconnect' are just kept for compatibility, in case you want to make your code distributed later ;)

Internal events

events.on('connected', (emitterId) => {
    // triggered when the emitter has been connected
});

events.on('disconnected', (emitterId) => {
    // triggered when the emitter has been disconnected
});

events.on('error', (error) => {
   // triggered when an internal error occurs 
}):

events.on('request', (event, request, raw) => {
   // triggered before invoke a listener using emitToOne feature
   
   // request data filtering and modification is allowed
   // example:
   request.data = ('string' === typeof request.data) ? request.data.toUpperCase() : request.data
}):

events.on('response', (event, response, raw) => {
   // triggered after invoke a listener using emitToOne feature
   
   // response data filtering and modification is allowed
   // example:
   if (response.ok)
     response.data = ('string' === typeof response.data) ? response.data.toUpperCase() : response.data
   else 
     console.log('error ocurred: ' + response.data.message);
}):

API

getId: Get the emitter instance unique id.

events.getId(); // UUID v4 value

connect: Does nothing. Kept for compatilibity with the distributed version.

events.connect().then(()=> {
  console.log('connected');
});

disconnect:Does nothing. Kept for compatilibity with the distributed version.

events.disconnect().then(()=> {
  console.log('disconnected');
});

emitToOne: Notify a custom event to only one target listener. The method accept only one argument as event data.

events.on('my.event', (data, resolve, reject) => {
  if ('hello' === data){
    resolve('world');
  } else {
    reject('invalid args');
  }
});

// calling without timeout
events.emitToOne('my.event', 'hello').then((response) => {
  console.log('world' === response);
});

// calling with timeout (ms)
events.emitToOne('my.event', {data: 'hello'}, 100).catch((error) => {
  console.log('invalid args' === error);
});

Tests

$ npm install
$ npm test