How to determine what changed in an update

I’m trying to persistently full-text index a Y.Array. The idea would be, every time an update comes, understand what changed and index, re-index or de-index the new / changed / deleted elements of the array.

Is there a way to (directly or indirectly) understand what changed in one update? I see from other discussions that the recommended solution for similar problems is converting to JS array and debounce the re-index of the whole array for every update, but indexing is an expensive operation and doing so would be really inefficient as the array grows.

Hi @janesconference,

You could use the type observers:

ytext.observe(event => {
  event.delta // is a quill delta - https://quilljs.com/docs/delta/
})

If you only want to observe the changes that happen when applying specific updates, you could simply register an observer, and then unregister after the update has been applied.

Hi @dmonad, if I try that on my array,

yMessagearray.observe((event) => {
  console.log(event.delta);
});

event.delta is always undefined.

(and by the way, looking at the event object, I can’t seem to find info on what was changed anywhere)

Try event.changes - the YArrayEvent works a bit differently. It also has a delta-like property: event.changes.delta

@dmonad that works! Many thanks!