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

simple-ee

v3.1.0

Published

Simple event emitter

Downloads

6

Readme

Simple EE

Simple event emitter

npm npm

Contents

Installation

npm install --save simple-ee

Usage

// Load the simple-ee module
var EE = require('simple-ee');

// A handler function we'll use later on
var handler = function(now) {
  console.log('Now:', now);
};

// Create a new simple event emitter
var YourModule = new EE();

// Register our event handler on the 'tick' event
YourModule.on('tick', handler);

// Register a handler that will fire once
YourModule.once('tick', function(now) {
  console.log('Running once');
  console.log('Now:', now);
});

// Make it tick every 1000ms
// Every second, you will see 'Now: <date>' in your console
var ticker = setInterval(function() {
  YourModule.emit('tick', new Date());
}, 1000);

// Let's remove the listener after 5 seconds
setTimeout(function() {
  YourModule.off('tick', handler);
  // The logs will stop now
  
  // Let's clear the interval to allow a node.js copy-paste to shut down
  clearInterval(ticker);
}, 5000);

Functions

constructor

The module returns a constructor to initiate a new instance of the event emitter.

// Load the module
var EE = require('simple-ee');

// Initialize a bare event emitter
var instance = new EE();

// Attach all EE methods to an existing object
var existingObject = {};
EE(existingObject);

// Using the constructor inside another constructor
function customConstructor() {
  
  // Run it as a constructor on the 'this' reference
  // You could also claim 'this' is an existing object (so running 'EE(this)')
  EE.apply(this);
}

// Initializing it will attach all EE methods to the object
var customObject = new customConstructor();

ee.on

Attach a new event handler to an instance of EE

// This code should be run after the the constructor example

// Keep a reference to our handlers for later use
var exampleHandler = console.log.bind(console, '[example]:'),
    anyHandler     = console.log.bind(console, '[*]:');

// Listen for a named event
customObject.on('example', exampleHandler);

// Listens for any emitted event
customObject.on('*', anyHandler);

ee.once

You can also attach a handler that will run only once instead of all the time

// This code should be run after the ee.on example

// Attach once to a named event
customObject.once('rare', function() {
  console.log('[rare]: This should only occur once');  
});

ee.emit

Emit an event on an existing EE instance

// This code should be run after the ee.once example

// Emit an 'example' event
// Output:
//   [example]: Thu Apr 19 2018 13:24:35 GMT+0200 (CEST)
//   [*]: Thu Apr 19 2018 13:24:35 GMT+0200 (CEST)
customObject.emit('example', new Date());

// Emit an 'rare' event
// Output:
//   [rare]: This should only occur once
//   [*]: Thu Apr 19 2018 13:24:35 GMT+0200 (CEST)
customObject.emit('rare', new Date());

// Emit another 'rare' event
// Output:
//   [*]: Thu Apr 19 2018 13:24:35 GMT+0200 (CEST)
customObject.emit('rare', new Date());

ee.off

Remove a specific handler from an EE instance

// This code should be run after the ee.emit example

// Remove the example listener
customObject.off('example', exampleHandler);

// Emit again for this example
// Output:
//   [*]: Thu Apr 19 2018 13:24:35 GMT+0200 (CEST)
customObject.emit('example', new Date());

License

MIT (c) Finwo