Skip to content

Commit 2c699cf

Browse files
committed
Improve events page
1 parent 5d9ad84 commit 2c699cf

File tree

4 files changed

+175
-6
lines changed

4 files changed

+175
-6
lines changed

Writerside/topics/application.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,11 +160,32 @@ if ($app1->id->equals($app2->id)) {
160160
The application will automatically emit the following events (and intentions)
161161
during its lifecycle.
162162

163+
To subscribe to events, you can use direct access to the
164+
<a href="events.md#event-listener">event listener</a>, using
165+
`Application::$events` property.
166+
167+
```php
168+
$app->events->addEventListener(Event::class, function(Event $e) {
169+
var_dump($e);
170+
});
171+
```
172+
173+
The application instance also supports a more convenient and simple way of
174+
registering events using the `on()` method.
175+
176+
```php
177+
$app->on(function(Event $event): void {
178+
var_dump($event);
179+
});
180+
```
181+
163182
<note>
164183
More information about events can be found in the <a href="events.md">events
165184
documentation</a>.
166185
</note>
167186

187+
188+
168189
### Starting Intention
169190
<secondary-label ref="intention"/>
170191

Writerside/topics/events.md

Lines changed: 116 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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

7777
The read-only property `Event::$isPropagationStopped` (or alternative
7878
PSR-compatible method `Event::isPropagationStopped()`) is available to check
7979
for 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
107107
stop 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+
```

Writerside/topics/webview.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,25 @@ enum State
150150
The webview will automatically emit the following events (and intentions)
151151
during its lifecycle.
152152

153+
To subscribe to events, you can use direct access to the
154+
<a href="events.md#event-listener">event listener</a>, using
155+
`WebView::$events` property.
156+
157+
```php
158+
$app->events->addEventListener(Event::class, function(Event $e) {
159+
var_dump($e);
160+
});
161+
```
162+
163+
The webview instance also supports a more convenient and simple way of
164+
registering events using the `on()` method.
165+
166+
```php
167+
$app->webview->on(function(Event $event): void {
168+
var_dump($event);
169+
});
170+
```
171+
153172
<note>
154173
More information about events can be found in the <a href="events.md">events
155174
documentation</a>.

Writerside/topics/window.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -837,6 +837,25 @@ be used with caution.
837837
The window will automatically emit the following events (and intentions)
838838
during its lifecycle.
839839

840+
To subscribe to events, you can use direct access to the
841+
<a href="events.md#event-listener">event listener</a>, using
842+
`Window::$events` property.
843+
844+
```php
845+
$app->events->addEventListener(Event::class, function(Event $e) {
846+
var_dump($e);
847+
});
848+
```
849+
850+
The window instance also supports a more convenient and simple way of
851+
registering events using the `on()` method.
852+
853+
```php
854+
$app->window->on(function(Event $event): void {
855+
var_dump($event);
856+
});
857+
```
858+
840859
<note>
841860
More information about events can be found in the <a href="events.md">events
842861
documentation</a>.

0 commit comments

Comments
 (0)