From: Scott Worley Date: Wed, 26 Jan 2022 06:13:35 +0000 (-0800) Subject: Persistence X-Git-Url: http://git.scottworley.com/vopamoi/commitdiff_plain/60a63831b05de43349b166eb449df42ce076ee47?ds=sidebyside Persistence --- 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(); }