This is the main type class, an instance of event listener allows to subscribe and dispatche events.
Creates a new instance of EventListener.
-
name: string The name of the event.
-
options: Partial<EventListenerOptions> (optional) Option settings.
It subscribes a listener function to the event.
The listener will be executed when the event will be dispatched.
- fn: ListenerFunction A listener function to be executed when the event is dispatched.
- options: Partial<SubscribeOptions> (optional) Option settings.
True if the subscribe had success, false otherwise.
It unsubscribes a function listener, the function must be the same used in subscribe.
- fn: ListenerFunction The function listener you want to unsubscribe.
- options: Partial<UnsubscribeOptions> (optional) Option settings.
True if the unsubscribe had success, false otherwise.
It dispatch an event, all subscribed listener function will be executed in order or by priority passing sender and data as parameters.
-
sender: unknown Who is dispatching the event, usually you need to pass this, in fact you can pass any kind of data.
-
data: unknown Any kind of data you want to pass to the listener function.
-
options: Partial<DispatchOptions> (optional) Option settings.
It allows to subscribe a function using a key as reference, so you don't need to keep a reference to the function.
- fn: ListenerFunction The function listener you want to subscribe.
- key: string The key you want use as reference for this subscribe.
- options:Partial<SubscribeWithKeyOptions> (optional) Option settings.
True if the subscribe had success, false otherwise.
It allows to unsubscribe a set of function listeners using a key as reference.
- key:string The key you want to use as reference for this unsubscribe.
- options:Partial<UnubscribeWithKeyOptions> (optional) Option settings.
True if the unsubscribe had success, false otherwise.
If the event has never dispatched before, it waits until the first dispatch occurs, otherwise it will complete without waiting.
The beaviour can be modified to wait next dispatch in any case.
- options:Partial<WaitUntilFirstDispatchOptions> (optional) Option settings.
Per default it returs undefined, but if the dispatch is configured to store data, it will return the latest data passed to the dispatch.
It binds this event A to another event B, when event B is dispatched, also event A will be dispatched. Internally event A subscribes to event B and propagates the dispatch.
- options:<EventListener> *The event you want to bind to.
- options:Partial<BindToEventOptions> (optional) Option settings.
True if it has binded successfully, otherwise false.
It unbinds this event from another binded event.
It attachs an event B to this event A, when event A is dispatched, also event B will be dispatched. It works like binding but in reverse way-.
- options:<EventListener> *The event you want to attach to.
- options:Partial<BindToEventOptions> (optional) Option settings.
True if it has been attached successfully, otherwise false.
It detachs the even, it works like unbind but in reverse way.
- options:<EventListener> *The event you want to detach.
- options:Partial<UnbindFromEventOptions> (optional) Option settings.
True if it has detached successfully, otherwise false.
This is the type signature of a function listener that can be used to subscribe to an event as callback.
- sender: unknown Who dispatched the event.
- data: unknown Data that can be passed when the event is dispatched.
The interface for a JSON object used to pass the configuration to EventListener constructor.
Name | Type | Default | Description |
---|---|---|---|
logger | Logger | console | NullLogger | It allows to set a custom logger, as default console is used if supported, otherwise loging will be disabled. |
The interface for a JSON object used to pass the configuration to subscribe.
Name | Type | Default | Description |
---|---|---|---|
shouldThrowErrors | boolean | false | If set to true subscribe will throw an error if fails, otherwise it will just return false. |
allowMultipleSubscribeSameFunction | boolean | false | If set to true allows to use multiple times the same function as listener.e The usage is discouraged because you lose the only unique reference with the listener. |
priority | Number | null | If set to a positive values it gives an higher priority to the listener function, if negative a lower priority. |
The interface for a JSON object used to pass the configuration to unsubscribe.
Name | Type | Default | Description |
---|---|---|---|
shouldThrowErrors | boolean | false | If set to true unsubscribe will throw an error if fails, otherwise it will just return false. |
removeOnlyFirstOccurrence | boolean | true | If set to false, in the case the same function is subscribed multiple times, it will remove all the occurence. Consider the usage in case you allow to subscribe multiple times the same function. It can break the order of execution of listeners. |
The interface for a JSON object used to pass the configuration to dispatch.
Name | Type | Default | Description |
---|---|---|---|
shouldThrowErrors | boolean | false | If set to true, in case one of the listeners throws an exeception, will not be catched, so it will stop the execution of all subsequent listeners. The usage is discouraged. |
defer | boolean | false | By default all listeners are executed immediately after dispatch, blocking the execution subsequent code until all listeners are executed. If set to true the execution of listeners will be demanded to javascript event manager and executed as soon as the execution of current task code terminates. Useful if the execution of listeners is particularly slow, in any case the code will be executed on same thread. |
storeData | boolean | false | If set to true, last dispatched data will be stored, to be used and returned by waitUntilFirstDispatchAsync. |
The interface for a JSON object used to pass the configuration to subscribeWithkey.
SubscribeOptions
Same as SubscribeOptions plus:
Name | Type | Default | Description |
---|---|---|---|
allowMultipleListernersPerKey | boolean | true | It allows to subscribe multiple functions with same key. |
The interface for a JSON object used to pass the configuration to unsubscribeWithkey.
UnsubscribeOptions
Same as UnsubscribeOptions plus:
Name | Type | Default | Description |
---|---|---|---|
removeOnlyFirstKeyedListener | boolean | false | If set to true it will remove only the first function listener found, when multiple listeners are subscribed with same key. The usage is discouraged because it is not predictable whan listener will be removed. If you want to unsubscribe just one function you can use unsubscribe passing the function reference. |
The interface for a JSON object used to pass the configuration to waitUntilFirstDispatch.
Name | Type | Default | Description |
---|---|---|---|
resetFirstDispatchBefore | boolean | false | If set to true, it will wait for next dispatch in any case. The status will be reset before checking if dispatched occurred |
resetFirstDispatchAfter | boolean | false | If set to true, after the promise is resolved it will reset the status. If dispatch already occured it will not wait it, but it will wait at next call. |
The interface for a JSON object used to pass the configuration to bindToEvent.
DispatchOptions
Same as DispatchOptions plus:
Name | Type | Default | Description |
---|---|---|---|
priority | Number | null | If set to a positive values it gives an higher priority to the listener function, if negative a lower priority. |
The interface for a JSON object used to pass the configuration to unbindFromEvent.
Name | Type | Default | Description |
---|---|---|---|
shouldThrowErrors | boolean | false | If set to true, in case unbind fails (event is not binded) it will throw error. |