Skip to content

Commit

Permalink
group cleanups for YText changes into a single transaction
Browse files Browse the repository at this point in the history
Fixes #522 but is still massively slow
  • Loading branch information
NilSet committed Jun 12, 2023
1 parent 00ef472 commit 3741f43
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
11 changes: 10 additions & 1 deletion src/types/YText.js
Expand Up @@ -859,12 +859,21 @@ export class YText extends AbstractType {
_callObserver (transaction, parentSubs) {
super._callObserver(transaction, parentSubs)
const event = new YTextEvent(this, transaction, parentSubs)
const doc = transaction.doc
callTypeObservers(this, transaction, event)
// If a remote change happened, we try to cleanup potential formatting duplicates.
if (!transaction.local) {
transaction._yTexts.push(this)
}
}

/**
* @param {Transaction} transaction
*/
_cleanup (transaction) {
if (!transaction.local) {
// check if another formatting item was inserted
let foundFormattingItem = false
const doc = transaction.doc
for (const [client, afterClock] of transaction.afterState.entries()) {
const clock = transaction.beforeState.get(client) || 0
if (afterClock === clock) {
Expand Down
13 changes: 12 additions & 1 deletion src/utils/Transaction.js
Expand Up @@ -11,7 +11,7 @@ import {
Item,
generateNewClientId,
createID,
UpdateEncoderV1, UpdateEncoderV2, GC, StructStore, AbstractType, AbstractStruct, YEvent, Doc // eslint-disable-line
UpdateEncoderV1, UpdateEncoderV2, GC, StructStore, AbstractType, AbstractStruct, YEvent, Doc, YText // eslint-disable-line
} from '../internals.js'

import * as map from 'lib0/map'
Expand Down Expand Up @@ -114,6 +114,10 @@ export class Transaction {
* @type {Set<Doc>}
*/
this.subdocsLoaded = new Set()
/**
* @type {Array<YText>}
*/
this._yTexts = []
}
}

Expand Down Expand Up @@ -295,6 +299,13 @@ const cleanupTransactions = (transactionCleanups, i) => {
fs.push(() => doc.emit('afterTransaction', [transaction, doc]))
})
callAll(fs, [])
if (transaction._yTexts.length > 0) {
transact(doc, () => {
transaction._yTexts.forEach(yText => {
yText._cleanup(transaction)
})
})
}
} finally {
// Replace deleted items with ItemDeleted / GC.
// This is where content is actually remove from the Yjs Doc.
Expand Down

0 comments on commit 3741f43

Please sign in to comment.