Skip to content

Commit 866dbe1

Browse files
committed
fix(history): linear history, setsize had incorrect behavior
1 parent e126915 commit 866dbe1

File tree

2 files changed

+76
-1
lines changed

2 files changed

+76
-1
lines changed

src/impl/undo/UndoHistoryImpl.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,12 @@ export class UndoHistoryImpl extends UndoHistory {
167167
public setSizeMax(max: number): void {
168168
if (max >= 0) {
169169
const removed = this.undos.splice(0, this.undos.length - max);
170-
if (this.undos.length === 0 && removed.length > 0) {
170+
const willRemoved = removed.length > 0 || this.redos.length > 0;
171+
if (removed.length > 0 && this.undos.length === 0) {
171172
this.undoPublisher.next(undefined);
173+
}
174+
this.clearRedo();
175+
if (willRemoved) {
172176
this.publishSize();
173177
}
174178
this.sizeMax = max;

test/undo/UndoHistory.test.ts

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,77 @@ describe("using an undo history", () => {
320320
});
321321
expect(history.size()).toStrictEqual([0, 0]);
322322
});
323+
324+
test("set size max greater", () => {
325+
history.setSizeMax(2);
326+
history.add(undoable);
327+
history.add(undoable2);
328+
329+
testScheduler.run(helpers => {
330+
const {cold, expectObservable} = helpers;
331+
cold("-a", {
332+
"a": () => history.setSizeMax(100)
333+
}).subscribe(v => v());
334+
335+
expectObservable(history.redosObservable()).toBe("--");
336+
expectObservable(history.undosObservable()).toBe("--");
337+
expectObservable(history.sizeObservable()).toBe("--");
338+
});
339+
340+
expect(history.size()).toStrictEqual([2, 0]);
341+
});
342+
343+
test("set size max to 0", () => {
344+
history.setSizeMax(10);
345+
history.add(undoable);
346+
history.add(undoable2);
347+
history.undo();
348+
349+
testScheduler.run(helpers => {
350+
const {cold, expectObservable} = helpers;
351+
cold("-a", {
352+
"a": () => history.setSizeMax(0)
353+
}).subscribe(v => v());
354+
355+
expectObservable(history.undosObservable()).toBe("-a", {"a": undefined});
356+
expectObservable(history.redosObservable()).toBe("-a", {"a": undefined});
357+
expectObservable(history.sizeObservable()).toBe("-a", {"a": [0, 0]});
358+
});
359+
360+
expect(history.size()).toStrictEqual([0, 0]);
361+
});
362+
363+
test("set size max to lower value", () => {
364+
history.setSizeMax(10);
365+
history.add(undoable);
366+
history.add(undoable2);
367+
history.undo();
368+
369+
testScheduler.run(helpers => {
370+
const {cold, expectObservable} = helpers;
371+
cold("-a", {
372+
"a": () => history.setSizeMax(1)
373+
}).subscribe(v => v());
374+
375+
expectObservable(history.undosObservable()).toBe("--");
376+
expectObservable(history.redosObservable()).toBe("-a", {"a": undefined});
377+
expectObservable(history.sizeObservable()).toBe("-a", {"a": [1, 0]});
378+
});
379+
380+
expect(history.size()).toStrictEqual([1, 0]);
381+
});
382+
});
383+
384+
test("use size max clean redos", () => {
385+
history.add(undoable);
386+
history.add(undoable2);
387+
history.add(mock());
388+
history.add(mock());
389+
history.undo();
390+
history.undo();
391+
history.setSizeMax(1);
392+
393+
expect(history.size()).toStrictEqual([1, 0]);
323394
});
324395

325396
test("crash in undo OK", () => {

0 commit comments

Comments
 (0)