@@ -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