generate-js-events
v3.0.0
Published
A generator for EventEmitter, lets you create an objects that can emit events.
Downloads
77
Readme
Generate-JS-Events
Table of Contents
- EventEmitter
EventEmitter
A generator for EventEmitter, lets you create an objects that can emit events, or generate a new generator that inherits from EventEmitter.
Install:
$ npm install generate-js-events
new EventEmitter()
- return:
Object
A new object that inherits from EventEmitter.
Creates a new object that inherits from EventEmitter.
Example:
var EventEmitter = require('generate-js-events');
var myEmitter = new EventEmitter();
EventEmitter.generate(create)
- create
Function
Create method that gets called when creating a new object that inherits from EventEmitter. - return:
Generator
A new generator that inherits from EventEmitter.
Returns a new generator that inherits from EventEmitter.
Example:
var myGenerator = EventEmitter.generate(
/* create method */
function myGenerator() {
// my create code here
}
);
myGenerator.definePrototype(
{
// my descriptor here
},
{
// my prototype here
}
);
Class: Generation
A new generator that inherits from the generator that generated it using the EventEmitter.generate(create) method.
Class: Creation
A new object that inherits from the generator that created it using the new EventEmitter() method.
Creation.on(event, listener[, observer]
- event
String
Name of event. - listener
Function
Event handler function. - observer
Object
Object reference for binding. - return:
Object
This object.
Adds a 'listener' on 'event' to this EventEmitter instance.
Example:
/*
* Set lestener.
* NOTE: myEmitter.off('error') does not remove *this* listener.
*/
myEmitter.onerror = function (err) {
console.log(err);
}
/*
* Add listener.
*/
myEmitter.on('myevent', function(a, b, c) {
console.log(a, b, c);
});
Creation.once(event, listener[, observer])
event
String
Name of event.listener
Function
Event handler function.observer
Object
Object reference for binding.return:
Object
This object.
Adds a 'listener' on 'event' to this EventEmitter instance which is removed after one 'event'.
Example:
/*
* Add one-time listener.
*/
myEmitter.once('myobjectevent', function(event) {
console.log(event);
});
Creation.off([event][, listener][, observer])
- event
String
Name of event. - listener
Function
Event handler function. - observer
Object
Object reference for binding. - return:
Object
This object.
Removes a 'listener' on 'event', or all listeners on 'event', or all listeners from this EventEmitter instance.
Example:
/*
* Remove specific listener.
*/
function specific(event) {
console.log(event);
}
myEmitter.on('specific', specific);
myEmitter.off('specific', specific);
/*
* Remove all listeners on same event.
*/
myEmitter.off('same');
/*
* Remove All listeners.
*/
myEmitter.off();
Creation.emit(event, [...args])
- event
String
Name of event. - args
Arguments
All otherarguments
to be emitted to listeners. - return:
Object
This object.
Emits an 'event' with 'args' on this EventEmitter instance.
Example:
/*
* Emits 1, 2, 3 on 'myevent' listener(s).
*/
myEmitter.emit('myevent', 1, 2, 3);
Creation.emitEvent(event, eventObject)
- event
String
Name of event. - eventObject
Object
An event object to be emitted to listeners. - return:
Object
This object.
Emits an event object containing 'eventObject' on this EventEmitter instance.
Example:
/*
* Emits {eventData: 'data'} on 'myobjectevent' listener(s).
*/
myEmitter.emitEvent('myobjectevent', {eventData: 'data'});