Skip to content

Commit 6e44da6

Browse files
committed
wip: tweaks
1 parent 050ca87 commit 6e44da6

File tree

2 files changed

+24
-15
lines changed

2 files changed

+24
-15
lines changed

packages/runtime-vapor/src/block.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -149,13 +149,10 @@ export function remove(block: Block, parent?: ParentNode): void {
149149
if (block.anchor) remove(block.anchor, parent)
150150
if ((block as DynamicFragment).scope) {
151151
;(block as DynamicFragment).scope!.stop()
152-
153-
const pausedScopes = (block as DynamicFragment).pausedScopes
154-
if (pausedScopes) {
155-
for (let i = 0; i < pausedScopes.length; i++) {
156-
pausedScopes[i].stop()
157-
}
158-
pausedScopes.length = 0
152+
const scopes = (block as DynamicFragment).keptAliveScopes
153+
if (scopes) {
154+
scopes.forEach(scope => scope.stop())
155+
scopes.clear()
159156
}
160157
}
161158
}

packages/runtime-vapor/src/fragment.ts

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ export class DynamicFragment extends VaporFragment {
7373
current?: BlockFn
7474
fallback?: BlockFn
7575
anchorLabel?: string
76-
pausedScopes?: EffectScope[]
76+
inKeepAlive?: boolean
77+
keptAliveScopes?: Map<any, EffectScope>
7778

7879
constructor(anchorLabel?: string) {
7980
super([])
@@ -97,14 +98,13 @@ export class DynamicFragment extends VaporFragment {
9798
const parent = isHydrating ? null : this.anchor.parentNode
9899
const transition = this.$transition
99100
const instance = currentInstance!
100-
101+
this.inKeepAlive = isKeepAlive(instance)
101102
// teardown previous branch
102103
if (this.scope) {
103-
if (isKeepAlive(instance)) {
104+
if (this.inKeepAlive) {
104105
;(instance as KeepAliveInstance).processFragment(this)
105-
// Pause the scope and store it for later cleanup
106-
this.scope.pause()
107-
;(this.pausedScopes || (this.pausedScopes = [])).push(this.scope)
106+
if (!this.keptAliveScopes) this.keptAliveScopes = new Map()
107+
this.keptAliveScopes.set(this.current, this.scope)
108108
} else {
109109
this.scope.stop()
110110
}
@@ -160,9 +160,21 @@ export class DynamicFragment extends VaporFragment {
160160
parent: ParentNode | null,
161161
) {
162162
if (render) {
163-
this.scope = new EffectScope()
163+
// For KeepAlive, try to reuse the keepAlive scope for this key
164+
const scope =
165+
this.inKeepAlive && this.keptAliveScopes
166+
? this.keptAliveScopes.get(this.current)
167+
: undefined
168+
if (scope) {
169+
this.scope = scope
170+
this.keptAliveScopes!.delete(this.current!)
171+
this.scope.resume()
172+
} else {
173+
this.scope = new EffectScope()
174+
}
175+
164176
this.nodes = this.scope.run(render) || []
165-
if (isKeepAlive(instance)) {
177+
if (this.inKeepAlive) {
166178
;(instance as KeepAliveInstance).cacheFragment(this)
167179
}
168180
if (transition) {

0 commit comments

Comments
 (0)