Skip to content

Commit 6cf9ac4

Browse files
committed
perf: this caches the transaction diffing to not have to recompute
1 parent e9e2198 commit 6cf9ac4

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

packages/core/src/api/nodeUtil.ts

+18-2
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ export type BlocksChanged<
9999
BSchema extends BlockSchema = DefaultBlockSchema,
100100
ISchema extends InlineContentSchema = DefaultInlineContentSchema,
101101
SSchema extends StyleSchema = DefaultStyleSchema
102-
> = Array<
102+
> = ReadonlyArray<
103103
{
104104
/**
105105
* The affected block.
@@ -149,6 +149,15 @@ function areBlocksDifferentExcludingChildren<
149149
);
150150
}
151151

152+
const blocksChangedMap = new WeakMap<
153+
Transaction,
154+
{
155+
appendedTransactions: Transaction[];
156+
blocksChanged: BlocksChanged<any, any, any>;
157+
}
158+
>();
159+
const defaultAppendedTransactions: Transaction[] = [];
160+
152161
/**
153162
* Get the blocks that were changed by a transaction.
154163
* @param transaction The transaction to get the changes from.
@@ -161,8 +170,15 @@ export function getBlocksChangedByTransaction<
161170
SSchema extends StyleSchema = DefaultStyleSchema
162171
>(
163172
transaction: Transaction,
164-
appendedTransactions: Transaction[] = []
173+
appendedTransactions: Transaction[] = defaultAppendedTransactions
165174
): BlocksChanged<BSchema, ISchema, SSchema> {
175+
if (
176+
blocksChangedMap.has(transaction) &&
177+
blocksChangedMap.get(transaction)!.appendedTransactions ===
178+
appendedTransactions
179+
) {
180+
return blocksChangedMap.get(transaction)!.blocksChanged;
181+
}
166182
let source: BlockChangeSource = { type: "local" };
167183

168184
if (transaction.getMeta("paste")) {

0 commit comments

Comments
 (0)