changeset 465:caef9a302bec

Add methods for mutating the current state without updating the history (naming might not be that declarative).
author Lucas Thompson <dev@lucas.im>
date Fri, 30 Jun 2017 14:49:40 +0100
parents 50f61d1945db
children 8820a133bcf5
files src/app/Session.ts
diffstat 1 files changed, 18 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/src/app/Session.ts	Fri Jun 30 14:01:22 2017 +0100
+++ b/src/app/Session.ts	Fri Jun 30 14:49:40 2017 +0100
@@ -72,17 +72,27 @@
     this.historyOffset = 0;
   }
 
-  shift(): T {
+  shiftMutating(): T {
     const item = this.stack[0];
     this.stack = this.stack.slice(1);
+    return item;
+  }
+
+  shift(): T {
+    const item = this.shiftMutating();
     this.history.push([...this.stack]);
     return item;
   }
 
+  unshiftMutating(item: T): number {
+    this.stack = [item, ...this.stack];
+    return this.stack.length;
+  }
+
   unshift(item: T): number {
-    this.stack = [item, ...this.stack];
+    const newLength = this.unshift(item);
     this.history.push([...this.stack]);
-    return this.stack.length;
+    return newLength;
   }
 
   findIndex(predicate: (value: T,
@@ -100,12 +110,16 @@
   }
 
   set(index: number, value: T) {
+    this.setMutating(index, value);
+    this.history.push([...this.stack]);
+  }
+
+  setMutating(index: number, value: T) {
     this.stack = [
       ...this.stack.slice(0, index),
       value,
       ...this.stack.slice(index + 1)
     ];
-    this.history.push([...this.stack]);
   }
 
   map<U>(transform: (value: T, index: number, array: T[]) => U): U[] {