Skip to content

Commit 95145c8

Browse files
committed
fixes after new invalidation logic
1 parent 393ca9b commit 95145c8

File tree

1 file changed

+32
-66
lines changed

1 file changed

+32
-66
lines changed

src/data/model/MutableObject.ts

Lines changed: 32 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { HashReference } from './HashReference';
1212
import { Lock } from 'util/concurrency';
1313
import { MultiMap } from 'util/multimap';
1414
import { Resources } from 'spaces/Resources';
15+
import { CascadedInvalidateOp } from './CascadedInvalidateOp';
1516
//import { ObjectStateAgent } from 'sync/agents/state/ObjectStateAgent';
1617
//import { TerminalOpsStateAgent } from 'sync/agents/state/TerminalOpsStateAgent';
1718

@@ -27,7 +28,7 @@ abstract class MutableObject extends HashedObject {
2728

2829
_allAppliedOps : Set<Hash>;
2930
_terminalOps : Map<Hash, HashReference<MutationOp>>;
30-
_undoOpsPerOp : MultiMap<Hash, Hash>;
31+
_activeUndosOpsPerOp : MultiMap<Hash, Hash>;
3132

3233

3334
_unsavedOps : Array<MutationOp>;
@@ -47,11 +48,8 @@ abstract class MutableObject extends HashedObject {
4748
this._supportsUndo = supportsUndo;
4849

4950
if (supportsUndo) {
50-
if (acceptedOpClasses.indexOf(UndoOp.className) < 0) {
51-
acceptedOpClasses.push(UndoOp.className);
52-
}
53-
if (acceptedOpClasses.indexOf(RedoOp.className) < 0) {
54-
acceptedOpClasses.push(RedoOp.className);
51+
if (acceptedOpClasses.indexOf(CascadedInvalidateOp.className) < 0) {
52+
acceptedOpClasses.push(CascadedInvalidateOp.className);
5553
}
5654
}
5755

@@ -60,7 +58,7 @@ abstract class MutableObject extends HashedObject {
6058

6159
this._allAppliedOps = new Set();
6260
this._terminalOps = new Map();
63-
this._undoOpsPerOp = new MultiMap();
61+
this._activeUndosOpsPerOp = new MultiMap();
6462

6563
this._unsavedOps = [];
6664
this._unappliedOps = new Map();
@@ -76,12 +74,6 @@ abstract class MutableObject extends HashedObject {
7674

7775
abstract mutate(op: MutationOp, isNew: boolean): Promise<boolean>;
7876

79-
async undo(op: MutationOp, isNew: boolean): Promise<boolean> {
80-
op; isNew;
81-
82-
throw new Error('Class "' + this.getClassName() + '" does not support operation undo, yet one was received.');
83-
}
84-
8577
addMutationCallback(cb: (mut: MutationOp) => void) {
8678
this._externalMutationCallbacks.add(cb);
8779
}
@@ -298,68 +290,42 @@ abstract class MutableObject extends HashedObject {
298290

299291
this._allAppliedOps.add(opHash);
300292

301-
302-
if (op instanceof UndoOp) {
303-
304-
const targetOp = op.targetOp as MutationOp;
305-
const targetOpHash = targetOp.hash();
306-
307-
const alreadyUndone = this._undoOpsPerOp.get(targetOpHash).size > 0;
293+
let shouldMutate = true;
308294

309-
this._undoOpsPerOp.add(targetOpHash, opHash);
295+
if (op instanceof CascadedInvalidateOp) {
310296

311-
if (!alreadyUndone) {
312-
const done = this.undo(targetOp, isNew).then((mutated: boolean) => {
313-
if (mutated) {
314-
for (const cb of this._externalMutationCallbacks) {
315-
cb(op);
316-
}
317-
}
318-
});
319-
320-
return done;
321-
} else {
322-
return Promise.resolve();
297+
let currentOp = op;
298+
while (currentOp.getTargetOp() instanceof CascadedInvalidateOp) {
299+
currentOp = currentOp.getTargetOp() as CascadedInvalidateOp;
323300
}
324301

325-
} else {
326-
327-
let targetOp = op;
328-
let needToApply = true;
329-
330-
if (op instanceof RedoOp) {
331-
332-
const targetUndoOp = op.targetOp as UndoOp;
333-
const targetUndoOpHash = targetUndoOp.hash();
302+
const finalTargetOp = currentOp.getTargetOp();
303+
const finalTargetOpHash = finalTargetOp.hash();
334304

335-
targetOp = targetUndoOp.targetOp as MutationOp;
336-
337-
const targetOpHash = targetOp.hash();
338-
339-
const wasUndone = this._undoOpsPerOp.get(targetOpHash).size > 0;
340-
341-
this._undoOpsPerOp.delete(targetOpHash, targetUndoOpHash);
342-
343-
const shouldRedo = wasUndone && this._undoOpsPerOp.get(targetOpHash).size === 0;
305+
306+
const isUndone = this._activeUndosOpsPerOp.get(finalTargetOpHash).size > 0;
344307

345-
needToApply = shouldRedo;
308+
shouldMutate = isUndone !== op.undo;
346309

310+
if (op.undo) {
311+
this._activeUndosOpsPerOp.add(finalTargetOpHash, opHash);
312+
} else { // redo
313+
this._activeUndosOpsPerOp.delete(finalTargetOpHash, op.getTargetOp().hash());
347314
}
315+
}
348316

349-
if (needToApply) {
350-
const done = this.mutate(targetOp, isNew).then((mutated: boolean) => {
351-
if (mutated) {
352-
for (const cb of this._externalMutationCallbacks) {
353-
cb(targetOp);
354-
}
355-
}
356-
});
357-
358-
return done;
359-
} else {
360-
return Promise.resolve();
361-
}
362-
317+
if (shouldMutate) {
318+
const done = this.mutate(op, isNew).then((mutated: boolean) => {
319+
if (mutated) {
320+
for (const cb of this._externalMutationCallbacks) {
321+
cb(op);
322+
}
323+
}
324+
});
325+
326+
return done;
327+
} else {
328+
return Promise.resolve();
363329
}
364330
}
365331

0 commit comments

Comments
 (0)