How to distinguis updates for different items?

Hi all,

Assume that you have called doc.getText() twice. If you update one of them, how you can find in the .on(‘update’, …) call which one was changed? Can you get the name of the changed item?

Best,

Hi @rezass

If you peform the update inside a transaction, then you can pass different values for origin.

Check out the doc.transact api described here Y.Doc - Yjs Docs

So for example, you can update from two different places.

doc.transact(() => doc.getText().insert(0, "abc"),  "origin-x")
doc.transact(() => doc.getText().insert(0, "abc"), "origin-y")

In the update event handler, you can check the value of the origin to find out where this update happened.

doc.on("update", (update, origin, doc, transaction) => console.log(origin))

I believe even transaction also has transaction.origin set

1 Like

Thanks, it is a good idea. But I want to do it without relying on the client to send such data.

@rezass Why not? Where else would the data come from?

My use case is that I want to limit clients to only working on particular Y.Text objects and prevent them from creating new ones.

If I am understanding you correctly, you want some kind of access control. While you cannot apply access control at the shared type level, you can apply access control to individual documents. So you could have one YText per YDoc and then implement the access rules on the server.

Authentication and authorization are not supported out of the box by Yjs, but there are a few implementations floating around if this is what you’re looking for.

Thanks! This will solve most of my challenges. The user will still be able to create objects in YDoc, which can’t be prevented with this approach.