@@ -1050,7 +1050,7 @@ impl<'a, 'o> TimelineEventHandler<'a, 'o> {
1050
1050
1051
1051
let is_room_encrypted = self . meta . is_room_encrypted ;
1052
1052
1053
- let mut item = EventTimelineItem :: new (
1053
+ let item = EventTimelineItem :: new (
1054
1054
sender,
1055
1055
sender_profile,
1056
1056
timestamp,
@@ -1071,13 +1071,13 @@ impl<'a, 'o> TimelineEventHandler<'a, 'o> {
1071
1071
Flow :: Remote {
1072
1072
position : TimelineItemPosition :: Start { .. } , event_id, txn_id, ..
1073
1073
} => {
1074
- let removed_duplicated_timeline_item = Self :: deduplicate_local_timeline_item (
1074
+ let item = Self :: recycle_local_or_create_item (
1075
1075
self . items ,
1076
- & mut item,
1077
- Some ( event_id) ,
1078
- txn_id. as_ref ( ) . map ( AsRef :: as_ref) ,
1076
+ self . meta ,
1077
+ item,
1078
+ event_id,
1079
+ txn_id. as_deref ( ) ,
1079
1080
) ;
1080
- let item = new_timeline_item ( self . meta , item, removed_duplicated_timeline_item) ;
1081
1081
1082
1082
trace ! ( "Adding new remote timeline item at the start" ) ;
1083
1083
@@ -1090,13 +1090,13 @@ impl<'a, 'o> TimelineEventHandler<'a, 'o> {
1090
1090
txn_id,
1091
1091
..
1092
1092
} => {
1093
- let removed_duplicated_timeline_item = Self :: deduplicate_local_timeline_item (
1093
+ let item = Self :: recycle_local_or_create_item (
1094
1094
self . items ,
1095
- & mut item,
1096
- Some ( event_id) ,
1097
- txn_id. as_ref ( ) . map ( AsRef :: as_ref) ,
1095
+ self . meta ,
1096
+ item,
1097
+ event_id,
1098
+ txn_id. as_deref ( ) ,
1098
1099
) ;
1099
- let item = new_timeline_item ( self . meta , item, removed_duplicated_timeline_item) ;
1100
1100
1101
1101
let all_remote_events = self . items . all_remote_events ( ) ;
1102
1102
let event_index = * event_index;
@@ -1153,13 +1153,13 @@ impl<'a, 'o> TimelineEventHandler<'a, 'o> {
1153
1153
Flow :: Remote {
1154
1154
position : TimelineItemPosition :: End { .. } , event_id, txn_id, ..
1155
1155
} => {
1156
- let removed_duplicated_timeline_item = Self :: deduplicate_local_timeline_item (
1156
+ let item = Self :: recycle_local_or_create_item (
1157
1157
self . items ,
1158
- & mut item,
1159
- Some ( event_id) ,
1160
- txn_id. as_ref ( ) . map ( AsRef :: as_ref) ,
1158
+ self . meta ,
1159
+ item,
1160
+ event_id,
1161
+ txn_id. as_deref ( ) ,
1161
1162
) ;
1162
- let item = new_timeline_item ( self . meta , item, removed_duplicated_timeline_item) ;
1163
1163
1164
1164
// Local events are always at the bottom. Let's find the latest remote event
1165
1165
// and insert after it, otherwise, if there is no remote event, insert at 0.
@@ -1231,17 +1231,18 @@ impl<'a, 'o> TimelineEventHandler<'a, 'o> {
1231
1231
}
1232
1232
}
1233
1233
1234
- /// Remove the local timeline item matching the `event_id` or the
1235
- /// `transaction_id` of `new_event_timeline_item` if it exists .
1236
- // Note: this method doesn't take `&mut self` to avoid a borrow checker
1237
- // conflict with `TimelineEventHandler::add_item`.
1238
- // TODO(bnjbvr): refactor
1239
- fn deduplicate_local_timeline_item (
1234
+ /// Try to recycle a local timeline item for the same event, or create a new
1235
+ /// timeline item for it .
1236
+ ///
1237
+ /// Note: this method doesn't take `&mut self` to avoid a borrow checker
1238
+ /// conflict with `TimelineEventHandler::add_item`.
1239
+ fn recycle_local_or_create_item (
1240
1240
items : & mut ObservableItemsTransaction < ' _ > ,
1241
- new_event_timeline_item : & mut EventTimelineItem ,
1242
- event_id : Option < & EventId > ,
1241
+ meta : & mut TimelineMetadata ,
1242
+ mut new_item : EventTimelineItem ,
1243
+ event_id : & EventId ,
1243
1244
transaction_id : Option < & TransactionId > ,
1244
- ) -> Option < Arc < TimelineItem > > {
1245
+ ) -> Arc < TimelineItem > {
1245
1246
// Detect a local timeline item that matches `event_id` or `transaction_id`.
1246
1247
if let Some ( ( local_timeline_item_index, local_timeline_item) ) = items
1247
1248
. iter ( )
@@ -1270,7 +1271,7 @@ impl<'a, 'o> TimelineEventHandler<'a, 'o> {
1270
1271
return ControlFlow :: Break ( None ) ;
1271
1272
}
1272
1273
1273
- if event_id == event_timeline_item. event_id ( )
1274
+ if Some ( event_id) == event_timeline_item. event_id ( )
1274
1275
|| ( transaction_id. is_some ( )
1275
1276
&& transaction_id == event_timeline_item. transaction_id ( ) )
1276
1277
{
@@ -1292,13 +1293,15 @@ impl<'a, 'o> TimelineEventHandler<'a, 'o> {
1292
1293
"Removing local timeline item"
1293
1294
) ;
1294
1295
1295
- transfer_details ( new_event_timeline_item , local_timeline_item) ;
1296
+ transfer_details ( & mut new_item , local_timeline_item) ;
1296
1297
1297
1298
// Remove the local timeline item.
1298
- return Some ( items. remove ( local_timeline_item_index) ) ;
1299
- } ;
1300
-
1301
- None
1299
+ let recycled = items. remove ( local_timeline_item_index) ;
1300
+ TimelineItem :: new ( new_item, recycled. internal_id . clone ( ) )
1301
+ } else {
1302
+ // We haven't found a matching local item to recycle; create a new item.
1303
+ meta. new_timeline_item ( new_item)
1304
+ }
1302
1305
}
1303
1306
1304
1307
/// After updating the timeline item `new_item` which id is
@@ -1366,23 +1369,3 @@ fn transfer_details(new_item: &mut EventTimelineItem, old_item: &EventTimelineIt
1366
1369
in_reply_to. event = old_in_reply_to. event . clone ( ) ;
1367
1370
}
1368
1371
}
1369
-
1370
- /// Create a new timeline item from an [`EventTimelineItem`].
1371
- ///
1372
- /// It is possible that the new timeline item replaces a duplicated timeline
1373
- /// event (see [`TimelineEventHandler::deduplicate_local_timeline_item`]) in
1374
- /// case it replaces a local timeline item.
1375
- fn new_timeline_item (
1376
- metadata : & mut TimelineMetadata ,
1377
- event_timeline_item : EventTimelineItem ,
1378
- replaced_timeline_item : Option < Arc < TimelineItem > > ,
1379
- ) -> Arc < TimelineItem > {
1380
- match replaced_timeline_item {
1381
- // Reuse the internal ID.
1382
- Some ( to_replace_timeline_item) => {
1383
- TimelineItem :: new ( event_timeline_item, to_replace_timeline_item. internal_id . clone ( ) )
1384
- }
1385
-
1386
- None => metadata. new_timeline_item ( event_timeline_item) ,
1387
- }
1388
- }
0 commit comments