summaryrefslogtreecommitdiff
path: root/u/history.ts
blob: 5b1396197c5c6596ba9b6ae1161f1e671d35dda0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
export interface History<T> {
    undo: () => History<T> | undefined;
    redo: () => History<T> | undefined;

    get: () => T;
    add: (value: T) => History<T>;
}

export class HistoryImpl<T> implements History<T> {
    private readonly item: T;
    private previous?: History<T>;
    private next?: History<T>;

    constructor(item: T) {
        this.item = item;
    }

    public get(): T {
        return this.item;
    }

    public undo(): History<T> | undefined {
        return this.previous;
    }

    public redo(): History<T> | undefined {
        return this.next;
    }

    public add(value: T): History<T> {
        const newHistory = new HistoryImpl(value);
        newHistory.previous = this;
        this.next = newHistory;
        return newHistory;
    }
}