In observeDeep, can I learn about what was deleted?


I’m trying to use observeDeep() to synchronize our view model with our yjs document.

In our document, we have a Y.Array of Y.Map. When a Y.Map is deleted from the Y.Array, I want to delete the corresponding objects in our view model.

I know I can use the event.changes.deleted to get a list of items deleted from the Y.Array, and that I can do a item.content.getContent() to get to the Y.Map instance that was removed.

My problem is, I want to be able to do a Y.Map.get(“_id”) field of the Y.Map so I can correlate it against our view model.

Unfortunately, since the Y.Map is no longer a member of the Y.Doc, I can’t read any of its fields.

Is there any work around to be able to identify which Y.Map was deleted?

Thank you!


It looks like I can do something like the below, but seems a bit fragile?

If order doesn’t matter, you could model it as a Y.Map of Y.Map. Then you would have access to the deleted id through event.changes.keys.

It’s unfortunate that you have to do a workaround rather than the yarray observe just passing the deleted values.

Thank you for replying!

In this case, order does matter, but your suggestion is great and one, and one that I’m using already in the case of Y.Maps of Y.Maps.

I faced the same exact issue re. Y.Array of Y.Map and need to get YMap on delete. As @dmonad has pointed out no data is kept that enables this. An approach like yours does work, however as it is undocumented and unsupported it isn’t a route I want to take.

In then end I’m used pEvent inside the observerDeep() Y.Map event handler to wait for the Y.Array delete event and when pEvent() resolves I still have access to the Y.Map item. Problem solved.

pEvent() has been a life saver for me in async situations like this. HTH.

FYI This is in my new app Clibu Notes.

1 Like