How to implement throttling in multi-layered document changes

I am trying to use YDoc as my Data Model as follows:
All of them are YJS shared types.
When I want to do something , it will change on many positions.
I don’t like write below code everywhere

this.yDoc.transact(() => {
                this.yMap.set("value", v);

Is there a way to record all actions within a specific time limit and send them out together


otherwise I think the amount of changes will too large when meet some complex sence.

There isn’t a built-in solution to enqueue and throttle transactions, but you can roll your own pretty easily.

const transactThrottled = (() => {
  // queued setter functions
  const queue = []

  // throttled function that runs all functions as a transaction 
  // and clears the queue
  const transactAll = _.throttle(() => {
    yDoc.transact(() => {
      queue.forEach(f => f())
      queue = []
  }, 1000, { leading: false })

  // queue a single function and trigger a transaction at the 
  // next interval
  return f => {

// queue first change
transactThrottled(() => {
  this.yMap.set("value", v)

// queue second change
transactThrottled(() => {
  this.yMap.set("value", w)

// one transaction with all changes will be executed 
// after the throttle period

thx for your reply, it’s helpful
I’ve considered it before, but haven’t implemented it.
The worry of mine is whether this way will make the collaboration action wrong.
I think maybe should end the transaction immediately when observe function invoked if need.

another question is if using async logic
the local client will also changed in throttle, so the reaction will looks weird.
the better way is still record all the changes and send them together.

True, throttling changes up front will make it feel less “real-time” for other users.

The non-decreasing size of YJS documents is a big problem in my experience. There isn’t a good way to discard old updates, and coordinating a doc migration to have a clean start is difficult in a distributed environment.