Skip to content

chore(ui): Unify the logic for timeline item insertions #4331

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

Merged

Conversation

Hywan
Copy link
Member

@Hywan Hywan commented Nov 26, 2024

(Extracted from #3512)

This patch tries to unify the code to insert timeline item at position Start and End. We can clearly have the same code for these TimelineItemPositions. It doesn't make sense to have separate implementations, especially knowing that End had duplicated events check while Start had not.

This code only and strictly code moving, modulo the position handling, but nothing has changed apart from that.

Note: this is going to be useful when (i) event deduplication will be removed, and (ii) when TimelineItemPosition::At will be added!


@Hywan Hywan marked this pull request as ready for review November 26, 2024 16:32
@Hywan Hywan requested a review from a team as a code owner November 26, 2024 16:32
@Hywan Hywan requested review from stefanceriu and poljar and removed request for a team and stefanceriu November 26, 2024 16:32
@bnjbvr bnjbvr requested review from bnjbvr and removed request for poljar November 26, 2024 17:02
@Hywan Hywan force-pushed the chore-ui-timeline-refactor-event-handler branch 3 times, most recently from 780fae9 to 6814b6d Compare November 27, 2024 08:01
Copy link

codecov bot commented Nov 27, 2024

Codecov Report

Attention: Patch coverage is 70.21277% with 14 lines in your changes missing coverage. Please review.

Project coverage is 85.06%. Comparing base (75d7d07) to head (979b6b0).
Report is 27 commits behind head on main.

Files with missing lines Patch % Lines
crates/matrix-sdk-ui/src/timeline/event_handler.rs 70.21% 14 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main    #4331   +/-   ##
=======================================
  Coverage   85.05%   85.06%           
=======================================
  Files         275      275           
  Lines       30309    30316    +7     
=======================================
+ Hits        25780    25788    +8     
+ Misses       4529     4528    -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

bnjbvr
bnjbvr previously requested changes Nov 27, 2024

// Insert the next item after the latest event item that's not a
// pending local echo, or at the start if there is no such item.
let insert_idx = latest_event_idx.map_or(0, |idx| idx + 1);
let (insert_index, must_push_front) = match position {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(spoiler alert: we don't need must_push_front, since inserting at insert_idx = 0 means pushing front…)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(spoiler alert: turns out, we need it :-D)

Imagine we have insert_index = 0. Should we push front or back? Front!, according to you. But it's gonna break many tests that assume that the first added event, even with position = End, must be pushed back. So by only looking at insert_index, in the case it's zero, we don't know whether we have to push it front or back. Hence the must_push_front that adds a clue.

Copy link
Member Author

@Hywan Hywan Nov 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatively, we can insert inside the match position. A bit more repetitive code, but it's much much clearer. Let me try that in the last commit.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still think what I suggested would work, if you both kept only the insert_idx and reverted the order of the final push statements as it were back in the original form:

  • in the case of inserting at the front, insert_idx would be 0, and when the list is empty that would result in a push_back; when it's not, it would be a push front
  • in the case of inserting at the back, we compute the proper insert_idx anyways.

And I mean, it's OK to change the test expectations when they lead to the same outcome ^^ push_back or push_front on an empty array yields to the same outcome. But OK, the new code works too.

@Hywan Hywan requested a review from bnjbvr November 27, 2024 15:15
Copy link
Contributor

@poljar poljar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The commit message looks a bit rough grammar-wise as well, perhaps something like:

chore(ui): Unify the logic for timeline item insertions

This patch unifies the logic for inserting timeline items at Start and End positions. Both TimelineItemPositions can share the same implementation, making separate logic unnecessary. Previously, End included a duplicated events check as well, while Start did not, leading to inconsistency.

The changes strictly involve moving and refactoring, with no functional modifications.

Comment on lines +1173 to +1182
p => unreachable!(
"An unexpected `TimelineItemPosition` has been received: {p:?}"
),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Err not that happy that this patch is adding an unreachable call, which we previously avoided. But ok.

Copy link
Member Author

@Hywan Hywan Nov 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah… it's unfortunate this is needed, but the upper match-arm makes this really unreachable. A larger refactoring would be needed to tackle that I think.

This patch unifies the logic for inserting timeline items at `Start`
and `End` positions. Both `TimelineItemPositions` can share the same
implementation, making separate logic unnecessary. Previously, `End`
included a duplicated events check as well, while `Start` did not, leading
to inconsistency.

The changes strictly involve moving and refactoring, with no functional
modifications.
@Hywan Hywan force-pushed the chore-ui-timeline-refactor-event-handler branch from 607d488 to 979b6b0 Compare November 28, 2024 06:57
@Hywan Hywan changed the title chore(ui): Have a single code for all kind of timeline item insertions chore(ui): Unify the logic for timeline item insertions Nov 28, 2024
@Hywan Hywan dismissed bnjbvr’s stale review November 28, 2024 06:58

Poljar did approve it, and all feedback from Ben have been addressed

@Hywan Hywan merged commit d2ecd74 into matrix-org:main Nov 28, 2024
40 checks passed
@Hywan Hywan mentioned this pull request Nov 28, 2024
37 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants