From 60a63831b05de43349b166eb449df42ce076ee47 Mon Sep 17 00:00:00 2001 From: Scott Worley Date: Tue, 25 Jan 2022 22:13:35 -0800 Subject: [PATCH] Persistence --- vopamoi.ts | 46 +++++++++++++++++++++++++++++++++++----------- 1 file changed, 35 insertions(+), 11 deletions(-) diff --git a/vopamoi.ts b/vopamoi.ts index 6cfdbf2..4d6cd86 100644 --- a/vopamoi.ts +++ b/vopamoi.ts @@ -37,18 +37,41 @@ const Model = { }, }; -const Log = { - addTask: function (description: string) { - this.applyLogEntry(`${Date.now()} Create ${description}`); - }, +const Log = (function () { + var next_log_index = 0; + return { + addTask: function (description: string) { + this.recordAndApplyLogEntry(`${Date.now()} Create ${description}`); + }, - applyLogEntry: function (entry: string) { - const [timestamp, command, data] = splitN(entry, " ", 2); - if (command == "Create") { - Model.addTask(data); - } - }, -}; + applyLogEntry: function (entry: string) { + const [timestamp, command, data] = splitN(entry, " ", 2); + if (command == "Create") { + Model.addTask(data); + } + }, + + recordLogEntry: function (entry: string) { + window.localStorage.setItem(`${next_log_index++}`, entry); + }, + + recordAndApplyLogEntry: function (entry: string) { + this.recordLogEntry(entry); + this.applyLogEntry(entry); + }, + + replay: function () { + while (true) { + const entry = window.localStorage.getItem(`${next_log_index}`); + if (entry === null) { + break; + } + this.applyLogEntry(entry); + next_log_index++; + } + }, + }; +})(); function handleKey(event: any) { if (event.target.tagName !== "INPUT") { @@ -71,4 +94,5 @@ function browserCreateTask(form: any) { function browserInit() { document.body.addEventListener("keydown", handleKey, { capture: false }); + Log.replay(); } -- 2.44.1