Events Class
Index
Methods
- Events constructor
- _getEvents
- _getOnceReturnValue
- addListener
- addListeners
- addOnceListener
- alias
- defineEvent
- defineEvents
- emit
- flattenListeners
- getListeners
- getListenersAsObject
- indexOfListener
- isValidListener
- manipulateListeners
- removeEvent
- removeListener
- removeListeners
- setOnceReturnValue
- triggerEvent
Methods
_getEvents
()
Object
Fetches the events object and creates one if required.
Returns:
The events storage object.
_getOnceReturnValue
()
| Boolean
Fetches the current value to check against when executing listeners. If the listeners return value matches this one then it should be removed automatically. It will return true by default.
Returns:
The current value to check for or the default, true.
addListener
(
-
evt
-
listener
)
Object
-
evt -
listener
Adds a listener function to the specified event. The listener will not be added if it is a duplicate. If the listener returns true then; it will be removed after it is called. If you pass a regular expression as the event name then; the listener will be added to all events that match it.
Parameters:
-
evtString | RegExpName of the event to attach the listener to.
-
listenerFunctionMethod to be called when the event is emitted. If the function returns true then it will be removed after calling.
Returns:
Current instance of EventEmitter for chaining.
addListeners
(
-
evt
-
[listeners]
)
Object
-
evt -
[listeners]
Adds listeners in bulk using the manipulateListeners method. If you pass an object as the first argument you can add to multiple events at once. The object should contain key value pairs of events and listeners or listener arrays. You can also pass it an event name and an array of listeners to be added. Can pass it a regular expression to add the array of listeners to all events that match it. Yeah, this function does quite a bit. That's probably a bad thing.
Parameters:
-
evtString | Object | RegExpAn event name if you will pass an array of listeners next. An object if you wish to add to multiple events at once.
-
[listeners]Function optionalAn optional array of listener functions to add.
Returns:
Current instance of EventEmitter for chaining.
addOnceListener
(
-
evt
-
listener
)
Object
-
evt -
listener
Semi-alias of addListener. It will add a listener that will be automatically removed after its first execution.
Parameters:
-
evtString | RegExpName of the event to attach the listener to.
-
listenerFunctionMethod to be called when the event is emitted. If the function returns true then it will be removed after calling.
Returns:
Current instance of EventEmitter for chaining.
alias
(
-
name
)
Function
-
name
Alias a method while keeping the context correct, to allow for overwriting of target method.
Parameters:
-
nameStringThe name of the target method.
Returns:
The aliased method
defineEvent
(
-
evt
)
Object
-
evt
Defines an event name. This is required if you want to use a regex to add a listener to multiple events at once. If you don't do this then how do you expect it to know what event to add to? Should it just add to every possible match for a regex? No. That is scary and bad. You need to tell it what event names should be matched by a regex.
Parameters:
-
evtStringName of the event to create.
Returns:
Current instance of EventEmitter for chaining.
defineEvents
(
-
evts
)
Object
-
evts
Uses defineEvent to define multiple events.
Parameters:
-
evtsStringAn array of event names to define.
Returns:
Current instance of EventEmitter for chaining.
emit
(
-
evt
-
Optional
)
Object
-
evt -
Optional
Subtly different from emitEvent in that it will pass its arguments on to the listeners, as opposed to taking a single array of arguments to pass on. you can pass a regex in place of the event name to emit to all events that match it.
Parameters:
-
evtString | RegExpName of the event to emit and execute listeners for.
-
Optional...additional arguments to be passed to each listener.
Returns:
Current instance of EventEmitter for chaining.
flattenListeners
(
-
listeners
)
Function
-
listeners
Takes a list of listener objects and flattens it into a list of listener functions.
Parameters:
-
listenersObjectRaw listener objects.
Returns:
Just the listener functions.
getListeners
(
-
evt
)
Function | Object
-
evt
Returns the listener array for the specified event. Will initialise the event object and listener arrays if required. Will return an object if you use a regex search. The object contains keys for each matched event. So /ba[rz]/ might return an object containing bar and baz. But only if you have either defined them with defineEvent or added some listeners to them. Each property in the object response is an array of listener functions.
Parameters:
-
evtString | RegExpName of the event to return the listeners from.
Returns:
All listener functions for the event.
getListenersAsObject
(
-
evt
)
Object
-
evt
Fetches the requested listeners via getListeners but will always return the results inside an object. This is mainly for internal use but others may find it useful.
Parameters:
-
evtString | RegExpName of the event to return the listeners from.
Returns:
All listener functions for an event in an object.
indexOfListener
(
-
listeners
-
listener
)
Number
-
listeners -
listener
Finds the index of the listener for the event in its storage array.
Parameters:
-
listenersFunctionArray of listeners to search through.
-
listenerFunctionMethod to look for.
Returns:
Index of the specified listener, -1 if not found
isValidListener
(
-
listener
)
-
listener
Parameters:
-
listenerObject
Returns:
manipulateListeners
(
-
remove
-
evt
-
[listeners]
)
Object
-
remove -
evt -
[listeners]
Edits listeners in bulk. The addListeners and removeListeners methods both use this to do their job. You should really use those instead, this is a little lower level. The first argument will determine if the listeners are removed (true) or added (false). If you pass an object as the second argument you can add/remove from multiple events at once. The object should contain key value pairs of events and listeners or listener arrays. You can also pass it an event name and an array of listeners to be added/removed. You can pass it a regular expression to manipulate the listeners of all events that match it.
Parameters:
-
removeBooleanTrue if you want to remove listeners, false if you want to add.
-
evtString | Object | RegExpAn event name if you will pass an array of listeners next.
-
[listeners]Function optionalAn optional array of listener functions to add/remove.
Returns:
Current instance of EventEmitter for chaining.
removeEvent
(
-
[evt]
)
Object
-
[evt]
Removes all listeners from a specified event. If you do not specify an event then all listeners will be removed. That means every event will be emptied. You can also pass a regex to remove all events that match it.
Parameters:
-
[evt]String | RegExp optionalOptional name of the event to remove all listeners for.
Returns:
Current instance of EventEmitter for chaining.
removeListener
(
-
evt
-
listener
)
Object
-
evt -
listener
Removes a listener function from the specified event. When passed a regular expression as the event name, it will remove the listener from all events that match it.
Parameters:
-
evtString | RegExpName of the event to remove the listener from.
-
listenerFunctionMethod to remove from the event.
Returns:
Current instance of EventEmitter for chaining.
removeListeners
(
-
evt
-
[listeners]
)
Object
-
evt -
[listeners]
Removes listeners in bulk using the manipulateListeners method. If you pass an object as the first argument you can remove from multiple events at once. The object should contain key value pairs of events and listeners or listener arrays. You can also pass it an event name and an array of listeners to be removed. You can also pass it a regular expression to remove the listeners from all events that match it.
Parameters:
-
evtString | Object | RegExpAn event name if you will pass an array of listeners next. An object if you wish to remove from multiple events at once.
-
[listeners]Function optionalAn optional array of listener functions to remove.
Returns:
Current instance of EventEmitter for chaining.
setOnceReturnValue
(
-
value
)
Object
-
value
Sets the current value to check against when executing listeners. If a listeners return value matches the one set here then it will be removed after execution. This value defaults to true.
Parameters:
-
valueThe new value to check for when executing listeners.
Returns:
Current instance of EventEmitter for chaining.
triggerEvent
(
-
evt
-
[args]
)
Object
-
evt -
[args]
Emits an event of your choice.
When emitted, every listener attached to that event will be executed.
If you pass the optional argument array then those arguments
will be passed to every listener upon execution.
Because it uses apply, your array of arguments will be passed as
if you wrote them out separately.
So they will not arrive within the array on the other side, they will be separate.
You can also pass a regular expression to emit to all events that match it.
Parameters:
-
evtString | RegExpName of the event to emit and execute listeners for.
-
[args]Array optionalOptional array of arguments to be passed to each listener.
Returns:
Current instance of EventEmitter for chaining.