Skip to content

Commit c704c2c

Browse files
committed
fix: Add documentation on onserving events.
1 parent 599f6e6 commit c704c2c

File tree

1 file changed

+104
-0
lines changed

1 file changed

+104
-0
lines changed

README.md

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,3 +165,107 @@ for await (const event of client.readEvents('/books/42', {
165165
```
166166

167167
*Note that `fromLatestEvent` and `lowerBound` can not be provided at the same time.*
168+
169+
#### Aborting Reading
170+
171+
If you need to abort reading use `break` or `return` within the `await for` loop. However, this only works if there is currently an iteration going on.
172+
173+
To abort reading independently of that, hand over an abort signal as third parameter when calling `readEvents`, and abort the appropriate `AbortController`:
174+
175+
```typescript
176+
const controller = new AbortController();
177+
178+
for await (const event of client.readEvents('/books/42', {
179+
recursive: false
180+
}, controller.signal)) {
181+
// ...
182+
}
183+
184+
// Somewhere else, abort the controller, which will cause
185+
// reading to end.
186+
controller.abort();
187+
```
188+
189+
### Observing Events
190+
191+
To observe all events of a subject, call the `observeEvents` function with the subject as the first argument and an options object as the second argument. Set the `recursive` option to `false`. This ensures that only events of the given subject are returned, not events of nested subjects.
192+
193+
The function returns an asynchronous iterator, which you can use e.g. inside a `for await` loop:
194+
195+
```typescript
196+
for await (const event of client.observeEvents('/books/42', {
197+
recursive: false
198+
})) {
199+
// ...
200+
}
201+
```
202+
203+
#### Observing From Subjects Recursively
204+
205+
If you want to observe not only all the events of a subject, but also the events of all nested subjects, set the `recursive` option to `true`:
206+
207+
```typescript
208+
for await (const event of client.observeEvents('/books/42', {
209+
recursive: true
210+
})) {
211+
// ...
212+
}
213+
```
214+
215+
This also allows you to observe *all* events ever written. To do so, provide `/` as the subject and set `recursive` to `true`, since all subjects are nested under the root subject.
216+
217+
#### Specifying Bounds
218+
219+
Sometimes you do not want to observe all events, but only a range of events. For that, you can specify the `lowerBound` option.
220+
221+
Specify the ID and whether to include or exclude it:
222+
223+
```typescript
224+
for await (const event of client.observeEvents('/books/42', {
225+
recursive: false,
226+
lowerBound: { id: '100', type: 'inclusive' }
227+
})) {
228+
// ...
229+
}
230+
```
231+
232+
#### Starting From the Latest Event of a Given Type
233+
234+
To observe starting from the latest event of a given type, provide the `fromLatestEvent` option and specify the subject, the type, and how to proceed if no such event exists.
235+
236+
Possible options are `wait-for-event`, which waits for an event of the given type to happen, or `read-everything`, which effectively behaves as if `fromLatestEvent` was not specified:
237+
238+
```typescript
239+
for await (const event of client.observeEvents('/books/42', {
240+
recursive: false,
241+
fromLatestEvent: {
242+
subject: '/books/42',
243+
type: 'io.eventsourcingdb.library.book-borrowed',
244+
ifEventIsMissing: 'read-everything'
245+
}
246+
})) {
247+
// ...
248+
}
249+
```
250+
251+
*Note that `fromLatestEvent` and `lowerBound` can not be provided at the same time.*
252+
253+
#### Aborting Observing
254+
255+
If you need to abort observing use `break` or `return` within the `await for` loop. However, this only works if there is currently an iteration going on.
256+
257+
To abort observing independently of that, hand over an abort signal as third parameter when calling `observeEvents`, and abort the appropriate `AbortController`:
258+
259+
```typescript
260+
const controller = new AbortController();
261+
262+
for await (const event of client.observeEvents('/books/42', {
263+
recursive: false
264+
}, controller.signal)) {
265+
// ...
266+
}
267+
268+
// Somewhere else, abort the controller, which will cause
269+
// observing to end.
270+
controller.abort();
271+
```

0 commit comments

Comments
 (0)