diff options
-rw-r--r-- | u/history.ts | 36 | ||||
-rw-r--r-- | u/mod.ts | 1 |
2 files changed, 37 insertions, 0 deletions
diff --git a/u/history.ts b/u/history.ts new file mode 100644 index 0000000..5b13961 --- /dev/null +++ b/u/history.ts @@ -0,0 +1,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; + } +} @@ -3,3 +3,4 @@ export * from "./leftpadesque/mod.ts"; export * from "./process/mod.ts"; export * from "./trace/mod.ts"; export * from "./server/mod.ts"; +export * from "./history.ts"; |