allow transactions within event handlers having different origins
This commit is contained in:
		
							parent
							
								
									cac9407185
								
							
						
					
					
						commit
						3ece681758
					
				@ -1064,7 +1064,7 @@ export class YText extends AbstractType {
 | 
				
			|||||||
        n = n.right
 | 
					        n = n.right
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
      packStr()
 | 
					      packStr()
 | 
				
			||||||
    }, splitSnapshotAffectedStructs)
 | 
					    }, 'cleanup')
 | 
				
			||||||
    return ops
 | 
					    return ops
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -251,7 +251,6 @@ const cleanupTransactions = (transactionCleanups, i) => {
 | 
				
			|||||||
    try {
 | 
					    try {
 | 
				
			||||||
      sortAndMergeDeleteSet(ds)
 | 
					      sortAndMergeDeleteSet(ds)
 | 
				
			||||||
      transaction.afterState = getStateVector(transaction.doc.store)
 | 
					      transaction.afterState = getStateVector(transaction.doc.store)
 | 
				
			||||||
      doc._transaction = null
 | 
					 | 
				
			||||||
      doc.emit('beforeObserverCalls', [transaction, doc])
 | 
					      doc.emit('beforeObserverCalls', [transaction, doc])
 | 
				
			||||||
      /**
 | 
					      /**
 | 
				
			||||||
       * An array of event callbacks.
 | 
					       * An array of event callbacks.
 | 
				
			||||||
@ -398,7 +397,10 @@ export const transact = (doc, f, origin = null, local = true) => {
 | 
				
			|||||||
  try {
 | 
					  try {
 | 
				
			||||||
    f(doc._transaction)
 | 
					    f(doc._transaction)
 | 
				
			||||||
  } finally {
 | 
					  } finally {
 | 
				
			||||||
    if (initialCall && transactionCleanups[0] === doc._transaction) {
 | 
					    if (initialCall) {
 | 
				
			||||||
 | 
					      const finishCleanup = doc._transaction === transactionCleanups[0]
 | 
				
			||||||
 | 
					      doc._transaction = null
 | 
				
			||||||
 | 
					      if (finishCleanup) {
 | 
				
			||||||
        // The first transaction ended, now process observer calls.
 | 
					        // The first transaction ended, now process observer calls.
 | 
				
			||||||
        // Observer call may create new transactions for which we need to call the observers and do cleanup.
 | 
					        // Observer call may create new transactions for which we need to call the observers and do cleanup.
 | 
				
			||||||
        // We don't want to nest these calls, so we execute these calls one after
 | 
					        // We don't want to nest these calls, so we execute these calls one after
 | 
				
			||||||
@ -410,4 +412,5 @@ export const transact = (doc, f, origin = null, local = true) => {
 | 
				
			|||||||
        cleanupTransactions(transactionCleanups, 0)
 | 
					        cleanupTransactions(transactionCleanups, 0)
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -2,6 +2,31 @@
 | 
				
			|||||||
import * as Y from '../src/index.js'
 | 
					import * as Y from '../src/index.js'
 | 
				
			||||||
import * as t from 'lib0/testing'
 | 
					import * as t from 'lib0/testing'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * @param {t.TestCase} _tc
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					export const testOriginInTransaction = _tc => {
 | 
				
			||||||
 | 
					  const doc = new Y.Doc()
 | 
				
			||||||
 | 
					  const ytext = doc.getText()
 | 
				
			||||||
 | 
					  /**
 | 
				
			||||||
 | 
					   * @type {Array<string>}
 | 
				
			||||||
 | 
					   */
 | 
				
			||||||
 | 
					  const origins = []
 | 
				
			||||||
 | 
					  doc.on('afterTransaction', (tr) => {
 | 
				
			||||||
 | 
					    origins.push(tr.origin)
 | 
				
			||||||
 | 
					    if (origins.length <= 1) {
 | 
				
			||||||
 | 
					      ytext.toDelta()
 | 
				
			||||||
 | 
					      doc.transact(() => {
 | 
				
			||||||
 | 
					        ytext.insert(0, 'a')
 | 
				
			||||||
 | 
					      }, 'nested')
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					  })
 | 
				
			||||||
 | 
					  doc.transact(() => {
 | 
				
			||||||
 | 
					    ytext.insert(0, '0')
 | 
				
			||||||
 | 
					  }, 'first')
 | 
				
			||||||
 | 
					  t.compareArrays(origins, ['first', 'cleanup', 'nested'])
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * Client id should be changed when an instance receives updates from another client using the same client id.
 | 
					 * Client id should be changed when an instance receives updates from another client using the same client id.
 | 
				
			||||||
 *
 | 
					 *
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user