Skip to content

Commit e2f0745

Browse files
Revise component lifecycle
1 parent 7431ab3 commit e2f0745

File tree

3 files changed

+27
-10
lines changed

3 files changed

+27
-10
lines changed

packages/library/src/base/component.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,17 @@ export class Component {
351351
private async _reset() {
352352
this.internals.iterator?.reset()
353353
await this.#emitter.trigger(EventName.reset)
354-
this.status = Status.initialized
354+
355+
// Reset the component status if it was previously shown (and prepare if
356+
// necessary) -- if it is currently being shown, it will be torn down
357+
// via the flip logic; if it hasn't been shown yet, it will already be ready.
358+
if (this.status >= Status.done) {
359+
this.status = Status.initialized
360+
361+
if (!this.options.tardy) {
362+
await this.prepare(false)
363+
}
364+
}
355365
}
356366

357367
async reset() {

packages/library/src/base/controller.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { FlipIterable, FlipIterator } from './util/iterators/flipIterable'
2-
import { Component } from './component'
2+
import { Component, Status } from './component'
33
import { Lock } from './util/lock'
44
import { Emitter } from './util/emitter'
55

@@ -89,7 +89,10 @@ export class Controller<C extends Component = Component> extends Emitter {
8989

9090
// Flip iterator go brrr
9191
while (!done) {
92-
const output = await this.iterator.next({flipData, context: this.context})
92+
const output = await this.iterator.next({
93+
flipData,
94+
context: this.context,
95+
})
9396
const lockPromise = this.lock.acquire()
9497
done = output.done ?? true
9598
this.context = output.value.context
@@ -180,11 +183,15 @@ export class Controller<C extends Component = Component> extends Emitter {
180183

181184
// Having torn down the larger part of the current component stack,
182185
// issue a reset to the remaining part.
186+
const last_c = last(this.currentStack)
183187
//@ts-ignore
184-
await last(this.currentStack)?._reset?.()
188+
await last_c?._reset?.()
185189

186190
// Don't re-mount the root component
187-
if (this.currentStack.length > 1) {
191+
if (
192+
this.currentStack.length > 1 &&
193+
(last_c?.status ?? Status.running) < Status.running
194+
) {
188195
//@ts-ignore
189196
this.iterator?.restartLeaf()
190197
}

packages/library/src/base/util/iterators/flipIterable.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,9 @@ export class FlipIterable {
117117
// the current and future stacks
118118
;({ incoming, outgoing } = resolveFlip(currentStack, nextStack))
119119

120-
if (restartLeaf && currentStack.length > 0) {
121-
//@ts-ignore
122-
incoming.unshift(currentStack.pop())
120+
const currentLeaf = last(currentStack)
121+
if (restartLeaf && currentLeaf!.status < Status.running) {
122+
currentLeaf!.status = Status.running
123123
restartLeaf = false
124124
}
125125

@@ -181,9 +181,9 @@ export class FlipIterable {
181181
this.showFrameRequest = undefined
182182
})
183183
// Treat locks independently so they aren't cancelled
184-
window.requestAnimationFrame(t => {
184+
//window.requestAnimationFrame(t => {
185185
cancelled.map(c => c.lock?.({ timestamp: t }))
186-
})
186+
//})
187187
this.renderFrameRequest = undefined
188188
},
189189
)

0 commit comments

Comments
 (0)