- Emitter([selection], target)
Applies the Emitter.js API to the target.
- APIReference :
string
|Object
A set of method references to the Emitter.js API.
- EventListener :
Function
A function bound to an emitter event type. Any data transmitted with the event will be passed into the listener as arguments.
- EventMapping :
Object
An object that maps event types to event listeners.
- EventType :
string
|symbol
A string or symbol that represents the type of event fired by the Emitter.
- Array
JavaScript Array
- boolean
JavaScript primitive boolean
- Error
JavaScript Error
- Function
JavaScript Function
- number
JavaScript primitive number
- null
JavaScript null
- Object
JavaScript Object
- Promise
JavaScript Promise
- string
JavaScript primitive string
- symbol
JavaScript primitive symbol
Emitter ⇐ Null
An object that emits named events which cause functions to be executed.
Kind: global class
Extends: Null
Mixes: asEmitter
See: https://github.com/nodejs/node/blob/master/lib/events.js
Since: 1.0.0
- Emitter ⇐
Null
- new Emitter([mapping])
- instance
- .maxListeners :
number
- .destroy()
- .toJSON() ⇒
Object
- .toString() ⇒
string
- .at([type], index, listener) ⇒
Emitter
- .clear([type]) ⇒
Emitter
- .emit(type, [...data]) ⇒
boolean
- .eventTypes() ⇒
Array.<EventType>
- .first(type, listener) ⇒
Emitter
- .getMaxListeners() ⇒
number
- .listenerCount(type) ⇒
number
- .listeners(type) ⇒
number
- .many(type, times, listener) ⇒
Emitter
- .off(type, listener) ⇒
Emitter
- .on([type], listener) ⇒
Emitter
- .once([type], listener) ⇒
Emitter
- .setMaxListeners(max) ⇒
Emitter
- .tick(type, [...data]) ⇒
Promise
- .trigger([type], data) ⇒
boolean
- .until([type], listener) ⇒
Emitter
- ":destroy"
- ":off"
- ":on"
- ":maxListeners"
- .maxListeners :
- static
- inner
- ~Null ⇐
null
- ~asEmitter
- .at([type], index, listener) ⇒
Emitter
- .clear([type]) ⇒
Emitter
- .emit(type, [...data]) ⇒
boolean
- .eventTypes() ⇒
Array.<EventType>
- .first(type, listener) ⇒
Emitter
- .getMaxListeners() ⇒
number
- .listenerCount(type) ⇒
number
- .listeners(type) ⇒
number
- .many(type, times, listener) ⇒
Emitter
- .off(type, listener) ⇒
Emitter
- .on([type], listener) ⇒
Emitter
- .once([type], listener) ⇒
Emitter
- .setMaxListeners(max) ⇒
Emitter
- .tick(type, [...data]) ⇒
Promise
- .trigger([type], data) ⇒
boolean
- .until([type], listener) ⇒
Emitter
- .at([type], index, listener) ⇒
- ~addConditionalEventListener(emitter, type, listener)
- ~addEventListener(emitter, type, listener, index)
- ~addFiniteEventListener(emitter, type, times, listener)
- ~addEventMapping(emitter, mapping)
- ~defineEventsProperty(emitter)
- ~emitAllEvents(emitter, type, data) ⇒
boolean
- ~emitErrors(emitter, errors)
- ~emitEvent(emitter, type, data, emitEvery) ⇒
boolean
- ~executeListener(listener, data, scope)
- ~getEventTypes(emitter) ⇒
Array.<EventType>
- ~getMaxListeners(emitter) ⇒
number
- ~isPositiveNumber(number) ⇒
boolean
- ~listenEmpty(handler, isFunction, emitter)
- ~listenOne(handler, isFunction, emitter, arg1)
- ~listenTwo(handler, isFunction, emitter, arg1, arg2)
- ~listenThree(handler, isFunction, emitter, arg1, arg2, arg3)
- ~listenMany(handler, isFunction, emitter, args)
- ~removeEventListener(emitter, type, listener)
- ~setMaxListeners(The, max)
- ~spliceList(list, index)
- ~tick(callback)
- ~tickAllEvents(emitter, type, data) ⇒
Promise
- ~toEmitter([selection], target)
- ~Null ⇐
Creates an instance of emitter. If mapping
are provided they will automatically be passed into on()
once construction is complete.
Param | Type | Description |
---|---|---|
[mapping] | EventMapping |
A mapping of event types to event listeners. |
Example (Using Emitter directly)
const greeter = new Emitter();
greeter.on( 'hello', () => console.log( 'Hello!' ) );
greeter.emit( 'hello' );
// Hello!
Example (Extending Emitter using Classical inheritance)
class Greeter extends Emitter {
constructor(){
super();
this.on( 'greet', ( name ) => console.log( `Hello, ${ name }!` ) );
}
greet( name ){
this.emit( 'greet', name );
}
}
const greeter = new Greeter();
greeter.greet( 'Jeff' );
// Hello, Jeff!
Example (Extending Emitter using Prototypal inheritance)
function Greeter(){
Emitter.call( this );
this.on( 'greet', ( name ) => console.log( `Hello, ${ name }!` ) );
}
Greeter.prototype = Object.create( Emitter.prototype );
Greeter.prototype.greet = function( name ){
this.emit( 'greet', name );
};
const greeter = new Greeter();
greeter.greet( 'Jeff' );
// Hello, Jeff!
Example (Namespaced events)
const greeter = new Emitter();
greeter.on( 'greeting:hello', ( name ) => console.log( `Hello, ${ name }!` ) );
greeter.on( 'greeting:hi', ( name ) => console.log( `Hi, ${ name }!` ) );
greeter.on( 'greeting', ( name ) => console.log( `${ name } was greeted.` );
greeter.emit( 'greeting:hi', 'Mark' );
greeter.emit( 'greeting:hello', 'Jeff' );
// Hi, Mark!
// Mark was greeted.
// Hello, Jeff!
// Jeff was greeted.
Example (Predefined events)
const greetings = {
hello: function( name ){ console.log( `Hello, ${name}!` ),
hi: function( name ){ console.log( `Hi, ${name}!` )
},
greeter = new Emitter( greetings );
greeter.emit( 'hello', 'Aaron' );
// Hello, Aaron!
Example (One-time events)
const greeter = new Emitter();
greeter.once( 'hello', ( name ) => console.log( `Hello, ${ name }!` ) );
greeter.emit( 'hello', 'Jeff' );
greeter.emit( 'hello', 'Terry' );
// Hello, Jeff!
Example (Many-time events)
const greeter = new Emitter();
greeter.many( 'hello', 2, ( name ) => console.log( `Hello, ${ name }!` ) );
greeter.emit( 'hello', 'Jeff' ); // 1
greeter.emit( 'hello', 'Terry' ); // 2
greeter.emit( 'hello', 'Steve' ); // 3
// Hello, Jeff!
// Hello, Terry!
emitter.maxListeners : number
By default Emitters will emit a :maxListeners
event if more than 10 listeners are added for a particular event type
. This property allows that to be changed. Set to 0 for unlimited.
Kind: instance property of Emitter
Since: 1.0.0
Example
const greeter = new Emitter();
console.log( greeter.maxListeners );
// 10
greeter.maxListeners = 1;
greeter.on( ':maxListeners', ( greeting ) => console.log( `Greeting "${ greeting }" has one too many!` ) );
greeter.on( 'hello', () => console.log( 'Hello!' ) );
greeter.on( 'hello', () => alert( 'Hello!' ) );
// Greeting "hello" has one too many!
Destroys the emitter.
Kind: instance method of Emitter
Emits: :destroy
Since: 1.0.0
emitter.toJSON() ⇒ Object
Kind: instance method of Emitter
Returns: Object
- An plain object representation of the emitter.
Since: 1.3.0
Example
const greeter = new Emitter();
greeter.maxListeners = 5;
greeter.on( 'greet', ( name ) => console.log( `Hello, ${ name }!` ) );
greeter.on( 'greet', ( name ) => console.log( `Hi, ${ name }!` ) );
console.log( greeter.toJSON() );
// { "maxListeners": 5, "listenerCount": { "greet": 2 } }
greeter.destroy();
console.log( greeter.toJSON() );
// "destroyed"
emitter.toString() ⇒ string
Kind: instance method of Emitter
Returns: string
- A string representation of the emitter.
Since: 1.3.0
Example
const greeter = new Emitter();
greeter.maxListeners = 5;
greeter.on( 'greet', ( name ) => console.log( `Hello, ${ name }!` ) );
greeter.on( 'greet', ( name ) => console.log( `Hi, ${ name }!` ) );
console.log( greeter.toString() );
// 'Emitter { "maxListeners": 5, "listenerCount": { "greet": 2 } }'
greeter.destroy();
console.log( greeter.toString() );
// 'Emitter "destroyed"'
emitter.at([type], index, listener) ⇒ Emitter
Adds a listener for the specified event type
at the specified index
. If no type
is given the listener will be triggered any event type
.
No checks are made to see if the listener
has already been added. Multiple calls passing the same combination type
and listener
will result in the listener
being added multiple times.
Kind: instance method of Emitter
Mixes: at
Returns: Emitter
- The emitter.
Emits: :on
, :maxListeners
Since: 2.0.0
Param | Type | Description |
---|---|---|
[type] | EventType |
The event type. |
index | number |
Where the listener will be added in the trigger list. |
listener | EventListener |
The event callback. |
emitter.clear([type]) ⇒ Emitter
Remove all listeners, or those for the specified event type
.
Kind: instance method of Emitter
Mixes: clear
Returns: Emitter
- The emitter.
Since: 1.0.0
Param | Type | Description |
---|---|---|
[type] | String |
The event type. |
Example (Clearing all event types)
const greeter = new Emitter();
greeter.on( 'hello', () => console.log( 'Hello!' ) );
greeter.on( 'hi', () => console.log( 'Hi!' ) );
greeter.emit( 'hello' );
// Hello!
greeter.emit( 'hi' );
// Hi!
greeter.clear();
greeter.emit( 'hello' );
greeter.emit( 'hi' );
Example (Clearing a specified event type)
const greeter = new Emitter();
greeter.on( {
'hello' : function(){ console.log( 'Hello!' ); },
'hi' : function(){ console.log( 'Hi!' ); }
} );
greeter.emit( 'hello' );
// Hello!
greeter.emit( 'hi' );
// Hi!
greeter.clear( 'hello' );
greeter.emit( 'hello' );
greeter.emit( 'hi' );
// Hi!
emitter.emit(type, [...data]) ⇒ boolean
Execute the listeners for the specified event type
with the supplied arguments.
The type
can be namespaced using :
, which will result in multiple events being triggered in succession. Listeners can be associated with the fully namespaced type
or a subset of the type
.
Returns true
if the event had listeners, false
otherwise.
Kind: instance method of Emitter
Mixes: emit
Returns: boolean
- Whether or not the event had listeners.
Since: 1.0.0
Param | Type | Description |
---|---|---|
type | EventType |
The event type. |
[...data] | * |
The data passed into the listeners. |
Example (Emitting an event)
const greeter = new Emitter();
greeter.on( 'hello', () => console.log( 'Hello!' ) );
greeter.emit( 'hello' ); // true
// Hello!
greeter.emit( 'goodbye' ); // false
Example (Emitting an event with data)
const greeter = new Emitter();
greeter.on( 'hello', ( name ) => console.log( `Hello, ${ name }!` ) );
greeter.emit( 'hello', 'World' );
// Hello, World!
Example (Emitting a namespaced event)
const greeter = new Emitter();
greeter.on( 'greeting:hello', ( name ) => console.log( `Hello, ${ name }!` ) );
greeter.on( 'greeting:hi', ( name ) => console.log( `Hi, ${ name }!` ) );
greeter.on( 'greeting', ( name ) => console.log( `${ name } was greeted.` );
// This event will not be triggered by emitting "greeting:hello"
greeter.on( 'hello', ( name ) => console.log( `Hello again, ${ name }` );
greeter.emit( 'greeting:hi', 'Mark' );
// Hi, Mark!
// Mark was greeted.
greeter.emit( 'greeting:hello', 'Jeff' );
// Hello, Jeff!
// Jeff was greeted.
emitter.eventTypes() ⇒ Array.<EventType>
Kind: instance method of Emitter
Mixes: eventTypes
Returns: Array.<EventType>
- The list of event types registered to the emitter.
Since: 2.0.0
Example
const greeter = new Emitter();
greeter.on( 'hello', () => console.log( `Hello` ) );
greeter.on( 'hi', () => console.log( `Hi` ) );
console.log( greeter.eventTypes() );
// [ 'hello', 'hi' ]
emitter.first(type, listener) ⇒ Emitter
Kind: instance method of Emitter
Mixes: first
Returns: Emitter
- The emitter.
Since: 2.0.0
Param | Type | Description |
---|---|---|
type | EventType |
The event type. |
listener | EventListener |
The event callback. |
emitter.getMaxListeners() ⇒ number
By default Emitter will emit a :maxListeners
evet if more than 10 listeners are added for a particular event type
. This method returns the current value.
Kind: instance method of Emitter
Mixes: getMaxListeners
Returns: number
- The maximum number of listeners.
Since: 2.0.0
Example
const greeter = new Emitter();
console.log( greeter.getMaxListeners() );
// 10
greeter.setMaxListeners( 5 );
console.log( greeter.getMaxListeners() );
// 5
emitter.listenerCount(type) ⇒ number
Kind: instance method of Emitter
Mixes: listenerCount
Returns: number
- The number of listeners for that event type within the given emitter.
Since: 1.0.0
Param | Type | Description |
---|---|---|
type | EventType |
The event type. |
Example
const greeter = new Emitter();
greeter.on( 'hello', () => console.log( 'Hello!' ) );
console.log( greeter.listenerCount( 'hello' ) );
// 1
console.log( greeter.listenerCount( 'goodbye' ) );
// 0
emitter.listeners(type) ⇒ number
Kind: instance method of Emitter
Mixes: listeners
Returns: number
- The number of listeners for that event type within the given emitter.
Since: 1.0.0
Param | Type | Description |
---|---|---|
type | EventType |
The event type. |
Example
const hello = function(){
console.log( 'Hello!' );
},
greeter = new Emitter();
greeter.on( 'hello', hello );
greeter.emit( 'hello' );
// Hello!
console.log( greeter.listeners( 'hello' )[ 0 ] === hello );
// true
emitter.many(type, times, listener) ⇒ Emitter
Adds a many time listener for the specified event type
. If no type
is given the listener will be triggered any event type
. After the listener is invoked the specified number of times
, it is removed.
No checks are made to see if the listener
has already been added. Multiple calls passing the same combination type
and listener
will result in the listener
being added multiple times.
Kind: instance method of Emitter
Mixes: many
Returns: Emitter
- The emitter.
Since: 1.0.0
Param | Type | Description |
---|---|---|
type | EventType |
The event type. |
times | number |
The number times the listener will be executed before being removed. |
listener | EventListener |
The event callback. |
Example (Listen to any event type a set number of times)
const greeter = new Emitter();
greeter.many( 2, ( name ) => console.log( `Greeted ${ name }` ) );
greeter.emit( 'hello', 'Jeff' ); // 1
// Greeted Jeff
greeter.emit( 'hi', 'Terry' ); // 2
// Greeted Terry
greeter.emit( 'yo', 'Steve' ); // 3
Example (Listen to the specified event type a set number of times)
const greeter = new Emitter();
greeter.many( 'hello', 2, ( name ) => console.log( `Hello, ${ name }!` ) );
greeter.emit( 'hello', 'Jeff' ); // 1
// Hello, Jeff!
greeter.emit( 'hello', 'Terry' ); // 2
// Hello, Terry!
greeter.emit( 'hello', 'Steve' ); // 3
emitter.off(type, listener) ⇒ Emitter
Removes the listener
for the specified event type
. If no type
is given it is assumed the listener
is not associated with a specific type
.
If any single listener has been added multiple times for the specified type
, then emitter.off()
must be called multiple times to remove each instance.
Kind: instance method of Emitter
Mixes: off
Returns: Emitter
- The emitter.
Emits: :off
Since: 1.0.0
Param | Type | Description |
---|---|---|
type | EventType |
The event type. |
listener | EventListener |
The event callback. |
Example (Remove a listener from any event type)
function greet( name ){
console.log( `Greetings, ${ name }!` );
}
const greeter = new Emitter();
greeter.on( greet );
greeter.emit( 'hello' 'Jeff' );
// Greetings, Jeff!
greeter.emit( 'hi' 'Jeff' );
// Greetings, Jeff!
greeter.off( greet );
greeter.emit( 'yo', 'Jeff' );
Example (Remove a listener from a specified event type)
function hello( name ){
console.log( `Hello, ${ name }!` );
}
const greeter = new Emitter();
greeter.on( 'hello', hello );
greeter.emit( 'hello', 'Jeff' );
// Hello, Jeff!
greeter.off( 'hello', hello );
greeter.emit( 'hello', 'Jeff' );
emitter.on([type], listener) ⇒ Emitter
Adds a listener for the specified event type
. If no type
is given the listener will be triggered any event type
.
No checks are made to see if the listener
has already been added. Multiple calls passing the same combination type
and listener
will result in the listener
being added multiple times.
Kind: instance method of Emitter
Mixes: on
Returns: Emitter
- The emitter.
Emits: :on
, :maxListeners
Since: 1.0.0
Param | Type | Description |
---|---|---|
[type] | EventType |
The event type. |
listener | EventListener |
The event callback. |
Example (Listen to all event types)
const greeter = new Emitter();
greeter.on( () => console.log( 'Greeted' ) );
greeter.emit( 'hello' );
// Greeted
greeter.emit( 'goodbye' );
// Greeted
Example (Listener to a specified event type)
const greeter = new Emitter();
greeter.on( 'hello', ( name ) => console.log( `Hello, ${ name }!` ) );
greeter.emit( 'hello', 'World' );
// Hello, World!
greeter.emit( 'hi', 'World' );
emitter.once([type], listener) ⇒ Emitter
Kind: instance method of Emitter
Mixes: once
Returns: Emitter
- The emitter.
Emits: :on
, Emitter#:maxListeners
const greeter = new Emitter();
greeter.once( () => console.log( 'Greeted' ) );
greeter.emit( 'hello' );
// Greeted
greeter.emit( 'goodbye' );event:
Since: 1.0.0
Param | Type | Description |
---|---|---|
[type] | EventType |
The event type. |
listener | EventListener |
The event callback. |
Example (Listen once to all event types)
const greeter = new Emitter();
greeter.once( 'hello', ( name ) => console.log( `Hello, ${ name }!` ) );
greeter.emit( 'hello', 'World' );
// Hello, World!
greeter.emit( 'hello', 'World' );
emitter.setMaxListeners(max) ⇒ Emitter
By default Emitter will emit a :maxListeners
evet if more than 10 listeners are added for a particular event type
. This method allows that to be changed. Set to 0 for unlimited.
Kind: instance method of Emitter
Mixes: setMaxListeners
Returns: Emitter
- The emitter.
Since: 2.0.0
Param | Type | Description |
---|---|---|
max | number |
The maximum number of listeners before a warning is issued. |
Example
const greeter = new Emitter();
greeter.setMaxListeners( 1 );
greeter.on( ':maxListeners', ( greeting ) => console.log( `Greeting "${ greeting }" has one too many!` ) );
greeter.on( 'hello', () => console.log( 'Hello!' ) );
greeter.on( 'hello', () => alert( 'Hello!' ) );
// Greeting "hello" has one too many!
emitter.tick(type, [...data]) ⇒ Promise
Asynchronously emits specified event type
with the supplied arguments. The listeners will still be synchronously executed in the specified order.
The type
can be namespaced using :
, which will result in multiple events being triggered in succession. Listeners can be associated with the fully namespaced type
or a subset of the type
.
Returns promise which resolves if the event had listeners, rejects otherwise.
Kind: instance method of Emitter
Mixes: tick
Returns: Promise
- A promise which resolves if the event had listeners, rejects otherwise.
Since: 2.0.0
Param | Type | Description |
---|---|---|
type | EventType |
The event type. |
[...data] | * |
The data passed into the listeners. |
Example (Asynchronously emitting an event)
const greeter = new Emitter();
greeter.on( 'hello', () => console.log( 'Hello!' ) );
greeter.tick( 'hello' ).then( ( heard ) => console.log( 'hello heard? ', heard ) );
greeter.tick( 'goodbye' ).then( ( heard ) => console.log( 'goodbye heard? ', heard ) );
// Hello!
// hello heard? true
// goodbye heard? false
emitter.trigger([type], data) ⇒ boolean
Execute the listeners for the specified event type
with the supplied data
.
Returns true
if the event had listeners, false
otherwise.
Kind: instance method of Emitter
Mixes: trigger
Returns: boolean
- Whether or not the event had listeners.
Since: 1.0.0
Param | Type | Description |
---|---|---|
[type] | EventType |
The event type. |
data | Array |
Example
const greeter = new Emitter();
greeter.on( 'hello', ( name ) => console.log( `Hello, ${ name }!` ) );
greeter.trigger( 'hello', [ 'World' ] );
// Hello, World!
Example
const greeter = new Emitter();
greeter.on( 'greeting:hello', ( name ) => console.log( `Hello, ${ name }!` ) );
greeter.on( 'greeting:hi', ( name ) => console.log( `Hi, ${ name }!` ) );
greeter.on( 'greeting', ( name ) => console.log( `${ name } was greeted.` );
greeter.trigger( 'greeting:hi', [ 'Mark' ] );
// Hi, Mark!
// Mark was greeted.
greeter.trigger( 'greeting:hello', [ 'Jeff' ] );
// Hello, Jeff!
// Jeff was greeted.
emitter.until([type], listener) ⇒ Emitter
Adds a listeners for the specified event type
that will be triggered until the listener
returns true
. If no type
is given the listener will be triggered any event type
.
No checks are made to see if the listener
has already been added. Multiple calls passing the same combination type
and listener
will result in the listener
being added multiple times.
Kind: instance method of Emitter
Mixes: until
Returns: Emitter
- The emitter.
Since: 1.2.0
Param | Type | Description |
---|---|---|
[type] | EventType |
The event type. |
listener | EventListener |
The event callback. |
Example
const greeter = new Emitter();
greeter.until( function( name ){
console.log( `Greeted ${ name }` );
return name === 'Terry';
} );
greeter.emit( 'hello', 'Jeff' );
// Greeted Jeff
greeter.emit( 'goodbye', 'Terry' );
// Greeted Terry
greeter.emit( 'hi', 'Aaron' );
Example
const greeter = new Emitter();
greeter.until( 'hello', function( name ){
console.log( `Hello, ${ name }!` );
return name === 'World';
} );
greeter.emit( 'hello', 'Jeff' );
// Hello, Jeff!
greeter.emit( 'hello', 'World' );
// Hello, World!
greeter.emit( 'hello', 'Mark' );
This event is emitted before an emitter destroys itself.
Kind: event emitted by Emitter
This event is emitted after a listener is removed.
Kind: event emitted by Emitter
This event is emitted before a listener is added.
Kind: event emitted by Emitter
This event is emitted once the maximum number of listeners has been exceeded for an event type.
Kind: event emitted by Emitter
Emitter.defaultMaxListeners : number
Sets the default maximum number of listeners for all emitters. Use emitter.maxListeners
to set the maximum on a per-instance basis.
By default Emitter will emit a :maxListeners
event if more than 10 listeners are added to a specific event type.
Kind: static property of Emitter
Default: 10
Since: 1.0.0
Example (Changing the default maximum listeners)
console.log( Emitter.defaultMaxListeners );
// 10
const greeter1 = new Emitter(),
greeter2 = new Emitter();
Emitter.defaultMaxListeners = 1;
greeter1.on( ':maxListeners', ( greeting ) => console.log( `Greeting "${ greeting }" has one too many!` ) );
greeter1.on( 'hello', () => console.log( 'Hello!' ) );
greeter1.on( 'hello', () => alert( 'Hello!' ) );
// Greeting "hello" has one too many!
greeter2.on( ':maxListeners', ( greeting ) => console.log( `Greeting "${ greeting }" has one too many!` ) );
greeter2.on( 'hi', () => console.log( 'Hi!' ) );
greeter2.on( 'hi', () => alert( 'Hi!' ) );
// Greeting "hi" has one too many!
Emitter.every : symbol
An id used to listen for events of any type
. For most methods, when no type
is given this is the default.
Listener bound to every event will not execute for Emitter lifecycle events, like :maxListeners
.
Using Emitter.every
is typically not necessary.
Kind: static property of Emitter
Since: 1.0.0
Example
const greeter = new Emitter();
greeter.on( Emitter.every, () => console.log( 'Greeted' ) );
greeter.emit( 'hello' );
// Greeted
greeter.emit( 'goodbye' );
// Greeted
Emitter.version : string
The current version of Emitter.js.
Kind: static property of Emitter
Since: 1.1.2
Example
console.log( Emitter.version );
// 2.0.0
Emitter~Null ⇐ null
Kind: inner class of Emitter
Extends: null
A "clean", empty container. Instantiating this is faster than explicitly calling Object.create( null )
.
A functional mixin that provides the Emitter.js API to its target. The constructor()
, destroy()
, toJSON()
, toString()
, and static properties on Emitter
are not provided. This mixin is used to populate the prototype
of Emitter
.
Like all functional mixins, this should be executed with call() or apply().
Kind: inner mixin of Emitter
Since: 1.1.0
Example (Applying Emitter functionality)
// Create a base object
const greeter = Object.create( null );
// Apply the mixin
asEmitter.call( greeter );
greeter.on( 'hello', ( name ) => console.log( `Hello, ${ name }!` ) );
greeter.emit( 'hello', 'World' );
// Hello, World!
Example (Applying chaos to your world)
// NO!!!
asEmitter(); // Madness ensues
- ~asEmitter
- .at([type], index, listener) ⇒
Emitter
- .clear([type]) ⇒
Emitter
- .emit(type, [...data]) ⇒
boolean
- .eventTypes() ⇒
Array.<EventType>
- .first(type, listener) ⇒
Emitter
- .getMaxListeners() ⇒
number
- .listenerCount(type) ⇒
number
- .listeners(type) ⇒
number
- .many(type, times, listener) ⇒
Emitter
- .off(type, listener) ⇒
Emitter
- .on([type], listener) ⇒
Emitter
- .once([type], listener) ⇒
Emitter
- .setMaxListeners(max) ⇒
Emitter
- .tick(type, [...data]) ⇒
Promise
- .trigger([type], data) ⇒
boolean
- .until([type], listener) ⇒
Emitter
- .at([type], index, listener) ⇒
asEmitter.at([type], index, listener) ⇒ Emitter
Adds a listener for the specified event type
at the specified index
. If no type
is given the listener will be triggered any event type
.
No checks are made to see if the listener
has already been added. Multiple calls passing the same combination type
and listener
will result in the listener
being added multiple times.
Kind: static method of asEmitter
Returns: Emitter
- The emitter.
Emits: :on
, :maxListeners
Since: 2.0.0
Param | Type | Description |
---|---|---|
[type] | EventType |
The event type. |
index | number |
Where the listener will be added in the trigger list. |
listener | EventListener |
The event callback. |
asEmitter.clear([type]) ⇒ Emitter
Remove all listeners, or those for the specified event type
.
Kind: static method of asEmitter
Returns: Emitter
- The emitter.
Since: 1.0.0
Param | Type | Description |
---|---|---|
[type] | String |
The event type. |
Example (Clearing all event types)
const greeter = new Emitter();
greeter.on( 'hello', () => console.log( 'Hello!' ) );
greeter.on( 'hi', () => console.log( 'Hi!' ) );
greeter.emit( 'hello' );
// Hello!
greeter.emit( 'hi' );
// Hi!
greeter.clear();
greeter.emit( 'hello' );
greeter.emit( 'hi' );
Example (Clearing a specified event type)
const greeter = new Emitter();
greeter.on( {
'hello' : function(){ console.log( 'Hello!' ); },
'hi' : function(){ console.log( 'Hi!' ); }
} );
greeter.emit( 'hello' );
// Hello!
greeter.emit( 'hi' );
// Hi!
greeter.clear( 'hello' );
greeter.emit( 'hello' );
greeter.emit( 'hi' );
// Hi!
asEmitter.emit(type, [...data]) ⇒ boolean
Execute the listeners for the specified event type
with the supplied arguments.
The type
can be namespaced using :
, which will result in multiple events being triggered in succession. Listeners can be associated with the fully namespaced type
or a subset of the type
.
Returns true
if the event had listeners, false
otherwise.
Kind: static method of asEmitter
Returns: boolean
- Whether or not the event had listeners.
Since: 1.0.0
Param | Type | Description |
---|---|---|
type | EventType |
The event type. |
[...data] | * |
The data passed into the listeners. |
Example (Emitting an event)
const greeter = new Emitter();
greeter.on( 'hello', () => console.log( 'Hello!' ) );
greeter.emit( 'hello' ); // true
// Hello!
greeter.emit( 'goodbye' ); // false
Example (Emitting an event with data)
const greeter = new Emitter();
greeter.on( 'hello', ( name ) => console.log( `Hello, ${ name }!` ) );
greeter.emit( 'hello', 'World' );
// Hello, World!
Example (Emitting a namespaced event)
const greeter = new Emitter();
greeter.on( 'greeting:hello', ( name ) => console.log( `Hello, ${ name }!` ) );
greeter.on( 'greeting:hi', ( name ) => console.log( `Hi, ${ name }!` ) );
greeter.on( 'greeting', ( name ) => console.log( `${ name } was greeted.` );
// This event will not be triggered by emitting "greeting:hello"
greeter.on( 'hello', ( name ) => console.log( `Hello again, ${ name }` );
greeter.emit( 'greeting:hi', 'Mark' );
// Hi, Mark!
// Mark was greeted.
greeter.emit( 'greeting:hello', 'Jeff' );
// Hello, Jeff!
// Jeff was greeted.
asEmitter.eventTypes() ⇒ Array.<EventType>
Kind: static method of asEmitter
Returns: Array.<EventType>
- The list of event types registered to the emitter.
Since: 2.0.0
Example
const greeter = new Emitter();
greeter.on( 'hello', () => console.log( `Hello` ) );
greeter.on( 'hi', () => console.log( `Hi` ) );
console.log( greeter.eventTypes() );
// [ 'hello', 'hi' ]
asEmitter.first(type, listener) ⇒ Emitter
Kind: static method of asEmitter
Returns: Emitter
- The emitter.
Since: 2.0.0
Param | Type | Description |
---|---|---|
type | EventType |
The event type. |
listener | EventListener |
The event callback. |
asEmitter.getMaxListeners() ⇒ number
By default Emitter will emit a :maxListeners
evet if more than 10 listeners are added for a particular event type
. This method returns the current value.
Kind: static method of asEmitter
Returns: number
- The maximum number of listeners.
Since: 2.0.0
Example
const greeter = new Emitter();
console.log( greeter.getMaxListeners() );
// 10
greeter.setMaxListeners( 5 );
console.log( greeter.getMaxListeners() );
// 5
asEmitter.listenerCount(type) ⇒ number
Kind: static method of asEmitter
Returns: number
- The number of listeners for that event type within the given emitter.
Since: 1.0.0
Param | Type | Description |
---|---|---|
type | EventType |
The event type. |
Example
const greeter = new Emitter();
greeter.on( 'hello', () => console.log( 'Hello!' ) );
console.log( greeter.listenerCount( 'hello' ) );
// 1
console.log( greeter.listenerCount( 'goodbye' ) );
// 0
asEmitter.listeners(type) ⇒ number
Kind: static method of asEmitter
Returns: number
- The number of listeners for that event type within the given emitter.
Since: 1.0.0
Param | Type | Description |
---|---|---|
type | EventType |
The event type. |
Example
const hello = function(){
console.log( 'Hello!' );
},
greeter = new Emitter();
greeter.on( 'hello', hello );
greeter.emit( 'hello' );
// Hello!
console.log( greeter.listeners( 'hello' )[ 0 ] === hello );
// true
asEmitter.many(type, times, listener) ⇒ Emitter
Adds a many time listener for the specified event type
. If no type
is given the listener will be triggered any event type
. After the listener is invoked the specified number of times
, it is removed.
No checks are made to see if the listener
has already been added. Multiple calls passing the same combination type
and listener
will result in the listener
being added multiple times.
Kind: static method of asEmitter
Returns: Emitter
- The emitter.
Since: 1.0.0
Param | Type | Description |
---|---|---|
type | EventType |
The event type. |
times | number |
The number times the listener will be executed before being removed. |
listener | EventListener |
The event callback. |
Example (Listen to any event type a set number of times)
const greeter = new Emitter();
greeter.many( 2, ( name ) => console.log( `Greeted ${ name }` ) );
greeter.emit( 'hello', 'Jeff' ); // 1
// Greeted Jeff
greeter.emit( 'hi', 'Terry' ); // 2
// Greeted Terry
greeter.emit( 'yo', 'Steve' ); // 3
Example (Listen to the specified event type a set number of times)
const greeter = new Emitter();
greeter.many( 'hello', 2, ( name ) => console.log( `Hello, ${ name }!` ) );
greeter.emit( 'hello', 'Jeff' ); // 1
// Hello, Jeff!
greeter.emit( 'hello', 'Terry' ); // 2
// Hello, Terry!
greeter.emit( 'hello', 'Steve' ); // 3
asEmitter.off(type, listener) ⇒ Emitter
Removes the listener
for the specified event type
. If no type
is given it is assumed the listener
is not associated with a specific type
.
If any single listener has been added multiple times for the specified type
, then emitter.off()
must be called multiple times to remove each instance.
Kind: static method of asEmitter
Returns: Emitter
- The emitter.
Emits: :off
Since: 1.0.0
Param | Type | Description |
---|---|---|
type | EventType |
The event type. |
listener | EventListener |
The event callback. |
Example (Remove a listener from any event type)
function greet( name ){
console.log( `Greetings, ${ name }!` );
}
const greeter = new Emitter();
greeter.on( greet );
greeter.emit( 'hello' 'Jeff' );
// Greetings, Jeff!
greeter.emit( 'hi' 'Jeff' );
// Greetings, Jeff!
greeter.off( greet );
greeter.emit( 'yo', 'Jeff' );
Example (Remove a listener from a specified event type)
function hello( name ){
console.log( `Hello, ${ name }!` );
}
const greeter = new Emitter();
greeter.on( 'hello', hello );
greeter.emit( 'hello', 'Jeff' );
// Hello, Jeff!
greeter.off( 'hello', hello );
greeter.emit( 'hello', 'Jeff' );
asEmitter.on([type], listener) ⇒ Emitter
Adds a listener for the specified event type
. If no type
is given the listener will be triggered any event type
.
No checks are made to see if the listener
has already been added. Multiple calls passing the same combination type
and listener
will result in the listener
being added multiple times.
Kind: static method of asEmitter
Returns: Emitter
- The emitter.
Emits: :on
, :maxListeners
Since: 1.0.0
Param | Type | Description |
---|---|---|
[type] | EventType |
The event type. |
listener | EventListener |
The event callback. |
Example (Listen to all event types)
const greeter = new Emitter();
greeter.on( () => console.log( 'Greeted' ) );
greeter.emit( 'hello' );
// Greeted
greeter.emit( 'goodbye' );
// Greeted
Example (Listener to a specified event type)
const greeter = new Emitter();
greeter.on( 'hello', ( name ) => console.log( `Hello, ${ name }!` ) );
greeter.emit( 'hello', 'World' );
// Hello, World!
greeter.emit( 'hi', 'World' );
asEmitter.once([type], listener) ⇒ Emitter
Kind: static method of asEmitter
Returns: Emitter
- The emitter.
Emits: :on
, Emitter#:maxListeners
const greeter = new Emitter();
greeter.once( () => console.log( 'Greeted' ) );
greeter.emit( 'hello' );
// Greeted
greeter.emit( 'goodbye' );event:
Since: 1.0.0
Param | Type | Description |
---|---|---|
[type] | EventType |
The event type. |
listener | EventListener |
The event callback. |
Example (Listen once to all event types)
const greeter = new Emitter();
greeter.once( 'hello', ( name ) => console.log( `Hello, ${ name }!` ) );
greeter.emit( 'hello', 'World' );
// Hello, World!
greeter.emit( 'hello', 'World' );
asEmitter.setMaxListeners(max) ⇒ Emitter
By default Emitter will emit a :maxListeners
evet if more than 10 listeners are added for a particular event type
. This method allows that to be changed. Set to 0 for unlimited.
Kind: static method of asEmitter
Returns: Emitter
- The emitter.
Since: 2.0.0
Param | Type | Description |
---|---|---|
max | number |
The maximum number of listeners before a warning is issued. |
Example
const greeter = new Emitter();
greeter.setMaxListeners( 1 );
greeter.on( ':maxListeners', ( greeting ) => console.log( `Greeting "${ greeting }" has one too many!` ) );
greeter.on( 'hello', () => console.log( 'Hello!' ) );
greeter.on( 'hello', () => alert( 'Hello!' ) );
// Greeting "hello" has one too many!
asEmitter.tick(type, [...data]) ⇒ Promise
Asynchronously emits specified event type
with the supplied arguments. The listeners will still be synchronously executed in the specified order.
The type
can be namespaced using :
, which will result in multiple events being triggered in succession. Listeners can be associated with the fully namespaced type
or a subset of the type
.
Returns promise which resolves if the event had listeners, rejects otherwise.
Kind: static method of asEmitter
Returns: Promise
- A promise which resolves if the event had listeners, rejects otherwise.
Since: 2.0.0
Param | Type | Description |
---|---|---|
type | EventType |
The event type. |
[...data] | * |
The data passed into the listeners. |
Example (Asynchronously emitting an event)
const greeter = new Emitter();
greeter.on( 'hello', () => console.log( 'Hello!' ) );
greeter.tick( 'hello' ).then( ( heard ) => console.log( 'hello heard? ', heard ) );
greeter.tick( 'goodbye' ).then( ( heard ) => console.log( 'goodbye heard? ', heard ) );
// Hello!
// hello heard? true
// goodbye heard? false
asEmitter.trigger([type], data) ⇒ boolean
Execute the listeners for the specified event type
with the supplied data
.
Returns true
if the event had listeners, false
otherwise.
Kind: static method of asEmitter
Returns: boolean
- Whether or not the event had listeners.
Since: 1.0.0
Param | Type | Description |
---|---|---|
[type] | EventType |
The event type. |
data | Array |
Example
const greeter = new Emitter();
greeter.on( 'hello', ( name ) => console.log( `Hello, ${ name }!` ) );
greeter.trigger( 'hello', [ 'World' ] );
// Hello, World!
Example
const greeter = new Emitter();
greeter.on( 'greeting:hello', ( name ) => console.log( `Hello, ${ name }!` ) );
greeter.on( 'greeting:hi', ( name ) => console.log( `Hi, ${ name }!` ) );
greeter.on( 'greeting', ( name ) => console.log( `${ name } was greeted.` );
greeter.trigger( 'greeting:hi', [ 'Mark' ] );
// Hi, Mark!
// Mark was greeted.
greeter.trigger( 'greeting:hello', [ 'Jeff' ] );
// Hello, Jeff!
// Jeff was greeted.
asEmitter.until([type], listener) ⇒ Emitter
Adds a listeners for the specified event type
that will be triggered until the listener
returns true
. If no type
is given the listener will be triggered any event type
.
No checks are made to see if the listener
has already been added. Multiple calls passing the same combination type
and listener
will result in the listener
being added multiple times.
Kind: static method of asEmitter
Returns: Emitter
- The emitter.
Since: 1.2.0
Param | Type | Description |
---|---|---|
[type] | EventType |
The event type. |
listener | EventListener |
The event callback. |
Example
const greeter = new Emitter();
greeter.until( function( name ){
console.log( `Greeted ${ name }` );
return name === 'Terry';
} );
greeter.emit( 'hello', 'Jeff' );
// Greeted Jeff
greeter.emit( 'goodbye', 'Terry' );
// Greeted Terry
greeter.emit( 'hi', 'Aaron' );
Example
const greeter = new Emitter();
greeter.until( 'hello', function( name ){
console.log( `Hello, ${ name }!` );
return name === 'World';
} );
greeter.emit( 'hello', 'Jeff' );
// Hello, Jeff!
greeter.emit( 'hello', 'World' );
// Hello, World!
greeter.emit( 'hello', 'Mark' );
Kind: inner method of Emitter
Param | Type | Description |
---|---|---|
emitter | Emitter |
The emitter on which the event would be emitted. |
type | EventType |
The event type. |
listener | EventListener |
The event callback. |
Kind: inner method of Emitter
Param | Type | Description |
---|---|---|
emitter | Emitter |
The emitter on which the event would be emitted. |
type | EventType |
The event type. |
listener | EventListener |
The event callback. |
index | number |
Kind: inner method of Emitter
Param | Type | Description |
---|---|---|
emitter | Emitter |
The emitter on which the event would be emitted. |
type | EventType |
The event type. |
times | number |
The number times the listener will be executed before being removed. |
listener | EventListener |
The event callback. |
Kind: inner method of Emitter
Param | Type | Description |
---|---|---|
emitter | Emitter |
The emitter on which the event would be emitted. |
mapping | EventMapping |
The event mapping. |
Kind: inner method of Emitter
Param | Type | Description |
---|---|---|
emitter | Emitter |
The emitter on which the property will be created. |
Emitter~emitAllEvents(emitter, type, data) ⇒ boolean
Kind: inner method of Emitter
Returns: boolean
- Whether or not a listener for the given event type was executed.
Throws:
Error
Iftype
iserror
and no listeners are subscribed.
Param | Type | Description |
---|---|---|
emitter | Emitter |
The emitter on which the event type will be emitted. |
type | EventType |
The event type. |
data | Array |
The data to be passed with the event. |
Kind: inner method of Emitter
Param | Type | Description |
---|---|---|
emitter | Emitter |
The emitter on which the errors will be emitted. |
errors | Array.<Error> |
The array of errors to be emitted. |
Emitter~emitEvent(emitter, type, data, emitEvery) ⇒ boolean
Kind: inner method of Emitter
Returns: boolean
- Whether or not a listener for the given event type was executed.
Throws:
Error
Iftype
iserror
and no listeners are subscribed.
Param | Type | Description |
---|---|---|
emitter | Emitter |
The emitter on which the event type will be emitted. |
type | EventType |
The event type. |
data | Array |
The data to be passed with the event. |
emitEvery | boolean |
Whether or not listeners for all types will be executed. |
Executes a listener using the internal execute*
functions based on the number of arguments.
Kind: inner method of Emitter
Param | Type |
---|---|
listener | Array.<Listener> | Listener |
data | Array |
scope | * |
Emitter~getEventTypes(emitter) ⇒ Array.<EventType>
Kind: inner method of Emitter
Returns: Array.<EventType>
- The list of event types registered to the emitter.
Param | Type | Description |
---|---|---|
emitter | Emitter |
The emitter on which event types will be retrieved. |
Emitter~getMaxListeners(emitter) ⇒ number
Kind: inner method of Emitter
Returns: number
- The maximum number of listeners.
Param | Type | Description |
---|---|---|
emitter | Emitter |
The emitter on which max listeners will be retrieved. |
Emitter~isPositiveNumber(number) ⇒ boolean
Checks whether or not a value is a positive number.
Kind: inner method of Emitter
Returns: boolean
- Whether or not the value is a positive number.
Param | Type | Description |
---|---|---|
number | * |
The value to be tested. |
Execute a listener with no arguments.
Kind: inner method of Emitter
Param | Type | Description |
---|---|---|
handler | EventListener | Array.<EventListener> |
One or more listeners that will be executed on the emitter . |
isFunction | boolean |
Whether or not the handler is a function. |
emitter | Emitter |
The emitter. |
Execute a listener with one argument.
Kind: inner method of Emitter
Param | Type | Description |
---|---|---|
handler | EventListener | Array.<EventListener> |
One or more listeners that will be executed on the emitter . |
isFunction | boolean |
Whether or not the handler is a function. |
emitter | Emitter |
The emitter. |
arg1 | * |
The first argument. |
Execute a listener with two arguments.
Kind: inner method of Emitter
Param | Type | Description |
---|---|---|
handler | EventListener | Array.<EventListener> |
One or more listeners that will be executed on the emitter . |
isFunction | boolean |
Whether or not the handler is a function. |
emitter | Emitter |
The emitter. |
arg1 | * |
The first argument. |
arg2 | * |
The second argument. |
Execute a listener with three arguments.
Kind: inner method of Emitter
Param | Type | Description |
---|---|---|
handler | EventListener | Array.<EventListener> |
One or more listeners that will be executed on the emitter . |
isFunction | boolean |
Whether or not the handler is a function. |
emitter | Emitter |
The emitter. |
arg1 | * |
The first argument. |
arg2 | * |
The second argument. |
arg3 | * |
The third argument. |
Execute a listener with four or more arguments.
Kind: inner method of Emitter
Param | Type | Description |
---|---|---|
handler | EventListener | Array.<EventListener> |
One or more listeners that will be executed on the emitter . |
isFunction | boolean |
Whether or not the handler is a function. |
emitter | Emitter |
The emitter. |
args | Array |
Four or more arguments. |
Kind: inner method of Emitter
Param | Type | Description |
---|---|---|
emitter | Emitter |
The emitter on which the event would be emitted. |
type | EventType |
The event type. |
listener | EventListener |
The event callback. |
Kind: inner method of Emitter
Param | Type | Description |
---|---|---|
The | Emitter |
emitter on which the maximum number of listeners will be set. |
max | number |
The maximum number of listeners before a warning is issued. |
Faster than Array.prototype.splice
Kind: inner method of Emitter
Param | Type |
---|---|
list | Array |
index | number |
Asynchronously executes a function.
Kind: inner method of Emitter
Param | Type | Description |
---|---|---|
callback | Function |
The function to be executed. |
Emitter~tickAllEvents(emitter, type, data) ⇒ Promise
Kind: inner method of Emitter
Returns: Promise
- A promise which resolves if the event had listeners, rejects otherwise.
Param | Type | Description |
---|---|---|
emitter | Emitter |
The emitter on which the event type will be asynchronously emitted. |
type | EventType |
The event type. |
data | Array |
The data to be passed with the event. |
Applies a selection
of the Emitter.js API to the target
.
Kind: inner method of Emitter
Param | Type | Description |
---|---|---|
[selection] | APIReference |
A selection of the Emitter.js API. |
target | Object |
The object on which the API will be applied. |
Applies the Emitter.js API to the target.
Kind: global function
Since: 2.0.0
Param | Type | Description |
---|---|---|
[selection] | APIReference |
A selection of the Emitter.js API that will be applied to the target . |
target | exteral:Object |
The object to which the Emitter.js API will be applied. |
Example (Applying all of the API)
let greeter = Object.create( null );
Emitter( greeter );
greeter.on( 'hello', () => console.log( 'Hello!' ) );
greeter.emit( 'hello' );
// Hello!
Example (Applying a selection of the API)
let greeter = Object.create( null );
Emitter( 'emit on off', greeter );
greeter.on( 'hello', () => console.log( 'Hello!' ) );
greeter.emit( 'hello' );
// Hello!
Example (Remapping a selection of the API)
let greeter = Object.create( null );
Emitter( { fire: 'emit', addListener: 'on' }, greeter );
greeter.addListener( 'hello', () => console.log( 'Hello!' ) );
greeter.fire( 'hello' );
// Hello!
- Emitter([selection], target)
- new Emitter([mapping])
- instance
- .maxListeners :
number
- .destroy()
- .toJSON() ⇒
Object
- .toString() ⇒
string
- .at([type], index, listener) ⇒
Emitter
- .clear([type]) ⇒
Emitter
- .emit(type, [...data]) ⇒
boolean
- .eventTypes() ⇒
Array.<EventType>
- .first(type, listener) ⇒
Emitter
- .getMaxListeners() ⇒
number
- .listenerCount(type) ⇒
number
- .listeners(type) ⇒
number
- .many(type, times, listener) ⇒
Emitter
- .off(type, listener) ⇒
Emitter
- .on([type], listener) ⇒
Emitter
- .once([type], listener) ⇒
Emitter
- .setMaxListeners(max) ⇒
Emitter
- .tick(type, [...data]) ⇒
Promise
- .trigger([type], data) ⇒
boolean
- .until([type], listener) ⇒
Emitter
- ":destroy"
- ":off"
- ":on"
- ":maxListeners"
- .maxListeners :
- static
- inner
- ~Null ⇐
null
- ~asEmitter
- .at([type], index, listener) ⇒
Emitter
- .clear([type]) ⇒
Emitter
- .emit(type, [...data]) ⇒
boolean
- .eventTypes() ⇒
Array.<EventType>
- .first(type, listener) ⇒
Emitter
- .getMaxListeners() ⇒
number
- .listenerCount(type) ⇒
number
- .listeners(type) ⇒
number
- .many(type, times, listener) ⇒
Emitter
- .off(type, listener) ⇒
Emitter
- .on([type], listener) ⇒
Emitter
- .once([type], listener) ⇒
Emitter
- .setMaxListeners(max) ⇒
Emitter
- .tick(type, [...data]) ⇒
Promise
- .trigger([type], data) ⇒
boolean
- .until([type], listener) ⇒
Emitter
- .at([type], index, listener) ⇒
- ~addConditionalEventListener(emitter, type, listener)
- ~addEventListener(emitter, type, listener, index)
- ~addFiniteEventListener(emitter, type, times, listener)
- ~addEventMapping(emitter, mapping)
- ~defineEventsProperty(emitter)
- ~emitAllEvents(emitter, type, data) ⇒
boolean
- ~emitErrors(emitter, errors)
- ~emitEvent(emitter, type, data, emitEvery) ⇒
boolean
- ~executeListener(listener, data, scope)
- ~getEventTypes(emitter) ⇒
Array.<EventType>
- ~getMaxListeners(emitter) ⇒
number
- ~isPositiveNumber(number) ⇒
boolean
- ~listenEmpty(handler, isFunction, emitter)
- ~listenOne(handler, isFunction, emitter, arg1)
- ~listenTwo(handler, isFunction, emitter, arg1, arg2)
- ~listenThree(handler, isFunction, emitter, arg1, arg2, arg3)
- ~listenMany(handler, isFunction, emitter, args)
- ~removeEventListener(emitter, type, listener)
- ~setMaxListeners(The, max)
- ~spliceList(list, index)
- ~tick(callback)
- ~tickAllEvents(emitter, type, data) ⇒
Promise
- ~toEmitter([selection], target)
- ~Null ⇐
Creates an instance of emitter. If mapping
are provided they will automatically be passed into on()
once construction is complete.
Param | Type | Description |
---|---|---|
[mapping] | EventMapping |
A mapping of event types to event listeners. |
Example (Using Emitter directly)
const greeter = new Emitter();
greeter.on( 'hello', () => console.log( 'Hello!' ) );
greeter.emit( 'hello' );
// Hello!
Example (Extending Emitter using Classical inheritance)
class Greeter extends Emitter {
constructor(){
super();
this.on( 'greet', ( name ) => console.log( `Hello, ${ name }!` ) );
}
greet( name ){
this.emit( 'greet', name );
}
}
const greeter = new Greeter();
greeter.greet( 'Jeff' );
// Hello, Jeff!
Example (Extending Emitter using Prototypal inheritance)
function Greeter(){
Emitter.call( this );
this.on( 'greet', ( name ) => console.log( `Hello, ${ name }!` ) );
}
Greeter.prototype = Object.create( Emitter.prototype );
Greeter.prototype.greet = function( name ){
this.emit( 'greet', name );
};
const greeter = new Greeter();
greeter.greet( 'Jeff' );
// Hello, Jeff!
Example (Namespaced events)
const greeter = new Emitter();
greeter.on( 'greeting:hello', ( name ) => console.log( `Hello, ${ name }!` ) );
greeter.on( 'greeting:hi', ( name ) => console.log( `Hi, ${ name }!` ) );
greeter.on( 'greeting', ( name ) => console.log( `${ name } was greeted.` );
greeter.emit( 'greeting:hi', 'Mark' );
greeter.emit( 'greeting:hello', 'Jeff' );
// Hi, Mark!
// Mark was greeted.
// Hello, Jeff!
// Jeff was greeted.
Example (Predefined events)
const greetings = {
hello: function( name ){ console.log( `Hello, ${name}!` ),
hi: function( name ){ console.log( `Hi, ${name}!` )
},
greeter = new Emitter( greetings );
greeter.emit( 'hello', 'Aaron' );
// Hello, Aaron!
Example (One-time events)
const greeter = new Emitter();
greeter.once( 'hello', ( name ) => console.log( `Hello, ${ name }!` ) );
greeter.emit( 'hello', 'Jeff' );
greeter.emit( 'hello', 'Terry' );
// Hello, Jeff!
Example (Many-time events)
const greeter = new Emitter();
greeter.many( 'hello', 2, ( name ) => console.log( `Hello, ${ name }!` ) );
greeter.emit( 'hello', 'Jeff' ); // 1
greeter.emit( 'hello', 'Terry' ); // 2
greeter.emit( 'hello', 'Steve' ); // 3
// Hello, Jeff!
// Hello, Terry!
emitter.maxListeners : number
By default Emitters will emit a :maxListeners
event if more than 10 listeners are added for a particular event type
. This property allows that to be changed. Set to 0 for unlimited.
Kind: instance property of Emitter
Since: 1.0.0
Example
const greeter = new Emitter();
console.log( greeter.maxListeners );
// 10
greeter.maxListeners = 1;
greeter.on( ':maxListeners', ( greeting ) => console.log( `Greeting "${ greeting }" has one too many!` ) );
greeter.on( 'hello', () => console.log( 'Hello!' ) );
greeter.on( 'hello', () => alert( 'Hello!' ) );
// Greeting "hello" has one too many!
Destroys the emitter.
Kind: instance method of Emitter
Emits: :destroy
Since: 1.0.0
emitter.toJSON() ⇒ Object
Kind: instance method of Emitter
Returns: Object
- An plain object representation of the emitter.
Since: 1.3.0
Example
const greeter = new Emitter();
greeter.maxListeners = 5;
greeter.on( 'greet', ( name ) => console.log( `Hello, ${ name }!` ) );
greeter.on( 'greet', ( name ) => console.log( `Hi, ${ name }!` ) );
console.log( greeter.toJSON() );
// { "maxListeners": 5, "listenerCount": { "greet": 2 } }
greeter.destroy();
console.log( greeter.toJSON() );
// "destroyed"
emitter.toString() ⇒ string
Kind: instance method of Emitter
Returns: string
- A string representation of the emitter.
Since: 1.3.0
Example
const greeter = new Emitter();
greeter.maxListeners = 5;
greeter.on( 'greet', ( name ) => console.log( `Hello, ${ name }!` ) );
greeter.on( 'greet', ( name ) => console.log( `Hi, ${ name }!` ) );
console.log( greeter.toString() );
// 'Emitter { "maxListeners": 5, "listenerCount": { "greet": 2 } }'
greeter.destroy();
console.log( greeter.toString() );
// 'Emitter "destroyed"'
emitter.at([type], index, listener) ⇒ Emitter
Adds a listener for the specified event type
at the specified index
. If no type
is given the listener will be triggered any event type
.
No checks are made to see if the listener
has already been added. Multiple calls passing the same combination type
and listener
will result in the listener
being added multiple times.
Kind: instance method of Emitter
Mixes: at
Returns: Emitter
- The emitter.
Emits: :on
, :maxListeners
Since: 2.0.0
Param | Type | Description |
---|---|---|
[type] | EventType |
The event type. |
index | number |
Where the listener will be added in the trigger list. |
listener | EventListener |
The event callback. |
emitter.clear([type]) ⇒ Emitter
Remove all listeners, or those for the specified event type
.
Kind: instance method of Emitter
Mixes: clear
Returns: Emitter
- The emitter.
Since: 1.0.0
Param | Type | Description |
---|---|---|
[type] | String |
The event type. |
Example (Clearing all event types)
const greeter = new Emitter();
greeter.on( 'hello', () => console.log( 'Hello!' ) );
greeter.on( 'hi', () => console.log( 'Hi!' ) );
greeter.emit( 'hello' );
// Hello!
greeter.emit( 'hi' );
// Hi!
greeter.clear();
greeter.emit( 'hello' );
greeter.emit( 'hi' );
Example (Clearing a specified event type)
const greeter = new Emitter();
greeter.on( {
'hello' : function(){ console.log( 'Hello!' ); },
'hi' : function(){ console.log( 'Hi!' ); }
} );
greeter.emit( 'hello' );
// Hello!
greeter.emit( 'hi' );
// Hi!
greeter.clear( 'hello' );
greeter.emit( 'hello' );
greeter.emit( 'hi' );
// Hi!
emitter.emit(type, [...data]) ⇒ boolean
Execute the listeners for the specified event type
with the supplied arguments.
The type
can be namespaced using :
, which will result in multiple events being triggered in succession. Listeners can be associated with the fully namespaced type
or a subset of the type
.
Returns true
if the event had listeners, false
otherwise.
Kind: instance method of Emitter
Mixes: emit
Returns: boolean
- Whether or not the event had listeners.
Since: 1.0.0
Param | Type | Description |
---|---|---|
type | EventType |
The event type. |
[...data] | * |
The data passed into the listeners. |
Example (Emitting an event)
const greeter = new Emitter();
greeter.on( 'hello', () => console.log( 'Hello!' ) );
greeter.emit( 'hello' ); // true
// Hello!
greeter.emit( 'goodbye' ); // false
Example (Emitting an event with data)
const greeter = new Emitter();
greeter.on( 'hello', ( name ) => console.log( `Hello, ${ name }!` ) );
greeter.emit( 'hello', 'World' );
// Hello, World!
Example (Emitting a namespaced event)
const greeter = new Emitter();
greeter.on( 'greeting:hello', ( name ) => console.log( `Hello, ${ name }!` ) );
greeter.on( 'greeting:hi', ( name ) => console.log( `Hi, ${ name }!` ) );
greeter.on( 'greeting', ( name ) => console.log( `${ name } was greeted.` );
// This event will not be triggered by emitting "greeting:hello"
greeter.on( 'hello', ( name ) => console.log( `Hello again, ${ name }` );
greeter.emit( 'greeting:hi', 'Mark' );
// Hi, Mark!
// Mark was greeted.
greeter.emit( 'greeting:hello', 'Jeff' );
// Hello, Jeff!
// Jeff was greeted.
emitter.eventTypes() ⇒ Array.<EventType>
Kind: instance method of Emitter
Mixes: eventTypes
Returns: Array.<EventType>
- The list of event types registered to the emitter.
Since: 2.0.0
Example
const greeter = new Emitter();
greeter.on( 'hello', () => console.log( `Hello` ) );
greeter.on( 'hi', () => console.log( `Hi` ) );
console.log( greeter.eventTypes() );
// [ 'hello', 'hi' ]
emitter.first(type, listener) ⇒ Emitter
Kind: instance method of Emitter
Mixes: first
Returns: Emitter
- The emitter.
Since: 2.0.0
Param | Type | Description |
---|---|---|
type | EventType |
The event type. |
listener | EventListener |
The event callback. |
emitter.getMaxListeners() ⇒ number
By default Emitter will emit a :maxListeners
evet if more than 10 listeners are added for a particular event type
. This method returns the current value.
Kind: instance method of Emitter
Mixes: getMaxListeners
Returns: number
- The maximum number of listeners.
Since: 2.0.0
Example
const greeter = new Emitter();
console.log( greeter.getMaxListeners() );
// 10
greeter.setMaxListeners( 5 );
console.log( greeter.getMaxListeners() );
// 5
emitter.listenerCount(type) ⇒ number
Kind: instance method of Emitter
Mixes: listenerCount
Returns: number
- The number of listeners for that event type within the given emitter.
Since: 1.0.0
Param | Type | Description |
---|---|---|
type | EventType |
The event type. |
Example
const greeter = new Emitter();
greeter.on( 'hello', () => console.log( 'Hello!' ) );
console.log( greeter.listenerCount( 'hello' ) );
// 1
console.log( greeter.listenerCount( 'goodbye' ) );
// 0
emitter.listeners(type) ⇒ number
Kind: instance method of Emitter
Mixes: listeners
Returns: number
- The number of listeners for that event type within the given emitter.
Since: 1.0.0
Param | Type | Description |
---|---|---|
type | EventType |
The event type. |
Example
const hello = function(){
console.log( 'Hello!' );
},
greeter = new Emitter();
greeter.on( 'hello', hello );
greeter.emit( 'hello' );
// Hello!
console.log( greeter.listeners( 'hello' )[ 0 ] === hello );
// true
emitter.many(type, times, listener) ⇒ Emitter
Adds a many time listener for the specified event type
. If no type
is given the listener will be triggered any event type
. After the listener is invoked the specified number of times
, it is removed.
No checks are made to see if the listener
has already been added. Multiple calls passing the same combination type
and listener
will result in the listener
being added multiple times.
Kind: instance method of Emitter
Mixes: many
Returns: Emitter
- The emitter.
Since: 1.0.0
Param | Type | Description |
---|---|---|
type | EventType |
The event type. |
times | number |
The number times the listener will be executed before being removed. |
listener | EventListener |
The event callback. |
Example (Listen to any event type a set number of times)
const greeter = new Emitter();
greeter.many( 2, ( name ) => console.log( `Greeted ${ name }` ) );
greeter.emit( 'hello', 'Jeff' ); // 1
// Greeted Jeff
greeter.emit( 'hi', 'Terry' ); // 2
// Greeted Terry
greeter.emit( 'yo', 'Steve' ); // 3
Example (Listen to the specified event type a set number of times)
const greeter = new Emitter();
greeter.many( 'hello', 2, ( name ) => console.log( `Hello, ${ name }!` ) );
greeter.emit( 'hello', 'Jeff' ); // 1
// Hello, Jeff!
greeter.emit( 'hello', 'Terry' ); // 2
// Hello, Terry!
greeter.emit( 'hello', 'Steve' ); // 3
emitter.off(type, listener) ⇒ Emitter
Removes the listener
for the specified event type
. If no type
is given it is assumed the listener
is not associated with a specific type
.
If any single listener has been added multiple times for the specified type
, then emitter.off()
must be called multiple times to remove each instance.
Kind: instance method of Emitter
Mixes: off
Returns: Emitter
- The emitter.
Emits: :off
Since: 1.0.0
Param | Type | Description |
---|---|---|
type | EventType |
The event type. |
listener | EventListener |
The event callback. |
Example (Remove a listener from any event type)
function greet( name ){
console.log( `Greetings, ${ name }!` );
}
const greeter = new Emitter();
greeter.on( greet );
greeter.emit( 'hello' 'Jeff' );
// Greetings, Jeff!
greeter.emit( 'hi' 'Jeff' );
// Greetings, Jeff!
greeter.off( greet );
greeter.emit( 'yo', 'Jeff' );
Example (Remove a listener from a specified event type)
function hello( name ){
console.log( `Hello, ${ name }!` );
}
const greeter = new Emitter();
greeter.on( 'hello', hello );
greeter.emit( 'hello', 'Jeff' );
// Hello, Jeff!
greeter.off( 'hello', hello );
greeter.emit( 'hello', 'Jeff' );
emitter.on([type], listener) ⇒ Emitter
Adds a listener for the specified event type
. If no type
is given the listener will be triggered any event type
.
No checks are made to see if the listener
has already been added. Multiple calls passing the same combination type
and listener
will result in the listener
being added multiple times.
Kind: instance method of Emitter
Mixes: on
Returns: Emitter
- The emitter.
Emits: :on
, :maxListeners
Since: 1.0.0
Param | Type | Description |
---|---|---|
[type] | EventType |
The event type. |
listener | EventListener |
The event callback. |
Example (Listen to all event types)
const greeter = new Emitter();
greeter.on( () => console.log( 'Greeted' ) );
greeter.emit( 'hello' );
// Greeted
greeter.emit( 'goodbye' );
// Greeted
Example (Listener to a specified event type)
const greeter = new Emitter();
greeter.on( 'hello', ( name ) => console.log( `Hello, ${ name }!` ) );
greeter.emit( 'hello', 'World' );
// Hello, World!
greeter.emit( 'hi', 'World' );
emitter.once([type], listener) ⇒ Emitter
Kind: instance method of Emitter
Mixes: once
Returns: Emitter
- The emitter.
Emits: :on
, Emitter#:maxListeners
const greeter = new Emitter();
greeter.once( () => console.log( 'Greeted' ) );
greeter.emit( 'hello' );
// Greeted
greeter.emit( 'goodbye' );event:
Since: 1.0.0
Param | Type | Description |
---|---|---|
[type] | EventType |
The event type. |
listener | EventListener |
The event callback. |
Example (Listen once to all event types)
const greeter = new Emitter();
greeter.once( 'hello', ( name ) => console.log( `Hello, ${ name }!` ) );
greeter.emit( 'hello', 'World' );
// Hello, World!
greeter.emit( 'hello', 'World' );
emitter.setMaxListeners(max) ⇒ Emitter
By default Emitter will emit a :maxListeners
evet if more than 10 listeners are added for a particular event type
. This method allows that to be changed. Set to 0 for unlimited.
Kind: instance method of Emitter
Mixes: setMaxListeners
Returns: Emitter
- The emitter.
Since: 2.0.0
Param | Type | Description |
---|---|---|
max | number |
The maximum number of listeners before a warning is issued. |
Example
const greeter = new Emitter();
greeter.setMaxListeners( 1 );
greeter.on( ':maxListeners', ( greeting ) => console.log( `Greeting "${ greeting }" has one too many!` ) );
greeter.on( 'hello', () => console.log( 'Hello!' ) );
greeter.on( 'hello', () => alert( 'Hello!' ) );
// Greeting "hello" has one too many!
emitter.tick(type, [...data]) ⇒ Promise
Asynchronously emits specified event type
with the supplied arguments. The listeners will still be synchronously executed in the specified order.
The type
can be namespaced using :
, which will result in multiple events being triggered in succession. Listeners can be associated with the fully namespaced type
or a subset of the type
.
Returns promise which resolves if the event had listeners, rejects otherwise.
Kind: instance method of Emitter
Mixes: tick
Returns: Promise
- A promise which resolves if the event had listeners, rejects otherwise.
Since: 2.0.0
Param | Type | Description |
---|---|---|
type | EventType |
The event type. |
[...data] | * |
The data passed into the listeners. |
Example (Asynchronously emitting an event)
const greeter = new Emitter();
greeter.on( 'hello', () => console.log( 'Hello!' ) );
greeter.tick( 'hello' ).then( ( heard ) => console.log( 'hello heard? ', heard ) );
greeter.tick( 'goodbye' ).then( ( heard ) => console.log( 'goodbye heard? ', heard ) );
// Hello!
// hello heard? true
// goodbye heard? false
emitter.trigger([type], data) ⇒ boolean
Execute the listeners for the specified event type
with the supplied data
.
Returns true
if the event had listeners, false
otherwise.
Kind: instance method of Emitter
Mixes: trigger
Returns: boolean
- Whether or not the event had listeners.
Since: 1.0.0
Param | Type | Description |
---|---|---|
[type] | EventType |
The event type. |
data | Array |
Example
const greeter = new Emitter();
greeter.on( 'hello', ( name ) => console.log( `Hello, ${ name }!` ) );
greeter.trigger( 'hello', [ 'World' ] );
// Hello, World!
Example
const greeter = new Emitter();
greeter.on( 'greeting:hello', ( name ) => console.log( `Hello, ${ name }!` ) );
greeter.on( 'greeting:hi', ( name ) => console.log( `Hi, ${ name }!` ) );
greeter.on( 'greeting', ( name ) => console.log( `${ name } was greeted.` );
greeter.trigger( 'greeting:hi', [ 'Mark' ] );
// Hi, Mark!
// Mark was greeted.
greeter.trigger( 'greeting:hello', [ 'Jeff' ] );
// Hello, Jeff!
// Jeff was greeted.
emitter.until([type], listener) ⇒ Emitter
Adds a listeners for the specified event type
that will be triggered until the listener
returns true
. If no type
is given the listener will be triggered any event type
.
No checks are made to see if the listener
has already been added. Multiple calls passing the same combination type
and listener
will result in the listener
being added multiple times.
Kind: instance method of Emitter
Mixes: until
Returns: Emitter
- The emitter.
Since: 1.2.0
Param | Type | Description |
---|---|---|
[type] | EventType |
The event type. |
listener | EventListener |
The event callback. |
Example
const greeter = new Emitter();
greeter.until( function( name ){
console.log( `Greeted ${ name }` );
return name === 'Terry';
} );
greeter.emit( 'hello', 'Jeff' );
// Greeted Jeff
greeter.emit( 'goodbye', 'Terry' );
// Greeted Terry
greeter.emit( 'hi', 'Aaron' );
Example
const greeter = new Emitter();
greeter.until( 'hello', function( name ){
console.log( `Hello, ${ name }!` );
return name === 'World';
} );
greeter.emit( 'hello', 'Jeff' );
// Hello, Jeff!
greeter.emit( 'hello', 'World' );
// Hello, World!
greeter.emit( 'hello', 'Mark' );
This event is emitted before an emitter destroys itself.
Kind: event emitted by Emitter
This event is emitted after a listener is removed.
Kind: event emitted by Emitter
This event is emitted before a listener is added.
Kind: event emitted by Emitter
This event is emitted once the maximum number of listeners has been exceeded for an event type.
Kind: event emitted by Emitter
Emitter.defaultMaxListeners : number
Sets the default maximum number of listeners for all emitters. Use emitter.maxListeners
to set the maximum on a per-instance basis.
By default Emitter will emit a :maxListeners
event if more than 10 listeners are added to a specific event type.
Kind: static property of Emitter
Default: 10
Since: 1.0.0
Example (Changing the default maximum listeners)
console.log( Emitter.defaultMaxListeners );
// 10
const greeter1 = new Emitter(),
greeter2 = new Emitter();
Emitter.defaultMaxListeners = 1;
greeter1.on( ':maxListeners', ( greeting ) => console.log( `Greeting "${ greeting }" has one too many!` ) );
greeter1.on( 'hello', () => console.log( 'Hello!' ) );
greeter1.on( 'hello', () => alert( 'Hello!' ) );
// Greeting "hello" has one too many!
greeter2.on( ':maxListeners', ( greeting ) => console.log( `Greeting "${ greeting }" has one too many!` ) );
greeter2.on( 'hi', () => console.log( 'Hi!' ) );
greeter2.on( 'hi', () => alert( 'Hi!' ) );
// Greeting "hi" has one too many!
Emitter.every : symbol
An id used to listen for events of any type
. For most methods, when no type
is given this is the default.
Listener bound to every event will not execute for Emitter lifecycle events, like :maxListeners
.
Using Emitter.every
is typically not necessary.
Kind: static property of Emitter
Since: 1.0.0
Example
const greeter = new Emitter();
greeter.on( Emitter.every, () => console.log( 'Greeted' ) );
greeter.emit( 'hello' );
// Greeted
greeter.emit( 'goodbye' );
// Greeted
Emitter.version : string
The current version of Emitter.js.
Kind: static property of Emitter
Since: 1.1.2
Example
console.log( Emitter.version );
// 2.0.0
Emitter~Null ⇐ null
Kind: inner class of Emitter
Extends: null
A "clean", empty container. Instantiating this is faster than explicitly calling Object.create( null )
.
A functional mixin that provides the Emitter.js API to its target. The constructor()
, destroy()
, toJSON()
, toString()
, and static properties on Emitter
are not provided. This mixin is used to populate the prototype
of Emitter
.
Like all functional mixins, this should be executed with call() or apply().
Kind: inner mixin of Emitter
Since: 1.1.0
Example (Applying Emitter functionality)
// Create a base object
const greeter = Object.create( null );
// Apply the mixin
asEmitter.call( greeter );
greeter.on( 'hello', ( name ) => console.log( `Hello, ${ name }!` ) );
greeter.emit( 'hello', 'World' );
// Hello, World!
Example (Applying chaos to your world)
// NO!!!
asEmitter(); // Madness ensues
- ~asEmitter
- .at([type], index, listener) ⇒
Emitter
- .clear([type]) ⇒
Emitter
- .emit(type, [...data]) ⇒
boolean
- .eventTypes() ⇒
Array.<EventType>
- .first(type, listener) ⇒
Emitter
- .getMaxListeners() ⇒
number
- .listenerCount(type) ⇒
number
- .listeners(type) ⇒
number
- .many(type, times, listener) ⇒
Emitter
- .off(type, listener) ⇒
Emitter
- .on([type], listener) ⇒
Emitter
- .once([type], listener) ⇒
Emitter
- .setMaxListeners(max) ⇒
Emitter
- .tick(type, [...data]) ⇒
Promise
- .trigger([type], data) ⇒
boolean
- .until([type], listener) ⇒
Emitter
- .at([type], index, listener) ⇒
asEmitter.at([type], index, listener) ⇒ Emitter
Adds a listener for the specified event type
at the specified index
. If no type
is given the listener will be triggered any event type
.
No checks are made to see if the listener
has already been added. Multiple calls passing the same combination type
and listener
will result in the listener
being added multiple times.
Kind: static method of asEmitter
Returns: Emitter
- The emitter.
Emits: :on
, :maxListeners
Since: 2.0.0
Param | Type | Description |
---|---|---|
[type] | EventType |
The event type. |
index | number |
Where the listener will be added in the trigger list. |
listener | EventListener |
The event callback. |
asEmitter.clear([type]) ⇒ Emitter
Remove all listeners, or those for the specified event type
.
Kind: static method of asEmitter
Returns: Emitter
- The emitter.
Since: 1.0.0
Param | Type | Description |
---|---|---|
[type] | String |
The event type. |
Example (Clearing all event types)
const greeter = new Emitter();
greeter.on( 'hello', () => console.log( 'Hello!' ) );
greeter.on( 'hi', () => console.log( 'Hi!' ) );
greeter.emit( 'hello' );
// Hello!
greeter.emit( 'hi' );
// Hi!
greeter.clear();
greeter.emit( 'hello' );
greeter.emit( 'hi' );
Example (Clearing a specified event type)
const greeter = new Emitter();
greeter.on( {
'hello' : function(){ console.log( 'Hello!' ); },
'hi' : function(){ console.log( 'Hi!' ); }
} );
greeter.emit( 'hello' );
// Hello!
greeter.emit( 'hi' );
// Hi!
greeter.clear( 'hello' );
greeter.emit( 'hello' );
greeter.emit( 'hi' );
// Hi!
asEmitter.emit(type, [...data]) ⇒ boolean
Execute the listeners for the specified event type
with the supplied arguments.
The type
can be namespaced using :
, which will result in multiple events being triggered in succession. Listeners can be associated with the fully namespaced type
or a subset of the type
.
Returns true
if the event had listeners, false
otherwise.
Kind: static method of asEmitter
Returns: boolean
- Whether or not the event had listeners.
Since: 1.0.0
Param | Type | Description |
---|---|---|
type | EventType |
The event type. |
[...data] | * |
The data passed into the listeners. |
Example (Emitting an event)
const greeter = new Emitter();
greeter.on( 'hello', () => console.log( 'Hello!' ) );
greeter.emit( 'hello' ); // true
// Hello!
greeter.emit( 'goodbye' ); // false
Example (Emitting an event with data)
const greeter = new Emitter();
greeter.on( 'hello', ( name ) => console.log( `Hello, ${ name }!` ) );
greeter.emit( 'hello', 'World' );
// Hello, World!
Example (Emitting a namespaced event)
const greeter = new Emitter();
greeter.on( 'greeting:hello', ( name ) => console.log( `Hello, ${ name }!` ) );
greeter.on( 'greeting:hi', ( name ) => console.log( `Hi, ${ name }!` ) );
greeter.on( 'greeting', ( name ) => console.log( `${ name } was greeted.` );
// This event will not be triggered by emitting "greeting:hello"
greeter.on( 'hello', ( name ) => console.log( `Hello again, ${ name }` );
greeter.emit( 'greeting:hi', 'Mark' );
// Hi, Mark!
// Mark was greeted.
greeter.emit( 'greeting:hello', 'Jeff' );
// Hello, Jeff!
// Jeff was greeted.
asEmitter.eventTypes() ⇒ Array.<EventType>
Kind: static method of asEmitter
Returns: Array.<EventType>
- The list of event types registered to the emitter.
Since: 2.0.0
Example
const greeter = new Emitter();
greeter.on( 'hello', () => console.log( `Hello` ) );
greeter.on( 'hi', () => console.log( `Hi` ) );
console.log( greeter.eventTypes() );
// [ 'hello', 'hi' ]
asEmitter.first(type, listener) ⇒ Emitter
Kind: static method of asEmitter
Returns: Emitter
- The emitter.
Since: 2.0.0
Param | Type | Description |
---|---|---|
type | EventType |
The event type. |
listener | EventListener |
The event callback. |
asEmitter.getMaxListeners() ⇒ number
By default Emitter will emit a :maxListeners
evet if more than 10 listeners are added for a particular event type
. This method returns the current value.
Kind: static method of asEmitter
Returns: number
- The maximum number of listeners.
Since: 2.0.0
Example
const greeter = new Emitter();
console.log( greeter.getMaxListeners() );
// 10
greeter.setMaxListeners( 5 );
console.log( greeter.getMaxListeners() );
// 5
asEmitter.listenerCount(type) ⇒ number
Kind: static method of asEmitter
Returns: number
- The number of listeners for that event type within the given emitter.
Since: 1.0.0
Param | Type | Description |
---|---|---|
type | EventType |
The event type. |
Example
const greeter = new Emitter();
greeter.on( 'hello', () => console.log( 'Hello!' ) );
console.log( greeter.listenerCount( 'hello' ) );
// 1
console.log( greeter.listenerCount( 'goodbye' ) );
// 0
asEmitter.listeners(type) ⇒ number
Kind: static method of asEmitter
Returns: number
- The number of listeners for that event type within the given emitter.
Since: 1.0.0
Param | Type | Description |
---|---|---|
type | EventType |
The event type. |
Example
const hello = function(){
console.log( 'Hello!' );
},
greeter = new Emitter();
greeter.on( 'hello', hello );
greeter.emit( 'hello' );
// Hello!
console.log( greeter.listeners( 'hello' )[ 0 ] === hello );
// true
asEmitter.many(type, times, listener) ⇒ Emitter
Adds a many time listener for the specified event type
. If no type
is given the listener will be triggered any event type
. After the listener is invoked the specified number of times
, it is removed.
No checks are made to see if the listener
has already been added. Multiple calls passing the same combination type
and listener
will result in the listener
being added multiple times.
Kind: static method of asEmitter
Returns: Emitter
- The emitter.
Since: 1.0.0
Param | Type | Description |
---|---|---|
type | EventType |
The event type. |
times | number |
The number times the listener will be executed before being removed. |
listener | EventListener |
The event callback. |
Example (Listen to any event type a set number of times)
const greeter = new Emitter();
greeter.many( 2, ( name ) => console.log( `Greeted ${ name }` ) );
greeter.emit( 'hello', 'Jeff' ); // 1
// Greeted Jeff
greeter.emit( 'hi', 'Terry' ); // 2
// Greeted Terry
greeter.emit( 'yo', 'Steve' ); // 3
Example (Listen to the specified event type a set number of times)
const greeter = new Emitter();
greeter.many( 'hello', 2, ( name ) => console.log( `Hello, ${ name }!` ) );
greeter.emit( 'hello', 'Jeff' ); // 1
// Hello, Jeff!
greeter.emit( 'hello', 'Terry' ); // 2
// Hello, Terry!
greeter.emit( 'hello', 'Steve' ); // 3
asEmitter.off(type, listener) ⇒ Emitter
Removes the listener
for the specified event type
. If no type
is given it is assumed the listener
is not associated with a specific type
.
If any single listener has been added multiple times for the specified type
, then emitter.off()
must be called multiple times to remove each instance.
Kind: static method of asEmitter
Returns: Emitter
- The emitter.
Emits: :off
Since: 1.0.0
Param | Type | Description |
---|---|---|
type | EventType |
The event type. |
listener | EventListener |
The event callback. |
Example (Remove a listener from any event type)
function greet( name ){
console.log( `Greetings, ${ name }!` );
}
const greeter = new Emitter();
greeter.on( greet );
greeter.emit( 'hello' 'Jeff' );
// Greetings, Jeff!
greeter.emit( 'hi' 'Jeff' );
// Greetings, Jeff!
greeter.off( greet );
greeter.emit( 'yo', 'Jeff' );
Example (Remove a listener from a specified event type)
function hello( name ){
console.log( `Hello, ${ name }!` );
}
const greeter = new Emitter();
greeter.on( 'hello', hello );
greeter.emit( 'hello', 'Jeff' );
// Hello, Jeff!
greeter.off( 'hello', hello );
greeter.emit( 'hello', 'Jeff' );
asEmitter.on([type], listener) ⇒ Emitter
Adds a listener for the specified event type
. If no type
is given the listener will be triggered any event type
.
No checks are made to see if the listener
has already been added. Multiple calls passing the same combination type
and listener
will result in the listener
being added multiple times.
Kind: static method of asEmitter
Returns: Emitter
- The emitter.
Emits: :on
, :maxListeners
Since: 1.0.0
Param | Type | Description |
---|---|---|
[type] | EventType |
The event type. |
listener | EventListener |
The event callback. |
Example (Listen to all event types)
const greeter = new Emitter();
greeter.on( () => console.log( 'Greeted' ) );
greeter.emit( 'hello' );
// Greeted
greeter.emit( 'goodbye' );
// Greeted
Example (Listener to a specified event type)
const greeter = new Emitter();
greeter.on( 'hello', ( name ) => console.log( `Hello, ${ name }!` ) );
greeter.emit( 'hello', 'World' );
// Hello, World!
greeter.emit( 'hi', 'World' );
asEmitter.once([type], listener) ⇒ Emitter
Kind: static method of asEmitter
Returns: Emitter
- The emitter.
Emits: :on
, Emitter#:maxListeners
const greeter = new Emitter();
greeter.once( () => console.log( 'Greeted' ) );
greeter.emit( 'hello' );
// Greeted
greeter.emit( 'goodbye' );event:
Since: 1.0.0
Param | Type | Description |
---|---|---|
[type] | EventType |
The event type. |
listener | EventListener |
The event callback. |
Example (Listen once to all event types)
const greeter = new Emitter();
greeter.once( 'hello', ( name ) => console.log( `Hello, ${ name }!` ) );
greeter.emit( 'hello', 'World' );
// Hello, World!
greeter.emit( 'hello', 'World' );
asEmitter.setMaxListeners(max) ⇒ Emitter
By default Emitter will emit a :maxListeners
evet if more than 10 listeners are added for a particular event type
. This method allows that to be changed. Set to 0 for unlimited.
Kind: static method of asEmitter
Returns: Emitter
- The emitter.
Since: 2.0.0
Param | Type | Description |
---|---|---|
max | number |
The maximum number of listeners before a warning is issued. |
Example
const greeter = new Emitter();
greeter.setMaxListeners( 1 );
greeter.on( ':maxListeners', ( greeting ) => console.log( `Greeting "${ greeting }" has one too many!` ) );
greeter.on( 'hello', () => console.log( 'Hello!' ) );
greeter.on( 'hello', () => alert( 'Hello!' ) );
// Greeting "hello" has one too many!
asEmitter.tick(type, [...data]) ⇒ Promise
Asynchronously emits specified event type
with the supplied arguments. The listeners will still be synchronously executed in the specified order.
The type
can be namespaced using :
, which will result in multiple events being triggered in succession. Listeners can be associated with the fully namespaced type
or a subset of the type
.
Returns promise which resolves if the event had listeners, rejects otherwise.
Kind: static method of asEmitter
Returns: Promise
- A promise which resolves if the event had listeners, rejects otherwise.
Since: 2.0.0
Param | Type | Description |
---|---|---|
type | EventType |
The event type. |
[...data] | * |
The data passed into the listeners. |
Example (Asynchronously emitting an event)
const greeter = new Emitter();
greeter.on( 'hello', () => console.log( 'Hello!' ) );
greeter.tick( 'hello' ).then( ( heard ) => console.log( 'hello heard? ', heard ) );
greeter.tick( 'goodbye' ).then( ( heard ) => console.log( 'goodbye heard? ', heard ) );
// Hello!
// hello heard? true
// goodbye heard? false
asEmitter.trigger([type], data) ⇒ boolean
Execute the listeners for the specified event type
with the supplied data
.
Returns true
if the event had listeners, false
otherwise.
Kind: static method of asEmitter
Returns: boolean
- Whether or not the event had listeners.
Since: 1.0.0
Param | Type | Description |
---|---|---|
[type] | EventType |
The event type. |
data | Array |
Example
const greeter = new Emitter();
greeter.on( 'hello', ( name ) => console.log( `Hello, ${ name }!` ) );
greeter.trigger( 'hello', [ 'World' ] );
// Hello, World!
Example
const greeter = new Emitter();
greeter.on( 'greeting:hello', ( name ) => console.log( `Hello, ${ name }!` ) );
greeter.on( 'greeting:hi', ( name ) => console.log( `Hi, ${ name }!` ) );
greeter.on( 'greeting', ( name ) => console.log( `${ name } was greeted.` );
greeter.trigger( 'greeting:hi', [ 'Mark' ] );
// Hi, Mark!
// Mark was greeted.
greeter.trigger( 'greeting:hello', [ 'Jeff' ] );
// Hello, Jeff!
// Jeff was greeted.
asEmitter.until([type], listener) ⇒ Emitter
Adds a listeners for the specified event type
that will be triggered until the listener
returns true
. If no type
is given the listener will be triggered any event type
.
No checks are made to see if the listener
has already been added. Multiple calls passing the same combination type
and listener
will result in the listener
being added multiple times.
Kind: static method of asEmitter
Returns: Emitter
- The emitter.
Since: 1.2.0
Param | Type | Description |
---|---|---|
[type] | EventType |
The event type. |
listener | EventListener |
The event callback. |
Example
const greeter = new Emitter();
greeter.until( function( name ){
console.log( `Greeted ${ name }` );
return name === 'Terry';
} );
greeter.emit( 'hello', 'Jeff' );
// Greeted Jeff
greeter.emit( 'goodbye', 'Terry' );
// Greeted Terry
greeter.emit( 'hi', 'Aaron' );
Example
const greeter = new Emitter();
greeter.until( 'hello', function( name ){
console.log( `Hello, ${ name }!` );
return name === 'World';
} );
greeter.emit( 'hello', 'Jeff' );
// Hello, Jeff!
greeter.emit( 'hello', 'World' );
// Hello, World!
greeter.emit( 'hello', 'Mark' );
Kind: inner method of Emitter
Param | Type | Description |
---|---|---|
emitter | Emitter |
The emitter on which the event would be emitted. |
type | EventType |
The event type. |
listener | EventListener |
The event callback. |
Kind: inner method of Emitter
Param | Type | Description |
---|---|---|
emitter | Emitter |
The emitter on which the event would be emitted. |
type | EventType |
The event type. |
listener | EventListener |
The event callback. |
index | number |
Kind: inner method of Emitter
Param | Type | Description |
---|---|---|
emitter | Emitter |
The emitter on which the event would be emitted. |
type | EventType |
The event type. |
times | number |
The number times the listener will be executed before being removed. |
listener | EventListener |
The event callback. |
Kind: inner method of Emitter
Param | Type | Description |
---|---|---|
emitter | Emitter |
The emitter on which the event would be emitted. |
mapping | EventMapping |
The event mapping. |
Kind: inner method of Emitter
Param | Type | Description |
---|---|---|
emitter | Emitter |
The emitter on which the property will be created. |
Emitter~emitAllEvents(emitter, type, data) ⇒ boolean
Kind: inner method of Emitter
Returns: boolean
- Whether or not a listener for the given event type was executed.
Throws:
Error
Iftype
iserror
and no listeners are subscribed.
Param | Type | Description |
---|---|---|
emitter | Emitter |
The emitter on which the event type will be emitted. |
type | EventType |
The event type. |
data | Array |
The data to be passed with the event. |
Kind: inner method of Emitter
Param | Type | Description |
---|---|---|
emitter | Emitter |
The emitter on which the errors will be emitted. |
errors | Array.<Error> |
The array of errors to be emitted. |
Emitter~emitEvent(emitter, type, data, emitEvery) ⇒ boolean
Kind: inner method of Emitter
Returns: boolean
- Whether or not a listener for the given event type was executed.
Throws:
Error
Iftype
iserror
and no listeners are subscribed.
Param | Type | Description |
---|---|---|
emitter | Emitter |
The emitter on which the event type will be emitted. |
type | EventType |
The event type. |
data | Array |
The data to be passed with the event. |
emitEvery | boolean |
Whether or not listeners for all types will be executed. |
Executes a listener using the internal execute*
functions based on the number of arguments.
Kind: inner method of Emitter
Param | Type |
---|---|
listener | Array.<Listener> | Listener |
data | Array |
scope | * |
Emitter~getEventTypes(emitter) ⇒ Array.<EventType>
Kind: inner method of Emitter
Returns: Array.<EventType>
- The list of event types registered to the emitter.
Param | Type | Description |
---|---|---|
emitter | Emitter |
The emitter on which event types will be retrieved. |
Emitter~getMaxListeners(emitter) ⇒ number
Kind: inner method of Emitter
Returns: number
- The maximum number of listeners.
Param | Type | Description |
---|---|---|
emitter | Emitter |
The emitter on which max listeners will be retrieved. |
Emitter~isPositiveNumber(number) ⇒ boolean
Checks whether or not a value is a positive number.
Kind: inner method of Emitter
Returns: boolean
- Whether or not the value is a positive number.
Param | Type | Description |
---|---|---|
number | * |
The value to be tested. |
Execute a listener with no arguments.
Kind: inner method of Emitter
Param | Type | Description |
---|---|---|
handler | EventListener | Array.<EventListener> |
One or more listeners that will be executed on the emitter . |
isFunction | boolean |
Whether or not the handler is a function. |
emitter | Emitter |
The emitter. |
Execute a listener with one argument.
Kind: inner method of Emitter
Param | Type | Description |
---|---|---|
handler | EventListener | Array.<EventListener> |
One or more listeners that will be executed on the emitter . |
isFunction | boolean |
Whether or not the handler is a function. |
emitter | Emitter |
The emitter. |
arg1 | * |
The first argument. |
Execute a listener with two arguments.
Kind: inner method of Emitter
Param | Type | Description |
---|---|---|
handler | EventListener | Array.<EventListener> |
One or more listeners that will be executed on the emitter . |
isFunction | boolean |
Whether or not the handler is a function. |
emitter | Emitter |
The emitter. |
arg1 | * |
The first argument. |
arg2 | * |
The second argument. |
Execute a listener with three arguments.
Kind: inner method of Emitter
Param | Type | Description |
---|---|---|
handler | EventListener | Array.<EventListener> |
One or more listeners that will be executed on the emitter . |
isFunction | boolean |
Whether or not the handler is a function. |
emitter | Emitter |
The emitter. |
arg1 | * |
The first argument. |
arg2 | * |
The second argument. |
arg3 | * |
The third argument. |
Execute a listener with four or more arguments.
Kind: inner method of Emitter
Param | Type | Description |
---|---|---|
handler | EventListener | Array.<EventListener> |
One or more listeners that will be executed on the emitter . |
isFunction | boolean |
Whether or not the handler is a function. |
emitter | Emitter |
The emitter. |
args | Array |
Four or more arguments. |
Kind: inner method of Emitter
Param | Type | Description |
---|---|---|
emitter | Emitter |
The emitter on which the event would be emitted. |
type | EventType |
The event type. |
listener | EventListener |
The event callback. |
Kind: inner method of Emitter
Param | Type | Description |
---|---|---|
The | Emitter |
emitter on which the maximum number of listeners will be set. |
max | number |
The maximum number of listeners before a warning is issued. |
Faster than Array.prototype.splice
Kind: inner method of Emitter
Param | Type |
---|---|
list | Array |
index | number |
Asynchronously executes a function.
Kind: inner method of Emitter
Param | Type | Description |
---|---|---|
callback | Function |
The function to be executed. |
Emitter~tickAllEvents(emitter, type, data) ⇒ Promise
Kind: inner method of Emitter
Returns: Promise
- A promise which resolves if the event had listeners, rejects otherwise.
Param | Type | Description |
---|---|---|
emitter | Emitter |
The emitter on which the event type will be asynchronously emitted. |
type | EventType |
The event type. |
data | Array |
The data to be passed with the event. |
Applies a selection
of the Emitter.js API to the target
.
Kind: inner method of Emitter
Param | Type | Description |
---|---|---|
[selection] | APIReference |
A selection of the Emitter.js API. |
target | Object |
The object on which the API will be applied. |
A set of method references to the Emitter.js API.
Kind: global typedef
Example (A selection reference)
'emit off on once'
Example (A mapping reference)
// 'emit()' will be mapped to 'fire()'
// 'on()' will be mapped to 'addListener()'
// 'off()' will be mapped to 'removeListener()'
{
fire: 'emit',
addListener: 'on',
removeListener: 'off'
}
EventListener : Function
A function bound to an emitter event type. Any data transmitted with the event will be passed into the listener as arguments.
Kind: global typedef
Param | Type | Description |
---|---|---|
...data | * |
The arguments passed by the emit . |
EventMapping : Object
An object that maps event types to event listeners.
A string or symbol that represents the type of event fired by the Emitter.
JavaScript Array
Kind: global external
See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array
JavaScript primitive boolean
Kind: global external
See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean
JavaScript Error
Kind: global external
See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error
JavaScript Function
Kind: global external
See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function
JavaScript primitive number
Kind: global external
See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number
JavaScript null
Kind: global external
See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/null
JavaScript Object
Kind: global external
See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object
JavaScript Promise
Kind: global external
See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise
JavaScript primitive string
Kind: global external
See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String
JavaScript primitive symbol
Kind: global external
See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol