new EventDispatcher()
Creates a new event dispatcher.
Methods
-
addEventListener(type, listener)
-
Adds or replaces a listener function.
This method returns the listener function which allows a caller to use an unnamed or arrow function defined within the call and still store it for later removal.
Parameters:
Name Type Description typestring The type of event to listen to.
listenerfunction The function that gets called when the event is fired.
Returns:
Returns the listener function so it can be stored for later removal.
- Type
- function
Example
let event_listener; onMounted() { event_listener = PD.GlobalEvents.addEventListener(PD.EVENT.SITE_LOCATION, (location) => { this.doSomethingWithTheLocation(location); }); }; onUnmounted() { if (event_listener) { PD.GlobalEvents.removeEventListener(PD.EVENT.SITE_LOCATION, event_listener); event_listener = null; } }; -
dispatch(type, args)
-
Invokes all the listeners for the given event type with the given arguments.
NOTE: This method is different to the standard
dispatchEvent()method onTHREE.EventDispatcherthat you may be used to. The THREE.js method requires a bespoke event object with the event arguments as properties of that object. It also modifies that object by adding an additionaltargetproperty prior to sending, and creates a copy of the listener array on every call.In our case, events may be generated many times a second as the view, location and/or sun-position are changed dynamically. The listener methods are all called in a tight loop and, given the nature of framework events, they do not remove themselves when called, so changes to the listener array during iteration are unlikely enough for the risk to be tolerable. Also, the dispatching object is typically static and globally available, so there is no need to send a reference to each listener. Whilst having to create a transitory event object, modify it and then copy the array may be a relatively small overhead, it is still an unnecessary one.
As a result, this method simply passes on the given event arguments to each listener without requiring a custom object, modifying it or copying the listener array each time.
Parameters:
Name Type Argument Description typestring The event type identifier.
argsany <repeatable>
The additional arguments that correspond to the given event type.
Returns:
Returns true if at least one event listener was invoked, otherwise false.
- Type
- boolean
-
hasEventListener(type, listener)
-
Determines if the given listener function is associated with the given event type.
Parameters:
Name Type Description typestring The type of event to listen to.
listenerfunction The function that gets called when the event is fired.
Returns:
Returns true if the listener function exists.
- Type
- boolean
-
hasEventListeners(type)
-
Determines if an event has any registered listeners.
Parameters:
Name Type Description typestring The type of event to check.
Returns:
Returns true if there is more than one listener for the given event.
- Type
- boolean
-
removeEventListener(type, listener)
-
Removes the given listener function from the given event type.
Parameters:
Name Type Description typestring The type of event to listen to.
listenerfunction The function that gets called when the event is fired.
Returns:
Returns true if the listener function was found and removed.
- Type
- boolean