Skip to content

Conversation

@ricokahler
Copy link
Contributor

@ricokahler ricokahler commented Jun 3, 2025

This PR refactors the diffArray function to simplify the logic for unsetting items from an array. Previously, the decision of whether to unset array items by index/range versus by _key was partially influenced by an isRevisionLocked check.

The isRevisionLocked check (which determined if ifRevisionID was set in options) has been removed from this specific decision point. The logic now prioritizes unsetting by _key if the items to be unset are uniquely keyed, regardless of the revision lock status. If items are not uniquely keyed, it falls back to unsetting by index/range.

Key Changes:

  • In diffArray, when itemB.length < itemA.length (items are removed):
    • The condition isRevisionLocked(options) || !isUniquelyKeyed(unsetItems) has been simplified to !isUniquelyKeyed(unsetItems).
    • This means the decision to unset by index/range now only depends on whether the unsetItems themselves have unique keys. If they do, unsetting by _key is preferred for better safety in collaborative environments.
  • Removed the isRevisionLocked(options: PatchOptions): boolean utility function as it's no longer used in this context.

Rationale:

  • Simplification: The logic for choosing the unsetting strategy is now more straightforward.
  • Consistency: Unsetting by _key is generally safer and more robust in collaborative editing. This change makes key-based unsetting the default when applicable, promoting better conflict resolution for keyed arrays.
  • Reduced Complexity: The ifRevisionID option's primary purpose is optimistic locking at the patch mutation level, not to dictate the internal strategy for array item unsetting. Decoupling these concerns simplifies the diffArray logic.

This refactor makes the array unsetting behavior more predictable and aligns better with the goal of generating conflict-resistant patches, especially for keyed arrays.

Copy link
Contributor Author

ricokahler commented Jun 3, 2025

@ricokahler ricokahler force-pushed the v6_remove-array-revision-lock-check branch from 3aff5f8 to 2459585 Compare June 6, 2025 21:57
@ricokahler ricokahler force-pushed the v6_new-dmp-behavior branch from 63bee6e to db4ab0e Compare June 6, 2025 21:57
Copy link
Contributor Author

ricokahler commented Jun 13, 2025

Merge activity

  • Jun 13, 7:24 PM UTC: A user started a stack merge that includes this pull request via Graphite.
  • Jun 13, 7:25 PM UTC: Graphite rebased this pull request as part of a merge.
  • Jun 13, 7:27 PM UTC: @ricokahler merged this pull request with Graphite.

@ricokahler ricokahler changed the base branch from v6_new-dmp-behavior to graphite-base/37 June 13, 2025 19:24
@ricokahler ricokahler changed the base branch from graphite-base/37 to main June 13, 2025 19:24
Remove the isRevisionLocked() condition from array diffing logic when
deciding between key-based vs index-based unset operations.

The revision lock check was redundant because:
- Key-based unsets are inherently safer in collaborative environments
- Revision locking doesn't change the safety characteristics of the operation
- If array items have unique keys, key-based operations should always be
  preferred regardless of whether we're targeting a specific revision
- Index-based operations are still used as fallback when items lack unique keys

This simplifies the logic to: if items have unique keys, always use key-based
unsets; otherwise fall back to index-based unsets.
@ricokahler ricokahler force-pushed the v6_remove-array-revision-lock-check branch from 2459585 to 4315ed5 Compare June 13, 2025 19:25
@ricokahler ricokahler merged commit 07763f4 into main Jun 13, 2025
7 checks passed
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.

2 participants