|
1 |
| -import { range } from 'lodash' |
| 1 | +import { last, range } from 'lodash' |
2 | 2 |
|
3 | 3 | import { Component, Status } from '../../component'
|
4 | 4 | import { requestAnimationFrameMaybe } from '../rAF'
|
@@ -47,6 +47,7 @@ export interface FlipIterator<T>
|
47 | 47 | // with the remainder of the interface.
|
48 | 48 | fastForward: (targetStack: String[], spliceLevel?: number) => Promise<void>
|
49 | 49 | peek: () => stackSummary
|
| 50 | + tempSplice: (level: number, input: IteratorInput) => Promise<any> |
50 | 51 | }
|
51 | 52 |
|
52 | 53 | export class FlipIterable {
|
@@ -76,6 +77,7 @@ export class FlipIterable {
|
76 | 77 | const sliceIterator = this.timelineIterable[Symbol.asyncIterator]()
|
77 | 78 | let currentStack: Component[] = []
|
78 | 79 | let lockPromises: Promise<any>[] = []
|
| 80 | + let restartLeaf = false |
79 | 81 |
|
80 | 82 | return {
|
81 | 83 | initialize: async () => {
|
@@ -112,23 +114,21 @@ export class FlipIterable {
|
112 | 114 | // the current and future stacks
|
113 | 115 | ;({ incoming, outgoing } = resolveFlip(currentStack, nextStack))
|
114 | 116 |
|
115 |
| - // End all outgoing components, starting from the leaf |
116 |
| - for (const c of [...outgoing].reverse()) { |
117 |
| - try { |
118 |
| - c.internals.emitter.off( |
119 |
| - EventName.endUncontrolled, |
120 |
| - triggerContinue, |
121 |
| - ) |
122 |
| - currentStack.pop() |
123 |
| - cancelled.push(c) |
124 |
| - await c.end(flipData.reason, { ...flipData, controlled: true }) |
125 |
| - context = c.leaveContext(context) |
126 |
| - } catch (error) { |
127 |
| - console.error(`Error ending`, c) |
128 |
| - throw error |
129 |
| - } |
| 117 | + if (restartLeaf && currentStack.length > 0) { |
| 118 | + //@ts-ignore |
| 119 | + incoming.unshift(currentStack.pop()) |
| 120 | + restartLeaf = false |
130 | 121 | }
|
131 | 122 |
|
| 123 | + // End all outgoing components, starting from the leaf |
| 124 | + context = await stopOutgoing( |
| 125 | + outgoing, |
| 126 | + currentStack, |
| 127 | + cancelled, |
| 128 | + flipData, |
| 129 | + context, |
| 130 | + ) |
| 131 | + |
132 | 132 | // Start all incoming components, starting from the top of the stack
|
133 | 133 | for (const c of incoming) {
|
134 | 134 | try {
|
@@ -193,6 +193,34 @@ export class FlipIterable {
|
193 | 193 | },
|
194 | 194 | }
|
195 | 195 | },
|
| 196 | + tempSplice: async ( |
| 197 | + level: number, |
| 198 | + [flipData, context]: [any, object] = [{}, {}], |
| 199 | + ) => { |
| 200 | + const oldStack = currentStack |
| 201 | + const newStack = currentStack.slice(0, level) |
| 202 | + |
| 203 | + // There are no incoming components in this flip |
| 204 | + const { outgoing } = resolveFlip(oldStack, newStack) |
| 205 | + |
| 206 | + // Stop outgoing components |
| 207 | + const newContext = await stopOutgoing( |
| 208 | + outgoing, |
| 209 | + currentStack, |
| 210 | + [], |
| 211 | + flipData, |
| 212 | + context, |
| 213 | + ) |
| 214 | + |
| 215 | + // Reset nested slice iterator |
| 216 | + sliceIterator.splice(level) |
| 217 | + |
| 218 | + return newContext |
| 219 | + }, |
| 220 | + //@ts-ignore |
| 221 | + restartLeaf: () => { |
| 222 | + restartLeaf = true |
| 223 | + }, |
196 | 224 | splice: sliceIterator.splice,
|
197 | 225 | findSplice: sliceIterator.findSplice,
|
198 | 226 | reset: sliceIterator.reset,
|
@@ -228,3 +256,25 @@ export class FlipIterable {
|
228 | 256 | }
|
229 | 257 | }
|
230 | 258 | }
|
| 259 | + |
| 260 | +const stopOutgoing = async function ( |
| 261 | + outgoing: Component[], |
| 262 | + currentStack: Component[], |
| 263 | + cancelled: Component[], |
| 264 | + flipData: any, |
| 265 | + context: object, |
| 266 | +) { |
| 267 | + for (const c of [...outgoing].reverse()) { |
| 268 | + try { |
| 269 | + c.internals.emitter.off(EventName.endUncontrolled, triggerContinue) |
| 270 | + currentStack.pop() |
| 271 | + cancelled.push(c) |
| 272 | + await c.end(flipData.reason, { ...flipData, controlled: true }) |
| 273 | + context = c.leaveContext(context) |
| 274 | + } catch (error) { |
| 275 | + console.error(`Error ending`, c) |
| 276 | + throw error |
| 277 | + } |
| 278 | + } |
| 279 | + return context |
| 280 | +} |
0 commit comments