initialsemitter v0.1.0
initialsemitter
Extending EventEmitter with the ability to emit initial values when registering new listeners.
Installation
npm install initialsemitter
API
var InitialsEmitter = require("initialsemitter");
var initials = new InitialsEmitter();
Class: InitialsEmitter
This class is extending node's events.EventEmitter by following methods:
setEventInitial(event: string, initial: function | Array | object)
Function to set an initial event value for the event event
.
This will execute all currently registered event listeners and all future
registered event listeners will be executed with the initial values.
When initial
is an Array, this array will be applied to the listener:
initials.set("event", [1, 2, 3]);
initials.on("event", function(a, b, c) {
console.log(a + b === c); // prints true (1 + 2 === 3)
});
When initial
is a function, this function will executed everytime an emitter
is registered and the returning value will be applied to the listener:
initials.set("event", function() {
return Date.now();
});
initial.on("event", function(date) {
console.log(date); // prints the current date
});
Otherwise the listeners will be called with the initial
function:
initials.set("event", 5);
initial.on("event", function(num) {
console.log(num * num); // prints 25
});
removeEventInitial(event: string)
Function to remove an initial event value
Static Method: InitialsEmitter.mixin(clas : function)
Function to mixin the InitialsEmitter into another class that already inherits another prototype.
License: MIT
8 years ago