3.0.0 • Published 6 years ago

nomatic-events v3.0.0

Weekly downloads
28
License
ISC
Repository
github
Last release
6 years ago

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.

3.0.0

6 years ago

2.3.0

6 years ago

2.2.1

6 years ago

2.2.0

6 years ago

2.1.1

7 years ago

2.1.0

7 years ago

2.0.1

7 years ago

2.0.0

7 years ago

2.0.0-alpha.2

7 years ago

2.0.0-alpha.1

7 years ago

2.0.0-alpha.0

7 years ago

1.1.1

7 years ago

1.1.0

7 years ago

1.0.4

7 years ago

1.0.3

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago

1.0.0-rc.0

7 years ago

0.3.4

7 years ago

0.3.3

7 years ago

0.3.2

7 years ago

0.3.1

7 years ago

0.3.0

7 years ago

0.3.0-rc.1

7 years ago

0.3.0-rc.0

7 years ago

0.2.1

8 years ago

0.2.0

8 years ago

0.1.1

8 years ago

0.1.0

8 years ago

0.0.0

8 years ago