Event Emitter Class is a generic event emitter from which to extend. It provides default methods that allows rules base transitioning between states.
npm install --production state-machine-class
# use "--production" to avoid installing unnecessary development dependencies
const EventEmitter = require("event-emitter-class");
export default class extends EventEmitter{
constructor(){
super();
//define methods here
}
}
new EventEmitter(events);
One may pass a pre-populate set of event handlers to an even emitter. See ## Instance Methods => ### Bulk.
Example:Constructor
const ee = new EventEmitter();
Cause EventEmitter to emit an event triggers all event handlers listening for specific events triggers all one-time handlers listening for specific events and remove them triggers _onmessage functon, if defined
Example:emit
ee.emit("number-event", 1);
- emitEvent
- dispatchEvent
- dispatch
Add EventListener to emitter
Example:on
const handler = function(payload){consolee.log(payload);};
ee.on("number-event", handler);
- addEventListener
Remove EventListener from emitter
Example:off
ee.off("number-event", handler);
Add EventListener to emitter that will only fire once
Example:once
const handler = function(payload){consolee.log(payload);};
ee.once("number-event", handler);
Remove EventListener from emitter
Example:offOnce
ee.offOnce("number-event", handler);
Remove EventListener from emitter
Example:bulk
ee.bulk({...});
Sets onmessage function
Example:bulk
ee.onmessage = (eventName, ...payload)=>{console.log(eventName,...payload)}
runs tests
Tests can be run with node test
, npm run test
, or npm test
Because test output in the standard TAP format, there are a number of existing utilities that exist to format the output
Example: HTML Output
npm test | npx tap-html --out ./tap-html.html
Example: Graphical Output
npm test | npx tap-nyan
17 -_-_-_-_-_-_-_-_-_,------,
0 -_-_-_-_-_-_-_-_-_| /\_/\
0 -_-_-_-_-_-_-_-_-^|__( ^ .^)
-_-_-_-_-_-_-_-_- "" ""
Pass!