@@ -109,14 +109,21 @@ export function isReviewing(baseState) {
109
109
return baseState . viewPos < baseState . queueLength
110
110
}
111
111
112
- export function moveBack ( baseState ) {
113
- let viewPos = baseState . viewPos - 1
114
- if ( viewPos < 0 ) {
115
- return baseState
112
+ export function teleport ( baseState , targetPos ) {
113
+ let viewPos = baseState . viewPos
114
+ if ( targetPos >= baseState . queueLength ) {
115
+ return goToEnd ( baseState )
116
116
}
117
+ let diff = targetPos - viewPos
118
+ let direction = Math . sign ( diff )
117
119
let moves = baseState . moves
118
- let move = moves [ viewPos ]
119
- let baseBoard = unApply ( baseState . baseBoard , move )
120
+ let baseBoard = baseState . baseBoard
121
+ let action = diff < 0 ? unApply : updateBoard
122
+ for ( let i = 0 ; i < Math . abs ( diff ) ; i ++ ) {
123
+ let [ , updated ] = action ( baseBoard , diff < 0 ? moves [ viewPos - 1 ] : moves [ viewPos ] )
124
+ baseBoard = updated
125
+ viewPos += direction
126
+ }
120
127
let historyBoard = baseState . historyBoard
121
128
return produce ( baseState , ( draft ) => {
122
129
draft . baseBoard = baseBoard
@@ -131,21 +138,14 @@ export function moveBack(baseState) {
131
138
} )
132
139
}
133
140
141
+ export function moveBack ( baseState ) {
142
+ let viewPos = baseState . viewPos
143
+ return teleport ( baseState , viewPos - 1 )
144
+ }
145
+
134
146
export function moveForward ( baseState ) {
135
147
let viewPos = baseState . viewPos
136
- if ( viewPos - baseState . queueLength >= 0 ) {
137
- return goToEnd ( baseState )
138
- }
139
- let moves = baseState . moves
140
- let move = moves [ viewPos ]
141
- let [ , updated ] = updateBoard ( baseState . baseBoard , move )
142
- let historyBoard = baseState . historyBoard
143
- return produce ( baseState , ( draft ) => {
144
- draft . baseBoard = updated
145
- draft . board = cheapRehydrate ( updated , historyBoard )
146
- draft . viewPos = viewPos + 1
147
- draft . lastMove = move . action === "pass" ? undefined : move
148
- } )
148
+ return teleport ( baseState , viewPos + 1 )
149
149
}
150
150
151
151
function goToEnd ( baseState ) {
@@ -161,6 +161,7 @@ function goToEnd(baseState) {
161
161
}
162
162
return produce ( baseState , ( draft ) => {
163
163
draft . board = rehydrate ( baseBoard , historyBoard )
164
+ draft . viewPos = baseState . queueLength
164
165
} )
165
166
}
166
167
@@ -320,7 +321,7 @@ function unApply(board, move) {
320
321
result [ y ] [ x ] = ( move . color ^ COLORS )
321
322
} )
322
323
}
323
- return result
324
+ return [ 1 , result ]
324
325
}
325
326
326
327
function cheapRehydrate ( baseBoard , historyBoard ) {
0 commit comments