Skip to content

Commit

Permalink
chore: tweek
Browse files Browse the repository at this point in the history
  • Loading branch information
yangchangtao committed Sep 27, 2024
1 parent db4c041 commit 42ac85d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
4 changes: 4 additions & 0 deletions packages/runtime-core/src/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,8 @@ export interface ComponentInternalInstance {
*/
asyncResolved: boolean

keepAliveEffct: Function[]

// lifecycle
isMounted: boolean
isUnmounted: boolean
Expand Down Expand Up @@ -670,6 +672,8 @@ export function createComponentInstance(
asyncDep: null,
asyncResolved: false,

keepAliveEffct: [],

// lifecycle hooks
// not using enums here because it results in computed properties
isMounted: false,
Expand Down
6 changes: 6 additions & 0 deletions packages/runtime-core/src/components/KeepAlive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import { devtoolsComponentAdded } from '../devtools'
import { isAsyncWrapper } from '../apiAsyncComponent'
import { isSuspense } from './Suspense'
import { LifecycleHooks } from '../enums'
import { queuePostFlushCb } from '../scheduler'

type MatchPattern = string | RegExp | (string | RegExp)[]

Expand Down Expand Up @@ -150,6 +151,11 @@ const KeepAliveImpl: ComponentOptions = {
vnode.slotScopeIds,
optimized,
)

const effects = instance.keepAliveEffct
queuePostFlushCb(effects)
instance.keepAliveEffct.length = 0

queuePostRenderEffect(() => {
instance.isDeactivated = false
if (instance.a) {
Expand Down
12 changes: 9 additions & 3 deletions packages/runtime-core/src/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1431,7 +1431,13 @@ function baseCreateRenderer(
} else {
let { next, bu, u, parent, vnode } = instance

if (checkInstanceDeactive(instance)) {
const keepAliveParent = checkInstanceDeactive(instance)
if (keepAliveParent) {
keepAliveParent.keepAliveEffct.push(() => {
if (!instance.isUnmounted) {
componentUpdateFn()
}
})
return
}

Expand Down Expand Up @@ -2549,14 +2555,14 @@ function locateNonHydratedAsyncRoot(
function checkInstanceDeactive(instance: ComponentInternalInstance | null) {
while (instance) {
if (instance.isDeactive) {
return true
return instance
}
if (isKeepAlive(instance.vnode)) {
break
}
instance = instance.parent
}
return false
return null
}

export function invalidateMount(hooks: LifecycleHook): void {
Expand Down

0 comments on commit 42ac85d

Please sign in to comment.