-
Notifications
You must be signed in to change notification settings - Fork 252
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(sdk): Implement Client::observe_events
and Client::observe_room_events
#4253
feat(sdk): Implement Client::observe_events
and Client::observe_room_events
#4253
Conversation
53b11d5
to
174191e
Compare
Client::observe_room_events
Client::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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 patch introduces a mechanism similar to
Client::add_event_handler
andClient::add_room_event_handler
but with a reactive programming pattern. This patch adds
Client::observe_events
andClient::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
ObservableEventHandler
andEventHandlerSubscriber
to learn more about how it works.In order to achieve that, it was necessary to implement
EventHandlerContext
on tuples where each part implements
EventHandlerContext
. Why? Because,while
EventHandler
is 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_events
remains“constant”: