Skip to content

Commit 61d4dab

Browse files
committed
feat: add debounce clear batch timer
1 parent 2647a7e commit 61d4dab

File tree

2 files changed

+32
-13
lines changed

2 files changed

+32
-13
lines changed

packages/collaboration-manager/src/CollaborationManager.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import { BatchedOperation } from './BatchedOperation.js';
1414
import { type ModifyOperationData, Operation, OperationType } from './Operation.js';
1515
import { UndoRedoManager } from './UndoRedoManager.js';
1616

17+
const DEBOUCE_TIMEOUT = 500;
18+
1719
/**
1820
* CollaborationManager listens to EditorJSModel events and applies operations
1921
*/
@@ -48,6 +50,11 @@ export class CollaborationManager {
4850
*/
4951
#client: OTClient | null = null;
5052

53+
/**
54+
* Debounce timer to move current batch to undo stack after a delay
55+
*/
56+
#debounceTimer?: ReturnType<typeof setTimeout>;
57+
5158
/**
5259
* Creates an instance of CollaborationManager
5360
*
@@ -249,24 +256,25 @@ export class CollaborationManager {
249256
*/
250257
if (this.#currentBatch === null) {
251258
this.#currentBatch = new BatchedOperation(operation);
259+
this.#debounce();
252260

253261
return;
254262
}
255263

256264
/**
257265
* If current operation could not be added to the batch, then terminate current batch and create a new one with current operation
258-
*
259-
* @todo - add debounce timeout 500ms
260266
*/
261267
if (!this.#currentBatch.canAdd(operation)) {
262268
this.#moveBatchToUndo();
263269

264270
this.#currentBatch = new BatchedOperation(operation);
271+
this.#debounce();
265272

266273
return;
267274
}
268275

269276
this.#currentBatch.add(operation);
277+
this.#debounce();
270278
}
271279

272280
/**
@@ -279,4 +287,15 @@ export class CollaborationManager {
279287
this.#currentBatch = null;
280288
}
281289
}
290+
291+
/**
292+
* Debouneces timer of #moveBatchToUndo method
293+
*/
294+
#debounce(): void {
295+
clearTimeout(this.#debounceTimer);
296+
297+
this.#debounceTimer = setTimeout(() => {
298+
this.#moveBatchToUndo();
299+
}, DEBOUCE_TIMEOUT);
300+
}
282301
}

packages/collaboration-manager/src/UndoRedoManager.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,6 @@ export class UndoRedoManager {
1414
*/
1515
#redoStack: Operation[] = [];
1616

17-
/**
18-
* Transforms passed operations stack against the operation
19-
*
20-
* @param operation - operation to transform against
21-
* @param stack - stack to transform
22-
* @returns - new transformed list of operations
23-
*/
24-
private transformStack(operation: Operation, stack: Operation[]): Operation[] {
25-
return stack.map((op: Operation) => op.transform(operation));
26-
}
27-
2817
/**
2918
* Returns operation to undo (if any)
3019
*/
@@ -79,4 +68,15 @@ export class UndoRedoManager {
7968
this.#undoStack = this.transformStack(operation, this.#undoStack);
8069
this.#redoStack = this.transformStack(operation, this.#redoStack);
8170
}
71+
72+
/**
73+
* Transforms passed operations stack against the operation
74+
*
75+
* @param operation - operation to transform against
76+
* @param stack - stack to transform
77+
* @returns - new transformed list of operations
78+
*/
79+
private transformStack(operation: Operation, stack: Operation[]): Operation[] {
80+
return stack.map((op: Operation) => op.transform(operation));
81+
}
8282
}

0 commit comments

Comments
 (0)