Events
Boson provides several different events. Event subscriptions are hierarchical and are arranged in the following order.
You can subscribe to a specific event at any level, receiving all the corresponding events of the context.
You can read more about each type of event in the corresponding sections of the documentation.
Events and Intentions
Any event is a fact. Events can not be changed or rejected.
Unlike events, an intent is an attempt by the application or any of its components to do something. Any intent can be rejected or modified.
Both events and intentions use a common event bus within the Boson application.
Stop Propagation
To stop the "event bubbling" there is a method Event::stopPropagation()
.
When this method is called, the event will not be delegated to the upper level of event listener and all subsequent subscribers of this level of event listener that were registered after.
The read-only property Event::$isPropagationStopped
(or alternative PSR-compatible method Event::isPropagationStopped()
) is available to check for propagation stop events.
Timestamp
Each event also contains the timestamp Event::$time
read-only property of the event creation. The property contain an int32 value of UNIX timestamp.
Cancellation
Method Intention::cancel()
is used to stop and prevent the standard behavior.
For example, if you call the cancel()
method when trying to stop an application, the application will not be stopped. Therefore, the application stop event will not be called either.
Listener Provider
Each core class such as Application, Window and WebView exposes events using Boson\Dispatcher\EventListenerProviderInterface
.
The provider exposes a property EventListenerProviderInterface::$events
that contains an instance of EventListenerInterface
(described below) and a method EventListenerProviderInterface::on()
that provides a simpler and more convenient way to subscribe to events.
Simple Listen Events
To subscribe to events, it is enough to call the on()
method, which must contain a callback with one parameter, which contains the event type.
For example, to subscribe to the window creation event, you can write the following code.
If you don't need to process the event object, you can use an alternative option by passing the event name as the first parameter and the callback as the second.
Event Listener
The Boson\Dispatcher\EventListenerInterface
provides a way to subscribe to and handle events in the Boson application. It allows you to register callbacks that will be executed when specific events occur.
Listen Events
To subscribe to events, you need to use the EventListenerInterface::addEventListener()
method:
The first parameter is the event class you want to listen to, and the second parameter is a callback function that will be executed when the event occurs.
Cancel Subscription
Each EventListenerInterface::addEventListener()
returns the SubscriptionInterface
object. To cancel a subscription, use the cancel()
method.
Cancel All Subscriptions
To cancel all existing event subscriptions of a certain type, call the removeAllEventListenersForEvent()
method.