summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--u/history.ts36
-rw-r--r--u/mod.ts1
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;
+ }
+}
diff --git a/u/mod.ts b/u/mod.ts
index 8397ce6..2ab8f68 100644
--- a/u/mod.ts
+++ b/u/mod.ts
@@ -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";