From c31e481a0776b7b696cb13ff6260831b0d527018 Mon Sep 17 00:00:00 2001 From: Elizabeth Hunt Date: Sat, 14 Jun 2025 18:54:05 -0700 Subject: Add history to pengueno --- u/history.ts | 36 ++++++++++++++++++++++++++++++++++++ u/mod.ts | 1 + 2 files changed, 37 insertions(+) create mode 100644 u/history.ts (limited to 'u') 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 { + undo: () => History | undefined; + redo: () => History | undefined; + + get: () => T; + add: (value: T) => History; +} + +export class HistoryImpl implements History { + private readonly item: T; + private previous?: History; + private next?: History; + + constructor(item: T) { + this.item = item; + } + + public get(): T { + return this.item; + } + + public undo(): History | undefined { + return this.previous; + } + + public redo(): History | undefined { + return this.next; + } + + public add(value: T): History { + 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"; -- cgit v1.2.3-70-g09d2