]> git.scottworley.com Git - vopamoi/commitdiff
Factor out some boilerplate in UI
authorScott Worley <scottworley@scottworley.com>
Fri, 28 Jan 2022 07:36:26 +0000 (23:36 -0800)
committerScott Worley <scottworley@scottworley.com>
Fri, 28 Jan 2022 07:36:26 +0000 (23:36 -0800)
vopamoi.ts

index 24b8774a4225036243e3fac4aaf81e9e5c791bc0..dc61007ec9724a2ec417b1f786670c93a6cc3f6a 100644 (file)
@@ -216,6 +216,10 @@ const log = Log();
 
 function UI() {
   const undoLog: string[] = [];
+  function perform(forward: string, reverse: string) {
+    undoLog.push(reverse);
+    return log.recordAndApply(`${clock.now()} ${forward}`);
+  }
   return {
     addTask: function (description: string): Element {
       const now = clock.now();
@@ -223,24 +227,19 @@ function UI() {
       return <Element>log.recordAndApply(`${now} Create ${description}`);
     },
     addTag: function (createTimestamp: string, tag: string) {
-      undoLog.push(`Untag ${createTimestamp} ${tag}`);
-      return log.recordAndApply(`${clock.now()} Tag ${createTimestamp} ${tag}`);
+      return perform(`Tag ${createTimestamp} ${tag}`, `Untag ${createTimestamp} ${tag}`);
     },
     edit: function (createTimestamp: string, newDescription: string, oldDescription: string) {
-      undoLog.push(`Edit ${createTimestamp} ${oldDescription}`);
-      return log.recordAndApply(`${clock.now()} Edit ${createTimestamp} ${newDescription}`);
+      return perform(`Edit ${createTimestamp} ${newDescription}`, `Edit ${createTimestamp} ${oldDescription}`);
     },
     removeTag: function (createTimestamp: string, tag: string) {
-      undoLog.push(`Tag ${createTimestamp} ${tag}`);
-      return log.recordAndApply(`${clock.now()} Untag ${createTimestamp} ${tag}`);
+      return perform(`Untag ${createTimestamp} ${tag}`, `Tag ${createTimestamp} ${tag}`);
     },
     setPriority: function (createTimestamp: string, newPriority: number, oldPriority: number) {
-      undoLog.push(`Priority ${createTimestamp} ${oldPriority}`);
-      return log.recordAndApply(`${clock.now()} Priority ${createTimestamp} ${newPriority}`);
+      return perform(`Priority ${createTimestamp} ${newPriority}`, `Priority ${createTimestamp} ${oldPriority}`);
     },
     setState: function (createTimestamp: string, newState: string, oldState: string) {
-      undoLog.push(`State ${createTimestamp} ${oldState}`);
-      return log.recordAndApply(`${clock.now()} State ${createTimestamp} ${newState}`);
+      return perform(`State ${createTimestamp} ${newState}`, `State ${createTimestamp} ${oldState}`);
     },
     undo: function () {
       if (undoLog.length > 0) {