12
12
// See the License for the specific language governing permissions and
13
13
// limitations under the License.
14
14
15
- use std:: { ops :: ControlFlow , sync:: Arc } ;
15
+ use std:: sync:: Arc ;
16
16
17
17
use as_variant:: as_variant;
18
18
use indexmap:: IndexMap ;
@@ -1171,23 +1171,15 @@ impl<'a, 'o> TimelineEventHandler<'a, 'o> {
1171
1171
// so we are inserting at the last non-local item position as a fallback.
1172
1172
let timeline_item_index = timeline_item_index. unwrap_or_else ( || {
1173
1173
self . items
1174
- . iter ( )
1175
- . enumerate ( )
1174
+ . iter_remotes_region ( )
1176
1175
. rev ( )
1177
1176
. find_map ( |( timeline_item_index, timeline_item) | {
1178
- ( !timeline_item. as_event ( ) ?. is_local_echo ( ) )
1179
- . then_some ( timeline_item_index + 1 )
1177
+ timeline_item. as_event ( ) . and_then ( |_| Some ( timeline_item_index + 1 ) )
1180
1178
} )
1181
1179
. unwrap_or_else ( || {
1182
- // We don't have any local echo, so we could insert at 0. However, in
1183
- // the case of an insertion caused by a pagination, we
1184
- // may have already pushed the start of the timeline item, so we need
1185
- // to check if the first item is that, and insert after it otherwise.
1186
- if self . items . get ( 0 ) . is_some_and ( |item| item. is_timeline_start ( ) ) {
1187
- 1
1188
- } else {
1189
- 0
1190
- }
1180
+ // There is no remote timeline item, so we could insert at the start of
1181
+ // the remotes region.
1182
+ self . items . first_remotes_region_index ( )
1191
1183
} )
1192
1184
} ) ;
1193
1185
@@ -1211,28 +1203,18 @@ impl<'a, 'o> TimelineEventHandler<'a, 'o> {
1211
1203
txn_id. as_deref ( ) ,
1212
1204
) ;
1213
1205
1214
- // Local events are always at the bottom. Let's find the latest remote event
1215
- // and insert after it, otherwise, if there is no remote event, insert at 0 or 1
1216
- // depending of the presence of the `TimelineStart` virtual item.
1206
+ // Let's find the latest remote event and insert after it
1217
1207
let timeline_item_index = self
1218
1208
. items
1219
- . iter ( )
1220
- . enumerate ( )
1209
+ . iter_remotes_region ( )
1221
1210
. rev ( )
1222
1211
. find_map ( |( timeline_item_index, timeline_item) | {
1223
- ( !timeline_item. as_event ( ) ?. is_local_echo ( ) )
1224
- . then_some ( timeline_item_index + 1 )
1212
+ timeline_item. as_event ( ) . and_then ( |_| Some ( timeline_item_index + 1 ) )
1225
1213
} )
1226
1214
. unwrap_or_else ( || {
1227
- // We don't have any local echo, so we could insert at 0. However, in
1228
- // the case of an insertion caused by a pagination, we
1229
- // may have already pushed the start of the timeline item, so we need
1230
- // to check if the first item is that, and insert after it otherwise.
1231
- if self . items . get ( 0 ) . is_some_and ( |item| item. is_timeline_start ( ) ) {
1232
- 1
1233
- } else {
1234
- 0
1235
- }
1215
+ // There is no remote timeline item, so we could insert at the start of
1216
+ // the remotes region.
1217
+ self . items . first_remotes_region_index ( )
1236
1218
} ) ;
1237
1219
1238
1220
let event_index = self
@@ -1306,46 +1288,25 @@ impl<'a, 'o> TimelineEventHandler<'a, 'o> {
1306
1288
) -> Arc < TimelineItem > {
1307
1289
// Detect a local timeline item that matches `event_id` or `transaction_id`.
1308
1290
if let Some ( ( local_timeline_item_index, local_timeline_item) ) = items
1309
- . iter ( )
1310
- // Get the index of each item.
1311
- . enumerate ( )
1291
+ // Iterate the locals region.
1292
+ . iter_locals_region ( )
1312
1293
// Iterate from the end to the start.
1313
1294
. rev ( )
1314
- // Use a `Iterator::try_fold` to produce a single value, and to stop the iterator
1315
- // when a non local event timeline item is met. We want to stop iterating when:
1316
- //
1317
- // - a duplicate local event timeline item has been found,
1318
- // - a non local event timeline item is met,
1319
- // - a non event timeline is met.
1320
- //
1321
- // Indeed, it is a waste of time to iterate over all items in `items`. Local event
1322
- // timeline items are necessarily at the end of `items`: as soon as they have been
1323
- // iterated, we can stop the entire iteration.
1324
- . try_fold ( ( ) , |( ) , ( nth, timeline_item) | {
1325
- let Some ( event_timeline_item) = timeline_item. as_event ( ) else {
1326
- // Not an event timeline item? Stop iterating here.
1327
- return ControlFlow :: Break ( None ) ;
1328
- } ;
1329
-
1330
- // Not a local event timeline item? Stop iterating here.
1331
- if !event_timeline_item. is_local_echo ( ) {
1332
- return ControlFlow :: Break ( None ) ;
1333
- }
1295
+ . find_map ( |( nth, timeline_item) | {
1296
+ let event_timeline_item = timeline_item. as_event ( ) ?;
1334
1297
1335
1298
if Some ( event_id) == event_timeline_item. event_id ( )
1336
1299
|| ( transaction_id. is_some ( )
1337
1300
&& transaction_id == event_timeline_item. transaction_id ( ) )
1338
1301
{
1339
1302
// A duplicate local event timeline item has been found!
1340
- ControlFlow :: Break ( Some ( ( nth, event_timeline_item) ) )
1303
+ Some ( ( nth, event_timeline_item) )
1341
1304
} else {
1342
1305
// This local event timeline is not the one we are looking for. Continue our
1343
1306
// search.
1344
- ControlFlow :: Continue ( ( ) )
1307
+ None
1345
1308
}
1346
1309
} )
1347
- . break_value ( )
1348
- . flatten ( )
1349
1310
{
1350
1311
trace ! (
1351
1312
?event_id,
0 commit comments