@@ -50,9 +50,10 @@ import { setBlockKey } from './helpers/setKey'
5050import {
5151 type VaporKeepAliveContext ,
5252 currentKeepAliveCtx ,
53+ isKeepAliveEnabled ,
5354 setCurrentKeepAliveCtx ,
5455 withCurrentCacheKey ,
55- } from './components/KeepAlive '
56+ } from './keepAlive '
5657import {
5758 applyTransitionHooks ,
5859 applyTransitionLeaveHooks ,
@@ -94,24 +95,32 @@ export class VaporFragment<
9495 // render context
9596 readonly renderInstance : GenericComponentInstance | null = currentInstance
9697 readonly slotOwner : VaporComponentInstance | null = currentSlotOwner
97- readonly keepAliveCtx : VaporKeepAliveContext | null = currentKeepAliveCtx
98+ readonly keepAliveCtx ? : VaporKeepAliveContext | null
9899 readonly inheritedSlotBoundary : SlotBoundaryContext | null =
99100 currentSlotBoundary
100101
101102 constructor ( nodes : T ) {
102103 this . nodes = nodes
104+ if ( isKeepAliveEnabled ) {
105+ this . keepAliveCtx = currentKeepAliveCtx
106+ }
103107 }
104108
105109 protected runWithRenderCtx < R > ( fn : ( ) => R ) : R {
106110 const prevInstance = setCurrentInstance ( this . renderInstance )
107111 const prevSlotOwner = setCurrentSlotOwner ( this . slotOwner )
108- const prevKeepAliveCtx = setCurrentKeepAliveCtx ( this . keepAliveCtx )
112+ let prevKeepAliveCtx : VaporKeepAliveContext | null = null
113+ if ( isKeepAliveEnabled ) {
114+ prevKeepAliveCtx = setCurrentKeepAliveCtx ( this . keepAliveCtx || null )
115+ }
109116 const prevBoundary = setCurrentSlotBoundary ( this . inheritedSlotBoundary )
110117 try {
111118 return fn ( )
112119 } finally {
113120 setCurrentSlotBoundary ( prevBoundary )
114- setCurrentKeepAliveCtx ( prevKeepAliveCtx )
121+ if ( isKeepAliveEnabled ) {
122+ setCurrentKeepAliveCtx ( prevKeepAliveCtx )
123+ }
115124 setCurrentSlotOwner ( prevSlotOwner )
116125 setCurrentInstance ( ...prevInstance )
117126 }
@@ -213,24 +222,28 @@ export class DynamicFragment extends VaporFragment {
213222 const parent = isHydrating ? null : this . anchor . parentNode
214223 // teardown previous branch
215224 if ( this . scope ) {
216- let retainScope = false
217- const keepAliveCtx = this . keepAliveCtx
218-
219- // if keepAliveCtx exists and processShapeFlag returns a cache key,
220- // cache the scope and retain it.
221- const cacheKey = keepAliveCtx
222- ? this . keyed
223- ? withCurrentCacheKey ( this . current , ( ) =>
224- keepAliveCtx . processShapeFlag ( this . nodes ) ,
225- )
226- : keepAliveCtx . processShapeFlag ( this . nodes )
227- : false
228- if ( cacheKey !== false ) {
229- keepAliveCtx ! . cacheScope ( cacheKey , this . current , this . scope )
230- retainScope = true
231- }
225+ if ( isKeepAliveEnabled ) {
226+ let retainScope = false
227+ const keepAliveCtx = this . keepAliveCtx
228+
229+ // if keepAliveCtx exists and processShapeFlag returns a cache key,
230+ // cache the scope and retain it.
231+ if ( keepAliveCtx ) {
232+ const cacheKey = this . keyed
233+ ? withCurrentCacheKey ( this . current , ( ) =>
234+ keepAliveCtx . processShapeFlag ( this . nodes ) ,
235+ )
236+ : keepAliveCtx . processShapeFlag ( this . nodes )
237+ if ( cacheKey !== false ) {
238+ keepAliveCtx . cacheScope ( cacheKey , this . current , this . scope )
239+ retainScope = true
240+ }
241+ }
232242
233- if ( ! retainScope ) {
243+ if ( ! retainScope ) {
244+ this . scope . stop ( )
245+ }
246+ } else {
234247 this . scope . stop ( )
235248 }
236249 const mode = transition && transition . mode
@@ -309,7 +322,7 @@ export class DynamicFragment extends VaporFragment {
309322 ) : void {
310323 this . current = key
311324 if ( render ) {
312- const keepAliveCtx = this . keepAliveCtx
325+ const keepAliveCtx = isKeepAliveEnabled ? this . keepAliveCtx : null
313326 // try to reuse the kept-alive scope
314327 const scope = keepAliveCtx && keepAliveCtx . getScope ( this . current )
315328 if ( scope ) {
0 commit comments