From 57dde2c4d3e7a19b8f2f1dc2a5dd9072570669b8 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Thu, 25 Aug 2022 15:25:29 +0200 Subject: [PATCH] refactor(sdk): Avoid duplicate work and fix event handler call order Previously, when both a possibly-redacted timeline event handler and a non-redacted timeline timeline event handler would apply to multiple events in a sync response, they would individually run for every event in order. With this change, they will instead both be called for one event before the next is processed. --- crates/matrix-sdk/src/event_handler/mod.rs | 30 +++++++--------------- 1 file changed, 9 insertions(+), 21 deletions(-) diff --git a/crates/matrix-sdk/src/event_handler/mod.rs b/crates/matrix-sdk/src/event_handler/mod.rs index 4602e61e5..9a653f14b 100644 --- a/crates/matrix-sdk/src/event_handler/mod.rs +++ b/crates/matrix-sdk/src/event_handler/mod.rs @@ -376,37 +376,25 @@ impl Client { unsigned: Option, } - // Event handlers for possibly-redacted timeline events - for item in timeline_events { - let TimelineEventDetails { event_type, state_key, .. } = item.event.deserialize_as()?; - - let event_kind = match state_key { - Some(_) => EventKind::State, - None => EventKind::MessageLike, - }; - - let raw_event = &item.event.json(); - let encryption_info = item.encryption_info.as_ref(); - - self.call_event_handlers(room, raw_event, event_kind, &event_type, encryption_info) - .await; - } - - // Event handlers specifically for redacted OR unredacted timeline events for item in timeline_events { let TimelineEventDetails { event_type, state_key, unsigned } = item.event.deserialize_as()?; let redacted = unsigned.and_then(|u| u.redacted_because).is_some(); - let event_kind = match state_key { - Some(_) => EventKind::state_redacted(redacted), - None => EventKind::message_like_redacted(redacted), + let (event_kind_g, event_kind_r) = match state_key { + Some(_) => (EventKind::State, EventKind::state_redacted(redacted)), + None => (EventKind::MessageLike, EventKind::message_like_redacted(redacted)), }; let raw_event = &item.event.json(); let encryption_info = item.encryption_info.as_ref(); - self.call_event_handlers(room, raw_event, event_kind, &event_type, encryption_info) + // Event handlers for possibly-redacted timeline events + self.call_event_handlers(room, raw_event, event_kind_g, &event_type, encryption_info) + .await; + + // Event handlers specifically for redacted OR unredacted timeline events + self.call_event_handlers(room, raw_event, event_kind_r, &event_type, encryption_info) .await; }