Mercurial > hg > ugly-duckling
comparison src/app/Session.ts @ 467:b963af7e6eaf
Clear undo history ahead of read pointer when updating the history.
author | Lucas Thompson <dev@lucas.im> |
---|---|
date | Fri, 30 Jun 2017 15:27:56 +0100 |
parents | caef9a302bec |
children | 2fb2357420f9 |
comparison
equal
deleted
inserted
replaced
466:8820a133bcf5 | 467:b963af7e6eaf |
---|---|
78 return item; | 78 return item; |
79 } | 79 } |
80 | 80 |
81 shift(): T { | 81 shift(): T { |
82 const item = this.shiftMutating(); | 82 const item = this.shiftMutating(); |
83 this.history.push([...this.stack]); | 83 this.updateHistory(); |
84 return item; | 84 return item; |
85 } | 85 } |
86 | 86 |
87 unshiftMutating(item: T): number { | 87 unshiftMutating(item: T): number { |
88 this.stack = [item, ...this.stack]; | 88 this.stack = [item, ...this.stack]; |
89 return this.stack.length; | 89 return this.stack.length; |
90 } | 90 } |
91 | 91 |
92 unshift(item: T): number { | 92 unshift(item: T): number { |
93 const newLength = this.unshift(item); | 93 const newLength = this.unshift(item); |
94 this.history.push([...this.stack]); | 94 this.updateHistory(); |
95 return newLength; | 95 return newLength; |
96 } | 96 } |
97 | 97 |
98 findIndex(predicate: (value: T, | 98 findIndex(predicate: (value: T, |
99 index: number, | 99 index: number, |
109 return this.stack[index]; | 109 return this.stack[index]; |
110 } | 110 } |
111 | 111 |
112 set(index: number, value: T) { | 112 set(index: number, value: T) { |
113 this.setMutating(index, value); | 113 this.setMutating(index, value); |
114 this.history.push([...this.stack]); | 114 this.updateHistory(); |
115 } | 115 } |
116 | 116 |
117 setMutating(index: number, value: T) { | 117 setMutating(index: number, value: T) { |
118 this.stack = [ | 118 this.stack = [ |
119 ...this.stack.slice(0, index), | 119 ...this.stack.slice(0, index), |
139 if (!indices.includes(i)) { | 139 if (!indices.includes(i)) { |
140 acc.push(item); | 140 acc.push(item); |
141 } | 141 } |
142 return acc; | 142 return acc; |
143 }, [] as T[]); | 143 }, [] as T[]); |
144 this.history.push([...this.stack]); | 144 this.updateHistory(); |
145 } | 145 } |
146 | 146 |
147 stepBack(): void { | 147 stepBack(): void { |
148 const latest = this.history.length - 1; | 148 const latest = this.history.length - 1; |
149 if (++this.historyOffset <= latest) { | 149 if (++this.historyOffset <= latest) { |
160 } else { | 160 } else { |
161 this.historyOffset = 0; | 161 this.historyOffset = 0; |
162 } | 162 } |
163 } | 163 } |
164 | 164 |
165 updateHistory(): void { | |
166 if (this.historyOffset !== 0) { | |
167 this.history = this.history.slice( | |
168 0, | |
169 this.history.length - this.historyOffset | |
170 ); | |
171 this.historyOffset = 0; | |
172 } | |
173 this.history.push([...this.stack]); | |
174 } | |
175 | |
165 toIterable(): Iterable<T> { | 176 toIterable(): Iterable<T> { |
166 return this.stack; | 177 return this.stack; |
167 } | 178 } |
168 } | 179 } |