feat(sdk): Implement Client::observe_events and Client::observe_room_events#4253
Merged
Hywan merged 3 commits intomatrix-org:mainfrom Nov 13, 2024
Merged
Conversation
53b11d5 to
174191e
Compare
Client::observe_room_eventsClient::observe_events and Client::observe_room_events
This patch implements `EventHandlerContext` for tuples where each part implements `EventHandlerContext` itself.
174191e to
e817a85
Compare
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #4253 +/- ##
==========================================
+ Coverage 84.90% 84.93% +0.02%
==========================================
Files 274 274
Lines 29761 29795 +34
==========================================
+ Hits 25270 25307 +37
+ Misses 4491 4488 -3 ☔ View full report in Codecov by Sentry. |
bc2d18d to
e0d6d21
Compare
jmartinesp
approved these changes
Nov 13, 2024
Contributor
jmartinesp
left a comment
There was a problem hiding this comment.
Thanks, the changes LGTM in general since I assume it's correct, but maybe a second pair of more experienced eyes wouldn't hurt since I'm not familiar with some parts of it.
poljar
approved these changes
Nov 13, 2024
Contributor
poljar
left a comment
There was a problem hiding this comment.
Very nice. Quite the clever trick on how to make this pattern generic, great work.
I left some small nits about the documentation but otherwise this is good to go.
…om_events`.
Changelog: This patch introduces a mechanism similar to
`Client::add_event_handler` and `Client::add_room_event_handler`
but with a reactive programming pattern. This patch adds
`Client::observe_events` and `Client::observe_room_events`.
```rust
// Get an observer.
let observer =
client.observe_events::<SyncRoomMessageEvent, (Room, Vec<Action>)>();
// Subscribe to the observer.
let mut subscriber = observer.subscribe();
// Use the subscriber as a `Stream`.
let (message_event, (room, push_actions)) = subscriber.next().await.unwrap();
```
When calling `observe_events`, one has to specify the type of event
(in the example, `SyncRoomMessageEvent`) and a context (in the example,
`(Room, Vec<Action>)`, respectively for the room and the push actions).
ab4b0d0 to
5649704
Compare
5649704 to
e2b1818
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This patch introduces a mechanism similar to
Client::add_event_handlerandClient::add_room_event_handlerbut with a reactive programming pattern. This patch adds
Client::observe_eventsandClient::observe_room_events.When calling
observe_events, one has to specify the type of event(in the example,
SyncRoomMessageEvent) and a context (in the example,(Room, Vec<Action>), respectively for the room and the push actions).Read the documentation of the
ObservableEventHandlerandEventHandlerSubscriberto learn more about how it works.In order to achieve that, it was necessary to implement
EventHandlerContexton tuples where each part implements
EventHandlerContext. Why? Because,while
EventHandleris implemented forFnOnce()with 0 to 8 arguments(e.g. with 2 arguments
client.add_event_handler(|ev: E, ctx1: C1, ctx2: C2| { … })),it is not possible to have “variadic” generic parameters. Example, it's not possible
to write
client.observe_events::<E, C1, C2>(). The trick is to use tuples (!), like:client.observe_events::<E, (C1, C2)>(): the type ofobserve_eventsremains“constant”: