Alternative implementation of cursors #313
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR provides an implementation of cursors, which allow a particular element of a list, or a particular character in an
Automerge.Text
object, to be referenced from elsewhere in the same document. The API involves a newAutomerge.Cursor
class, and it can be used like this for lists:and like this for Text:
The key thing about a cursor is that its index automatically updates when the list/text it references is changed. In the examples above, the index updates to 3 if you insert a new element before the cursor position, and it updates to 1 if you delete an element before the cursor position. If you delete the element that the cursor points to, the index is set to the index of the closest preceding element that has not been deleted (or -1 if there is no such element that has not been deleted).
The implementation of cursors on #307 requires a direct call from the frontend into the backend, which is inconvenient when frontend and backend are on different threads. This PR is an alternative implementation that is integrated with the existing frontend-backend protocol, making it compatible with multiple threads. This PR also includes a way for cursors to be efficiently represented in the binary storage format for changes and documents. A downside of this approach compared to #307 is that this PR requires cursors to be stored within the same Automerge document; it is not possible to store them somewhere out-of-band, or in a separate document.