Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(core): if a transaction is a no-op do not attempt to re-apply it #4535 #5449

Merged
merged 2 commits into from
Dec 31, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/tame-worms-applaud.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@tiptap/core": patch
---

If a transaction results in the exact same editor state (either filtered out or failed to apply) then do not attempt to re-apply the same editor state and do not emit any events associated to the transaction
31 changes: 24 additions & 7 deletions packages/core/src/Editor.ts
Original file line number Diff line number Diff line change
@@ -197,7 +197,7 @@ export class Editor extends EventEmitter<EditorEvents> {
this.setOptions({ editable })

if (emitUpdate) {
this.emit('update', { editor: this, transaction: this.state.tr })
this.emit('update', { editor: this, transaction: this.state.tr, appendedTransactions: [] })
}
}

@@ -457,18 +457,29 @@ export class Editor extends EventEmitter<EditorEvents> {
return
}

const state = this.state.apply(transaction)
// Apply transaction and get resulting state and transactions
const { state, transactions } = this.state.applyTransaction(transaction)
const selectionHasChanged = !this.state.selection.eq(state.selection)
const rootTrWasApplied = transactions.includes(transaction)

this.emit('beforeTransaction', {
editor: this,
transaction,
nextState: state,
})

// If transaction was filtered out, we can return early
if (!rootTrWasApplied) {
return
}

this.view.updateState(state)

// Emit transaction event with appended transactions info
this.emit('transaction', {
editor: this,
transaction,
appendedTransactions: transactions.slice(1),
})

if (selectionHasChanged) {
@@ -478,32 +489,38 @@ export class Editor extends EventEmitter<EditorEvents> {
})
}

const focus = transaction.getMeta('focus')
const blur = transaction.getMeta('blur')
// Only emit the latest between focus and blur events
const mostRecentFocusTr = transactions.findLast(tr => tr.getMeta('focus') || tr.getMeta('blur'))
const focus = mostRecentFocusTr?.getMeta('focus')
const blur = mostRecentFocusTr?.getMeta('blur')

if (focus) {
this.emit('focus', {
editor: this,
event: focus.event,
transaction,
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
transaction: mostRecentFocusTr!,
})
}

if (blur) {
this.emit('blur', {
editor: this,
event: blur.event,
transaction,
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
transaction: mostRecentFocusTr!,
})
}

if (!transaction.docChanged || transaction.getMeta('preventUpdate')) {
// Compare states for update event
if (transaction.getMeta('preventUpdate') || transactions.every(tr => !tr.docChanged) || this.state.doc.eq(state.doc)) {
return
}

this.emit('update', {
editor: this,
transaction,
appendedTransactions: transactions.slice(1),
})
}

4 changes: 2 additions & 2 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
@@ -58,10 +58,10 @@ export interface EditorEvents {
*/
disableCollaboration: () => void;
};
update: { editor: Editor; transaction: Transaction };
update: { editor: Editor; transaction: Transaction; appendedTransactions: Transaction[] };
selectionUpdate: { editor: Editor; transaction: Transaction };
beforeTransaction: { editor: Editor; transaction: Transaction; nextState: EditorState };
transaction: { editor: Editor; transaction: Transaction };
transaction: { editor: Editor; transaction: Transaction; appendedTransactions: Transaction[] };
focus: { editor: Editor; event: FocusEvent; transaction: Transaction };
blur: { editor: Editor; event: FocusEvent; transaction: Transaction };
destroy: void;