Class Eventbus
Index
Constructors
Accessors
Methods
Properties
Constructors
constructor
Provides a constructor which optionally takes the eventbus name.
Parameters
Optional
name: stringOptional eventbus name.
Returns Eventbus
Accessors
callbackCount
- get callbackCount(): number
Returns the current callback count.
Returns number
The current callback count.
eventCount
- get eventCount(): number
Returns the current event count.
Returns number
Returns the current event count.
name
- get name(): string
Returns the current eventbus name.
Returns string
The current eventbus name.
Methods
before
- before(
count: number,
name: string | EventMap,
callback: object | Function,
context?: object,
options?: EventOptionsOut | EventOptions,
): EventbusJust like
on
, but causes the bound callback to fire several times up to the count specified before being removed. When multiple events are passed in using the space separated syntax, the event will fire count times for every event you passed in, not once for a combination of all events.Parameters
- count: number
Number of times the function will fire before being removed.
- name: string | EventMap
Event name(s) or event map.
- callback: object | Function
Event callback function or context for event map.
Optional
context: objectEvent context
Optional
options: EventOptionsOut | EventOptionsEvent registration options.
Returns Eventbus
This Eventbus instance.
- count: number
entries
Returns an iterable for all stored events yielding an array with event name, callback function, and event context.
Parameters
Optional
regex: RegExpOptional regular expression to filter event names.
Returns Generator<[string, Function, object, EventOptionsOut], void, unknown>
Generator
getOptions
Returns the options of an event name.
Parameters
- name: string
Event name(s) to verify.
Returns EventOptionsOut
The event options.
- name: string
getType
Returns the trigger type of event name. Note: if trigger type is not set then undefined is returned for type otherwise 'sync' or 'async' is returned.
Parameters
- name: string
Event name(s) to verify.
Returns string
The trigger type.
- name: string
isGuarded
Returns whether an event name is guarded.
Parameters
- name: string | EventMap
Event name(s) or event map to verify.
Optional
data: objectStores the output of which names are guarded.
Returns boolean
Whether the given event name is guarded.
- name: string | EventMap
keys
keysWithOptions
Returns an iterable for the event names / keys of registered event listeners along with event options.
Parameters
Optional
regex: RegExpOptional regular expression to filter event names.
Returns Generator<[string, EventOptionsOut], void, unknown>
Generator
listenTo
Tell an object to listen to a particular event on another object. The advantage of using this form, instead of other.on(event, callback, object), is that listenTo allows the object to keep track of the events, and they can be removed all at once later on. The callback will always be called with object as context.
Parameters
Returns Eventbus
This Eventbus instance.
listenToBefore
listenToOnce
off
Remove a previously-bound callback function from an object. If no context is specified, all the versions of the callback with different contexts will be removed. If no callback is specified, all callbacks for the event will be removed. If no event is specified, callbacks for all events will be removed.
Note that calling model.off(), for example, will indeed remove all events on the model.
Parameters
Returns Eventbus
This Eventbus instance.
Example
// Removes just the `onChange` callback.
object.off('change', onChange);
// Removes all 'change' callbacks.
object.off('change');
// Removes the `onChange` callback for all events.
object.off(null, onChange);
// Removes all callbacks for `context` for all events.
object.off(null, null, context);
// Removes all callbacks on `object`.
object.off();
on
- on(
name: string | EventMap,
callback: object | Function,
context?: object,
options?: EventOptionsOut | EventOptions,
): EventbusBind a callback function to an object. The callback will be invoked whenever the event is fired. If you have a large number of different events on a page, the convention is to use colons to namespace them: 'poll:start', or 'change:selection'.
To supply a context value for this when the callback is invoked, pass the optional last argument:
model.on('change', this.render, this)
ormodel.on({change: this.render}, this)
.Parameters
- name: string | EventMap
Event name(s) or event map.
- callback: object | Function
Event callback function or context for event map.
Optional
context: objectEvent context
Optional
options: EventOptionsOut | EventOptionsEvent registration options.
Returns Eventbus
This Eventbus instance.
Example
// The event string may also be a space-delimited list of several events...
book.on('change:title change:author', ...); - name: string | EventMap
once
- once(
name: string | EventMap,
callback: object | Function,
context?: object,
options?: EventOptionsOut | EventOptions,
): EventbusJust like
on
, but causes the bound callback to fire only once before being removed. Handy for saying "the next time that X happens, do this". When multiple events are passed in using the space separated syntax, the event will fire once for every event you passed in, not once for a combination of all eventsParameters
- name: string | EventMap
Event name(s) or event map.
- callback: object | Function
Event callback function or context for event map.
Optional
context: objectEvent context.
Optional
options: EventOptionsOut | EventOptionsEvent registration options.
Returns Eventbus
This Eventbus instance.
- name: string | EventMap
stopListening
Tell an object to stop listening to events. Either call stopListening with no arguments to have the object remove all of its registered callbacks ... or be more precise by telling it to remove just the events it's listening to on a specific object, or a specific event, or just a specific callback.
Parameters
- obj: object
Event context
Optional
name: stringEvent name(s)
Optional
callback: FunctionEvent callback function
Returns Eventbus
This Eventbus instance.
- obj: object
trigger
Trigger callbacks for the given event, or space-delimited list of events. Subsequent arguments to trigger will be passed along to the event callbacks.
Parameters
- name: string
Event name(s)
- ...args: any[]
Additional arguments passed to the event function(s).
Returns Eventbus
This Eventbus instance.
- name: string
triggerAsync
Provides
trigger
functionality, but collects any returned Promises from invoked targets and returns a single Promise generated byPromise.resolve
for a single value orPromise.all
for multiple results. This is a very useful mechanism to invoke asynchronous operations over an eventbus.Parameters
- name: string
Event name(s)
- ...args: any[]
Additional arguments passed to the event function(s).
Returns Promise<any>
A Promise with any results.
- name: string
triggerDefer
Defers invoking
trigger
. This is useful for triggering events in the next clock tick.Parameters
- name: string
Event name(s)
- ...args: any[]
Additional arguments passed to the event function(s).
Returns Eventbus
This Eventbus instance.
- name: string
triggerSync
Provides
trigger
functionality, but collects any returned result or results from invoked targets as a single value or in an array and passes it back to the callee in a synchronous manner.Parameters
- name: string
Event name(s).
- ...args: any[]
Additional arguments passed to the event function(s).
Returns any
The results of the event invocation.
- name: string
Provides the ability to bind and trigger custom named events. Bound callback functions may be triggered asynchronously or synchronously returning results.