Skip to content
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

fix(events): Simplify filter function, add new enqueueEvent function #8539

Merged
merged 6 commits into from
Aug 29, 2024

Conversation

cpcallen
Copy link
Contributor

@cpcallen cpcallen commented Aug 21, 2024

The basics

The details

Resolves

Fixes #2037, #8225

Proposed Changes

  • Remove the broken BlockChange event reordering code from filter. Instead, do necessary event reordering in a new (and hopefully more robust and less buggy) enqueueEvent function called from fireInternal as events are fired.
  • Since (as of refactor(events): Don't filter events before undo #8537) we don't filter in undo any more, and filtering in reverse order is confusing and can cause problems, prepare to remove this opportunity for error by deprecating calling filter with forward=false.
  • Simplify filter by having it consider only adjacent events in the queue for merging. Previously any events in the queue could potentially be merged if they were of suitable (typically identical) .type, even if other events had occurred between them (with the sole exception of BlockMove events, which would only be merged with adjacent ones). This could potentially result in replay failures during undo/redo/mirroring, though it is unknown whether any such problems occurred in practice.
  • Use for…of loop where appropriate.
  • Make filter reason-merging code more concise.
  • Use arrow functions when calling Array.prototype.filter.

Reason for Changes

  • Remove code from filter that has been the source of multiple current and previous bugs, and actively breaks other attempts to fix those bugs (e.g. by emitting events in the correct order to start with).
  • Simplify filter so it is easier to reason about its behaviour.

Test Coverage

  • Added unit tests for enqueueEvent.
  • There are existing unit tests for filter, none of which fail due to the changes made in this PR.

Additional Info

Strictly speaking, making the forward parameter of filter default to true is a breaking change, as JS users would previously have observed it defaulting to falsy undefined. My reasoning to overlook this is that I suspect very few developers use this function directly (to the point that I think we should consider deprecating its export), and the likelihood of any of them calling it with no second parameter, intending that to request filtering in reverse order, seems very remote.

Nevertheless the change adding a default could be removed if desired.

Remove the broken BlockChange event reordering code from filter.
Instead, do necessary event reordering (correctly, this time)
in a new enqueueEvent function used by fireInternal.
Since we don't filter in undo any more, and filtering in reverse
order causes problems, prepare to remove this opportunity for
error.
Simplify filter by having it consider only adjacent events in the
queue for merging.

Previously any events in the queue could potentially be merged if
they were of suitable (typically identical) .type, even if other
events had occurred between them (with the sole exception of
BlockMove events, which would only be merged with adjacent
events).  This could potentially result in replay failures during
undo/redo/mirroring, though it is unknown whether any such
problems occurred in practice.
- Use for…of loop where appropriate.
- Make filter reason-merging code more concise.
- Use arrow functions when calling Array.prototype.filter.
@cpcallen cpcallen requested a review from a team as a code owner August 21, 2024 17:11
@cpcallen cpcallen requested a review from gonfunko August 21, 2024 17:11
@github-actions github-actions bot removed the PR: fix Fixes a bug label Aug 21, 2024
@github-actions github-actions bot added the PR: fix Fixes a bug label Aug 21, 2024
core/events/utils.ts Outdated Show resolved Hide resolved
core/events/utils.ts Outdated Show resolved Hide resolved
tests/mocha/event_test.js Outdated Show resolved Hide resolved
core/events/utils.ts Outdated Show resolved Hide resolved
@cpcallen cpcallen merged commit a7afda8 into google:develop Aug 29, 2024
7 checks passed
@cpcallen cpcallen deleted the fix/filter branch August 29, 2024 16:41
@cpcallen cpcallen linked an issue Aug 30, 2024 that may be closed by this pull request
1 task
@github-actions github-actions bot added PR: fix Fixes a bug and removed PR: fix Fixes a bug labels Sep 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Issue with mirroring workspace controls_if: Mutation actions do not undo correctly
2 participants