Determining whether a transaction is local

Using, for example, ymap.observe:

ymap.observe(function(YMapEvent, Transaction)) Registers a change observer that will be called synchronously every time this shared type is modified

there seems to be at least three ways of finding out whether the change is local to this client:

  1. event.transaction.origin is null
  2. event.transaction.local is true
  3. save the local clientID with the value when a new value is set using ymap.set() and check this.

Which is the best? Will they all work equivalently?

I use transaction.local for this determination since its name implies its meaning. That said, I don’t know if there is a technical reason to prefer other methods.

You should prefer transaction.local to determine if a transaction was created locally. All changes from the providers are local = false.

The third method won’t work when you only delete a property (as there is no item created with an id).

If possible, you should actually use transaction.origin to determine the origin of a transaction. If the change was created by a provider (or an editor-binding) the provider-instance will be set as the origin (respectively the editor-binding instance). I want to establish a pattern that each library set the origin when they create a change. Users can create changes with just null or create their own origin.

1 Like

If the change was created by a provider (or an editor-binding) the provider-instance will be set as the origin (respectively the editor-binding instance).

Seems this is not true with y-indexeddb, from my testing, transaction from y-indexeddb has local=true and origin=null, it does not set the origin correctly to be identified as a provider.

That can very well be. Thanks for reporting @yufw! It is up to the provider to set local and origin correctly.

Can you please report an issue in y-indexeddb and maybe some steps to reproduce it? I guess this happens on initial load?

Reported an issue here, y-indexeddb does not set the correct origin on initial load · Issue #17 · yjs/y-indexeddb · GitHub

1 Like