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

Remove stale cursor pointing to array elements once they are removed #2

Closed
abhishiv opened this issue Jun 24, 2024 · 0 comments
Closed
Assignees

Comments

@abhishiv
Copy link
Owner

ChatGPT transcript

ok, in step 5, inside the Observable.observe callback, if the obj is an array wouldn't that fail if the obj is modified to insert something in-between two elements? Because in that case the cursorStr inside of wire.storesRS would be still reference the old indexes?

ChatGPT

You're correct that if the observed object is an array and elements are inserted or deleted in the middle, the existing cursor references based on indices would no longer be accurate. This can lead to issues with tracking and responding to changes correctly, as the cursors and their associated paths might not align with the new structure of the array.

To address this issue, we can enhance the change tracking and cursor management logic to handle array modifications more robustly. Specifically, we should account for array operations such as insertions, deletions, and splices, ensuring that cursor paths remain valid and accurately reflect the current state of the array.

Enhanced Handling of Array Modifications
Track Array Changes More Precisely:

When changes occur, identify if they involve array operations such as insertion, deletion, or splicing.
Update the cursor paths accordingly to reflect the new indices of elements in the array.
Update Cursors and Paths:

When elements are inserted or deleted, adjust the cursor paths to maintain correct references to the affected elements.
Ensure that any wires dependent on these paths are correctly identified and updated.
Here's an enhanced version of the Observable.observe callback that handles array modifications more robustly:

Code Example

        switch (change.type) {
          case 'insert':
          case 'delete':
          case 'splice':
            manager.wires.forEach(wire => {
              const cursors = wire.storesRS.get(manager);
              if (cursors) {
                const updatedCursors = new Set<string>();
                cursors.forEach(cursorStr => {
                  const cursor = decodeCursor(cursorStr);
                  // Adjust the cursor path based on the array operation
                  // Example: If an element is inserted at index 2, increment indices >= 2
                  const updatedCursor = adjustCursorForArrayChange(cursor, change);
                  updatedCursors.add(encodeCursor(updatedCursor));
                });
                wire.storesRS.set(manager, updatedCursors);
              }
            });
            break;
        }

// Function to adjust cursor paths for array changes
function adjustCursorForArrayChange(cursor: string[], change: Change): string[] {
  const newCursor = [...cursor];
  const index = parseInt(change.path[change.path.length - 1], 10);

  switch (change.type) {
    case 'insert':
      if (index <= cursor.length) {
        newCursor.push(String(index));
      }
      break;
    case 'delete':
      if (index < cursor.length) {
        newCursor.splice(index, 1);
      }
      break;
    case 'splice':
      const { index: spliceIndex, removed, added } = change;
      if (spliceIndex <= cursor.length) {
        newCursor.splice(spliceIndex, removed.length, ...added.map((_, i) => String(spliceIndex + i)));
      }
      break;
  }

  return newCursor;
}
@abhishiv abhishiv self-assigned this Jun 24, 2024
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

No branches or pull requests

1 participant