Skip to content

Commit b02fd92

Browse files
committed
refactor(ui): Introduce the AllRemoteEvents type.
This patch replaces `VecDeque<EventMeta>` by `AllRemoteEvents` which is a wrapper type around `VecDeque<EventMeta>`, but this new type aims at adding semantics API rather than a generic API. It also helps to isolate the use of these values and to know precisely when and how they are used. As a first step, `AllRemoteEvents` implements a generic API to not break the existing code. Next patches will revisit that a little bit step by step.
1 parent 9be8578 commit b02fd92

File tree

3 files changed

+57
-12
lines changed

3 files changed

+57
-12
lines changed

crates/matrix-sdk-ui/src/timeline/controller/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ use tracing::{
5252
};
5353

5454
pub(super) use self::state::{
55-
EventMeta, FullEventMeta, PendingEdit, PendingEditKind, TimelineMetadata,
55+
AllRemoteEvents, FullEventMeta, PendingEdit, PendingEditKind, TimelineMetadata,
5656
TimelineNewItemPosition, TimelineState, TimelineStateTransaction,
5757
};
5858
use super::{

crates/matrix-sdk-ui/src/timeline/controller/state.rs

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@
1313
// limitations under the License.
1414

1515
use std::{
16-
collections::{HashMap, VecDeque},
16+
collections::{
17+
vec_deque::{Iter, IterMut},
18+
HashMap, VecDeque,
19+
},
1720
future::Future,
1821
num::NonZeroUsize,
1922
sync::{Arc, RwLock},
@@ -715,7 +718,7 @@ impl TimelineStateTransaction<'_> {
715718
// Returns its position, in this case.
716719
fn event_already_exists(
717720
new_event_id: &EventId,
718-
all_remote_events: &VecDeque<EventMeta>,
721+
all_remote_events: &AllRemoteEvents,
719722
) -> Option<usize> {
720723
all_remote_events.iter().position(|EventMeta { event_id, .. }| event_id == new_event_id)
721724
}
@@ -924,7 +927,7 @@ pub(in crate::timeline) struct TimelineMetadata {
924927
///
925928
/// This is useful to get this for the moment as it helps the `Timeline` to
926929
/// compute read receipts and read markers.
927-
pub all_remote_events: VecDeque<EventMeta>,
930+
pub all_remote_events: AllRemoteEvents,
928931

929932
/// State helping matching reactions to their associated events, and
930933
/// stashing pending reactions.
@@ -1139,6 +1142,52 @@ impl TimelineMetadata {
11391142
}
11401143
}
11411144

1145+
/// A type for all remote events.
1146+
///
1147+
/// Having this type helps to know exactly which parts of the code and how they
1148+
/// use all remote events. It also helps to give a bit of semantics on top of
1149+
/// them.
1150+
#[derive(Clone, Debug, Default)]
1151+
pub(crate) struct AllRemoteEvents(VecDeque<EventMeta>);
1152+
1153+
impl AllRemoteEvents {
1154+
/// Return a front-to-back iterator over all remote events.
1155+
pub fn iter(&self) -> Iter<'_, EventMeta> {
1156+
self.0.iter()
1157+
}
1158+
1159+
/// Return a front-to-back iterator over all remote events as mutable
1160+
/// references.
1161+
pub fn iter_mut(&mut self) -> IterMut<'_, EventMeta> {
1162+
self.0.iter_mut()
1163+
}
1164+
1165+
/// Remove all remote events.
1166+
pub fn clear(&mut self) {
1167+
self.0.clear();
1168+
}
1169+
1170+
/// Insert a new remote event at the front of all the others.
1171+
pub fn push_front(&mut self, event_meta: EventMeta) {
1172+
self.0.push_front(event_meta)
1173+
}
1174+
1175+
/// Insert a new remote event at the back of all the others.
1176+
pub fn push_back(&mut self, event_meta: EventMeta) {
1177+
self.0.push_back(event_meta)
1178+
}
1179+
1180+
/// Remove one remote event at a specific index, and return it if it exists.
1181+
pub fn remove(&mut self, event_index: usize) -> Option<EventMeta> {
1182+
self.0.remove(event_index)
1183+
}
1184+
1185+
/// Return a reference to the last remote event if it exists.
1186+
pub fn back(&self) -> Option<&EventMeta> {
1187+
self.0.back()
1188+
}
1189+
}
1190+
11421191
/// Full metadata about an event.
11431192
///
11441193
/// Only used to group function parameters.

crates/matrix-sdk-ui/src/timeline/read_receipts.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
use std::{
16-
cmp::Ordering,
17-
collections::{HashMap, VecDeque},
18-
sync::Arc,
19-
};
15+
use std::{cmp::Ordering, collections::HashMap, sync::Arc};
2016

2117
use eyeball_im::ObservableVectorTransaction;
2218
use futures_core::Stream;
@@ -31,7 +27,7 @@ use tracing::{debug, error, warn};
3127

3228
use super::{
3329
controller::{
34-
EventMeta, FullEventMeta, TimelineMetadata, TimelineState, TimelineStateTransaction,
30+
AllRemoteEvents, FullEventMeta, TimelineMetadata, TimelineState, TimelineStateTransaction,
3531
},
3632
traits::RoomDataProvider,
3733
util::{rfind_event_by_id, RelativePosition},
@@ -103,7 +99,7 @@ impl ReadReceipts {
10399
&mut self,
104100
new_receipt: FullReceipt<'_>,
105101
is_own_user_id: bool,
106-
all_events: &VecDeque<EventMeta>,
102+
all_events: &AllRemoteEvents,
107103
timeline_items: &mut ObservableVectorTransaction<'_, Arc<TimelineItem>>,
108104
) {
109105
// Get old receipt.
@@ -243,7 +239,7 @@ impl ReadReceipts {
243239
pub(super) fn compute_event_receipts(
244240
&self,
245241
event_id: &EventId,
246-
all_events: &VecDeque<EventMeta>,
242+
all_events: &AllRemoteEvents,
247243
at_end: bool,
248244
) -> IndexMap<OwnedUserId, Receipt> {
249245
let mut all_receipts = self.get_event_receipts(event_id).cloned().unwrap_or_default();

0 commit comments

Comments
 (0)