@@ -11,16 +11,12 @@ import {
1111 remove ,
1212} from './block'
1313import {
14- type GenericComponentInstance ,
1514 type TransitionHooks ,
1615 type VNode ,
17- currentInstance ,
18- isKeepAlive ,
1916 queuePostFlushCb ,
2017} from '@vue/runtime-dom'
2118import type { VaporComponentInstance } from './component'
2219import type { NodeRef } from './apiTemplateRef'
23- import type { KeepAliveInstance } from './components/KeepAlive'
2420import {
2521 applyTransitionHooks ,
2622 applyTransitionLeaveHooks ,
@@ -33,6 +29,12 @@ import {
3329 locateHydrationNode ,
3430} from './dom/hydration'
3531
32+ export interface FragmentHooks {
33+ getScope ( key : any ) : EffectScope | undefined
34+ beforeUpdate ( oldKey : any , newKey : any ) : void
35+ afterUpdate ( newKey : any , nodes : Block , scope : EffectScope ) : void
36+ }
37+
3638export class VaporFragment < T extends Block = Block >
3739 implements TransitionOptions
3840{
@@ -73,8 +75,7 @@ export class DynamicFragment extends VaporFragment {
7375 current ?: BlockFn
7476 fallback ?: BlockFn
7577 anchorLabel ?: string
76- inKeepAlive ?: boolean
77- keptAliveScopes ?: Map < any , EffectScope >
78+ hooks ?: FragmentHooks
7879
7980 constructor ( anchorLabel ?: string ) {
8081 super ( [ ] )
@@ -97,21 +98,17 @@ export class DynamicFragment extends VaporFragment {
9798 const prevSub = setActiveSub ( )
9899 const parent = isHydrating ? null : this . anchor . parentNode
99100 const transition = this . $transition
100- const instance = currentInstance !
101- this . inKeepAlive = isKeepAlive ( instance )
102101 // teardown previous branch
103102 if ( this . scope ) {
104- if ( this . inKeepAlive ) {
105- ; ( instance as KeepAliveInstance ) . processFragment ( this )
106- if ( ! this . keptAliveScopes ) this . keptAliveScopes = new Map ( )
107- this . keptAliveScopes . set ( this . current , this . scope )
103+ if ( this . hooks ) {
104+ this . hooks . beforeUpdate ( this . current , key )
108105 } else {
109106 this . scope . stop ( )
110107 }
111108 const mode = transition && transition . mode
112109 if ( mode ) {
113110 applyTransitionLeaveHooks ( this . nodes , transition , ( ) =>
114- this . render ( render , instance , transition , parent ) ,
111+ this . render ( render , transition , parent ) ,
115112 )
116113 parent && remove ( this . nodes , parent )
117114 if ( mode === 'out-in' ) {
@@ -123,7 +120,7 @@ export class DynamicFragment extends VaporFragment {
123120 }
124121 }
125122
126- this . render ( render , instance , transition , parent )
123+ this . render ( render , transition , parent )
127124
128125 if ( this . fallback ) {
129126 // set fallback for nested fragments
@@ -155,27 +152,22 @@ export class DynamicFragment extends VaporFragment {
155152
156153 private render (
157154 render : BlockFn | undefined ,
158- instance : GenericComponentInstance ,
159155 transition : VaporTransitionHooks | undefined ,
160156 parent : ParentNode | null ,
161157 ) {
162158 if ( render ) {
163159 // 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
160+ const scope = this . hooks && this . hooks . getScope ( this . current )
168161 if ( scope ) {
169162 this . scope = scope
170- this . keptAliveScopes ! . delete ( this . current ! )
171- this . scope . resume ( )
172163 } else {
173164 this . scope = new EffectScope ( )
174165 }
175166
176167 this . nodes = this . scope . run ( render ) || [ ]
177- if ( this . inKeepAlive ) {
178- ; ( instance as KeepAliveInstance ) . cacheFragment ( this )
168+
169+ if ( this . hooks ) {
170+ this . hooks . afterUpdate ( this . current , this . nodes , this . scope )
179171 }
180172 if ( transition ) {
181173 this . $transition = applyTransitionHooks ( this . nodes , transition )
@@ -287,3 +279,9 @@ function findInvalidFragment(fragment: VaporFragment): VaporFragment | null {
287279export function isFragment ( val : NonNullable < unknown > ) : val is VaporFragment {
288280 return val instanceof VaporFragment
289281}
282+
283+ export function isDynamicFragment (
284+ val : NonNullable < unknown > ,
285+ ) : val is DynamicFragment {
286+ return val instanceof DynamicFragment
287+ }
0 commit comments