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

nomatic-events

v3.0.0

Published

Fast, asynchronous, and regex-enabled event framework for Node.js

Downloads

842

Readme

nomatic-events

Greenkeeper GitHub Release npm Build Status Coverage Status Known Vulnerabilities dependencies Status devDependencies Status License

Installation

You can install from NPM by doing:

npm install --save nomatic-events

Usage

Basic

var events = require("nomatic-events");
var EventEmitter = events.EventEmitter;
var emitter = new EventEmitter();

// Supports RegExp for listeners 
var listener = emitter.on(/incoming/i, function(data) {
  console.log("data is now " + data); 
});
// `listener` is an EventListener, but you do not have to capture this.
 
emitter.emit("INCOMING", 42);

Advanced

class EventedObject extends EventEmitter {
    constructor() {
        // Call EventEmitter, set maxListeners to 20
        super(20);
        
        this.listenerExecuted = true;
    }
    
    handleIncoming(some, option, or, another) {
        // Variadic arguments supported
        this.emit("INCOMING", some, option, or, another);
    }
}

let evented = new EventedObject();
// Don't have to take every argument supplied by `emit`
evented.on(/IN/, function(some, option, or) {
    console.log(this);
    if (this.listeners) {
        console.log(some, option, or);
        this.listenerExecuted = true;
    } else {
        console.log("Bad context");
    }
});

evented.handleIncoming("See no evil", "Hear no evil", "Speak no evil", 42);
// "See no evil"
// "Hear no evil"
// "Speak no evil"

Async

You can also use AsyncEventEmitter and AsyncEventListener to handle Promise-based callbacks.

Example

var events = require("nomatic-events");
var AsyncEventEmitter = events.AsyncEventEmitter;
var emitter = new AsyncEventEmitter();
var fs = require('fs');
var files = null;
var listener = emitter.on(/incoming/i, function(data) {
    return new Promise((resolve, reject) => {
        fs.readdir(__dirname, (err, list) => {
            if (err) reject(err);
            files = list;
            resolve();
        });
    })
});
// `listener` is an AsyncEventListener, but you do not have to capture this.
 
// Executed asynchronously 
emitter.emit("INCOMING", 42).then(() => {
    // 'files' isn't null!
    console.log(files);
});
// 'files' will still be null until promise resolves

Testing

Pride is taken when developing tests, you can find unit tests for both EventEmitter and EventListener here.

TypeScript

This package is written in TypeScript and declaration files are added to the NPM package. However, you do not even need to know what TypeScript is in order to use this package, let alone install the compiler. NPM packages include the compiled project.