Skip to content

Commit 46157da

Browse files
committed
fix(history): linear history must not have a size max by default
1 parent adff247 commit 46157da

3 files changed

Lines changed: 16 additions & 9 deletions

File tree

src/api/undo/UndoHistory.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,9 @@ export abstract class UndoHistory implements UndoHistoryBase {
6363
public abstract getSizeMax(): number;
6464

6565
/**
66-
* @param max - The max number of saved undoable objects. Must be great than 0.
66+
* @param max - The maximal number of undoable objects to store.
67+
* -1 (or any negative value) corresponds to an unlimited size.
68+
* 0 corresponds to a history that stores nothing.
6769
*/
6870
public abstract setSizeMax(max: number): void;
6971

src/impl/undo/UndoHistoryImpl.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ export class UndoHistoryImpl extends UndoHistory {
3535
public readonly considersEqualCmds: boolean;
3636

3737
/**
38-
* The maximal number of undo.
38+
* The maximal number of undoable objects.
39+
* -1 (or any negative value) corresponds to an unlimited size.
40+
* 0 corresponds to a history that stores nothing.
3941
*/
4042
private sizeMax: number;
4143

@@ -54,10 +56,9 @@ export class UndoHistoryImpl extends UndoHistory {
5456
*/
5557
public constructor(considerEqualCmd = false) {
5658
super();
57-
this.sizeMax = 0;
59+
this.sizeMax = -1;
5860
this.undos = [];
5961
this.redos = [];
60-
this.sizeMax = 20;
6162
this.undoPublisher = new Subject();
6263
this.redoPublisher = new Subject();
6364
this.sizePublisher = new Subject();
@@ -89,7 +90,7 @@ export class UndoHistoryImpl extends UndoHistory {
8990
}
9091

9192
public add(undoable: Undoable): void {
92-
if (this.sizeMax > 0) {
93+
if (this.sizeMax !== 0) {
9394
if (this.undos.length === this.sizeMax) {
9495
this.undos.shift();
9596
}
@@ -175,8 +176,8 @@ export class UndoHistoryImpl extends UndoHistory {
175176
if (willRemoved) {
176177
this.publishSize();
177178
}
178-
this.sizeMax = max;
179179
}
180+
this.sizeMax = max;
180181
}
181182

182183
public getUndo(): ReadonlyArray<Undoable> {

test/undo/UndoHistory.test.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,10 +184,14 @@ describe("using an undo history", () => {
184184
expect(history.getSizeMax()).toBe(21);
185185
});
186186

187-
test("sizeMaxMutatorsSizeKO", () => {
188-
history.setSizeMax(5);
187+
test("changing size max to negative one is ok and unlimited", () => {
188+
history.setSizeMax(2);
189189
history.setSizeMax(-1);
190-
expect(history.getSizeMax()).toBe(5);
190+
history.add(undoable);
191+
history.add(undoable2);
192+
history.add(mock<Undoable>());
193+
expect(history.getSizeMax()).toBe(-1);
194+
expect(history.size()).toStrictEqual([3, 0]);
191195
});
192196

193197
test("getLastRedoNothingStart", () => {

0 commit comments

Comments
 (0)