Module jls.util.EventPublisher
Allows to publish and subscribe to events.
Class EventPublisher
EventPublisher:new () | Creates a new EventPublisher. |
eventPublisher:unsubscribeAllEvents () | Unsubscribes all the events. |
eventPublisher:subscribeEvent (name, handler) | Subscribes to the specified event name with the specified function. |
eventPublisher:subscribeEventOnce (name, fn) | Subscribes once to the specified event name with the specified function. |
eventPublisher:eventNames () | Returns the subscribed event names. |
eventPublisher:subscriptionCount (name) | Returns the number of handlers for the specified event name. |
eventPublisher:unsubscribeEvent (name, key) | Unsubscribes the specified event name. |
eventPublisher:publishEvent (name[, ...]) | Publishes the specified event name. |
Class EventPublisher
The EventPublisher class.
The EventPublisher provides a way to subsribe and publish events.
- EventPublisher:new ()
-
Creates a new EventPublisher.
Returns:
-
a new EventPublisher
- eventPublisher:unsubscribeAllEvents ()
- Unsubscribes all the events.
- eventPublisher:subscribeEvent (name, handler)
-
Subscribes to the specified event name with the specified function.
Registers an event handler/listener.
When the handler raises an error, the 'error' event is published,
if there is no handler then the error is propagated.
Parameters:
- name string the event name
- handler the function to call when the event is published
Returns:
-
an opaque key that could be used to unsubscribe
- eventPublisher:subscribeEventOnce (name, fn)
-
Subscribes once to the specified event name with the specified function.
On the first publication, the subscription will be removed prior to call the handler.
Parameters:
- name string the event name
- fn function the function to call when the event is published
Returns:
-
an opaque key that could be used to unsubscribe
- eventPublisher:eventNames ()
-
Returns the subscribed event names.
Returns:
-
table
the event names
- eventPublisher:subscriptionCount (name)
-
Returns the number of handlers for the specified event name.
Parameters:
- name string the event name
Returns:
-
number
the number of handlers
- eventPublisher:unsubscribeEvent (name, key)
-
Unsubscribes the specified event name.
Parameters:
- name string the event name
- key the subscribed key, nil to remove all handlers for the name
- eventPublisher:publishEvent (name[, ...])
-
Publishes the specified event name.
Calls each subscribed handler for the event name in the subscription order.
Parameters:
- name string the event name
- ... the optional parameters to pass to the handler. (optional)
Returns:
-
boolean
true if the event had subscriptions, false otherwise