@@ -60,26 +60,26 @@ that were registered after.
6060
6161``` php
6262// ❌ event will NOT fired
63- $parent ->events->addEventListener(Event::class, fn($e) => ...);
63+ $app ->events->addEventListener(Event::class, fn($e) => ...);
6464
6565// ✅ event will be fired (registered BEFORE the propagation stop)
66- $child ->events->addEventListener(Event::class, fn($e) => ...);
66+ $app->window ->events->addEventListener(Event::class, fn($e) => ...);
6767
6868// ✅ event will be fired (callback stops "event bubbling")
69- $child ->events->addEventListener(Event::class, function($e): void {
69+ $app->window ->events->addEventListener(Event::class, function($e): void {
7070 $e->stopPropagation();
7171});
7272
7373// ❌ event will NOT fired (registered AFTER the propagation stop)
74- $child ->events->addEventListener(Event::class, fn($e) => ...);
74+ $app->window ->events->addEventListener(Event::class, fn($e) => ...);
7575```
7676
7777The read-only property ` Event::$isPropagationStopped ` (or alternative
7878PSR-compatible method ` Event::isPropagationStopped() ` ) is available to check
7979for propagation stop events.
8080
8181``` php
82- if ($event->isPropagationStopped) {
82+ if ($app-> event->isPropagationStopped) {
8383 echo 'Event propagation is stopped';
8484}
8585```
@@ -107,7 +107,117 @@ application, the application will not be stopped. Therefore, the application
107107stop event will not be called either.
108108
109109``` php
110- $events->addEventListener(ApplicationStopping::class, function($e): void {
110+ $app-> events->addEventListener(ApplicationStopping::class, function($e) {
111111 $e->cancel();
112112});
113113```
114+
115+ ## Listener Provider
116+
117+ Each core class such as [ Application] ( application.md ) ,
118+ [ Window] ( window.md ) and [ WebView] ( webview.md ) exposes
119+ events using ` Boson\Dispatcher\EventListenerProviderInterface ` .
120+
121+ The provider exposes a property ` EventListenerProviderInterface::$events `
122+ that contains an instance of ` EventListenerInterface ` (described below) and
123+ a method ` EventListenerProviderInterface::on() ` that provides a simpler and
124+ more convenient way to subscribe to events.
125+
126+ ### Simple Listen Events
127+
128+ To subscribe to events, it is enough to call the ` on() ` method, which must
129+ contain a callback with one parameter, which contains the event type.
130+
131+ For example, to subscribe to the window creation event, you can write
132+ the following code.
133+
134+ ``` php
135+ use Boson\Application;
136+ use Boson\Window\Event\WindowCreated;
137+
138+ $app = new Application();
139+
140+ $app->on(function(WindowCreated $e): void {
141+ echo 'Window ' . $e->subject->id . ' has been created';
142+ });
143+ ```
144+
145+ If you don't need to process the event object, you can use an alternative
146+ option by passing the event name as the first parameter and the callback
147+ as the second.
148+
149+ ``` php
150+ use Boson\Application;
151+ use Boson\Window\Event\WindowCreated;
152+
153+ $app = new Application();
154+
155+ $app->on(WindowCreated::class, function(): void {
156+ echo 'Window has been created';
157+ });
158+ ```
159+
160+ <tip >
161+ The second callback parameter also allows the event object to be present
162+ as the first argument.
163+
164+ <code-block lang =" php " >
165+ $app->on(Event::class, function(Event $e) { ... });
166+ </code-block >
167+ </tip >
168+
169+
170+ ## Event Listener
171+
172+ The ` Boson\Dispatcher\EventListenerInterface ` provides a way to subscribe to
173+ and handle events in the Boson application. It allows you to register callbacks
174+ that will be executed when specific events occur.
175+
176+ ### Listen Events
177+
178+ To subscribe to events, you need to use the
179+ ` EventListenerInterface::addEventListener() ` method:
180+
181+ ``` php
182+ $app->events->addEventListener(Event::class, function(Event $event): void {
183+ // Handle the event
184+ });
185+ ```
186+
187+ The first parameter is the event class you want to listen to, and the second
188+ parameter is a callback function that will be executed when the event occurs.
189+
190+ ### Cancel Subscription
191+
192+ Each ` EventListenerInterface::addEventListener() ` returns the
193+ ` SubscriptionInterface ` object. To cancel a subscription,
194+ use the ` cancel() ` method.
195+
196+ ``` php
197+ $subscription = $app->events->addEventListener($event, $callback);
198+
199+ // Cancel subscription
200+ $subscription->cancel();
201+ ```
202+
203+ <tip >
204+ The <code >EventListenerProviderInterface::on()</code > method also returns
205+ a subscription object.
206+
207+ <code-block lang =" PHP " >
208+ $subscription = $app->on(function(Event $e) { ... });
209+
210+ // Cancel subscription
211+ $subscription->cancel();
212+ </code-block >
213+ </tip >
214+
215+ ### Cancel All Subscriptions
216+
217+ To cancel all existing event subscriptions of a certain type,
218+ call the ` removeAllEventListenersForEvent() ` method.
219+
220+ ``` php
221+ // Cancel all EventName subscriptions
222+ $app->events->removeAllEventListenersForEvent(EventName::class);
223+ ```
0 commit comments