Skip to content
This repository was archived by the owner on Jul 19, 2025. It is now read-only.

Commit 7cacb65

Browse files
committed
fix(runtime-vapor): flush post jobs after unmount
1 parent af1581b commit 7cacb65

File tree

6 files changed

+32
-23
lines changed

6 files changed

+32
-23
lines changed

packages/runtime-vapor/src/apiRender.ts

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
setCurrentInstance,
66
} from './component'
77
import { insert, querySelector, remove } from './dom/element'
8-
import { flushPostFlushCbs, queuePostRenderEffect } from './scheduler'
8+
import { flushPostFlushCbs, queuePostFlushCb } from './scheduler'
99
import { invokeLifecycle } from './componentLifecycle'
1010
import { VaporLifecycleHooks } from './apiLifecycle'
1111
import {
@@ -107,14 +107,15 @@ function mountComponent(
107107
invokeLifecycle(instance, VaporLifecycleHooks.BEFORE_MOUNT, 'beforeMount')
108108

109109
insert(instance.block!, instance.container)
110-
instance.isMounted = true
111-
instance.comps.forEach(comp => {
112-
comp.isMounted = true
113-
})
114110

115111
// hook: mounted
116-
invokeLifecycle(instance, VaporLifecycleHooks.MOUNTED, 'mounted', true)
117-
112+
invokeLifecycle(
113+
instance,
114+
VaporLifecycleHooks.MOUNTED,
115+
'mounted',
116+
instance => (instance.isMounted = true),
117+
true,
118+
)
118119
return instance
119120
}
120121

@@ -128,6 +129,12 @@ export function unmountComponent(instance: ComponentInternalInstance) {
128129
block && remove(block, container)
129130

130131
// hook: unmounted
131-
invokeLifecycle(instance, VaporLifecycleHooks.UNMOUNTED, 'unmounted', true)
132-
queuePostRenderEffect(() => (instance.isUnmounted = true))
132+
invokeLifecycle(
133+
instance,
134+
VaporLifecycleHooks.UNMOUNTED,
135+
'unmounted',
136+
instance => queuePostFlushCb(() => (instance.isUnmounted = true)),
137+
true,
138+
)
139+
flushPostFlushCbs()
133140
}
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,36 @@
11
import { invokeArrayFns } from '@vue/shared'
22
import type { VaporLifecycleHooks } from './apiLifecycle'
33
import { type ComponentInternalInstance, setCurrentInstance } from './component'
4-
import { queuePostRenderEffect } from './scheduler'
4+
import { queuePostFlushCb } from './scheduler'
55
import { type DirectiveHookName, invokeDirectiveHook } from './directives'
66

77
export function invokeLifecycle(
88
instance: ComponentInternalInstance,
99
lifecycle: VaporLifecycleHooks,
1010
directive: DirectiveHookName,
11+
cb?: (instance: ComponentInternalInstance) => void,
1112
post?: boolean,
1213
) {
1314
invokeArrayFns(post ? [invokeSub, invokeCurrent] : [invokeCurrent, invokeSub])
1415

1516
function invokeCurrent() {
17+
cb && cb(instance)
1618
const hooks = instance[lifecycle]
1719
if (hooks) {
1820
const fn = () => {
1921
const reset = setCurrentInstance(instance)
2022
instance.scope.run(() => invokeArrayFns(hooks))
2123
reset()
2224
}
23-
post ? queuePostRenderEffect(fn) : fn()
25+
post ? queuePostFlushCb(fn) : fn()
2426
}
2527

2628
invokeDirectiveHook(instance, directive)
2729
}
2830

2931
function invokeSub() {
3032
instance.comps.forEach(comp =>
31-
invokeLifecycle(comp, lifecycle, directive, post),
33+
invokeLifecycle(comp, lifecycle, directive, cb, post),
3234
)
3335
}
3436
}

packages/runtime-vapor/src/dom/event.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
recordEventMetadata,
1010
} from '../componentMetadata'
1111
import { withKeys, withModifiers } from '@vue/runtime-dom'
12-
import { queuePostRenderEffect } from '../scheduler'
12+
import { queuePostFlushCb } from '../scheduler'
1313

1414
export function addEventListener(
1515
el: Element,
@@ -35,7 +35,7 @@ export function on(
3535
) {
3636
const handler: DelegatedHandler = eventHandler(handlerGetter, options)
3737
let cleanupEvent: (() => void) | undefined
38-
queuePostRenderEffect(() => {
38+
queuePostFlushCb(() => {
3939
cleanupEvent = addEventListener(el, event, handler, options)
4040
})
4141

packages/runtime-vapor/src/dom/templateRef.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {
1515
remove,
1616
} from '@vue/shared'
1717
import { warn } from '../warning'
18-
import { queuePostRenderEffect } from '../scheduler'
18+
import { queuePostFlushCb } from '../scheduler'
1919

2020
export type NodeRef = string | Ref | ((ref: Element) => void)
2121

@@ -90,10 +90,10 @@ export function setRef(el: Element, ref: NodeRef, refFor = false) {
9090
}
9191
}
9292
doSet.id = -1
93-
queuePostRenderEffect(doSet)
93+
queuePostFlushCb(doSet)
9494

9595
onScopeDispose(() => {
96-
queuePostRenderEffect(() => {
96+
queuePostFlushCb(() => {
9797
if (isArray(existing)) {
9898
remove(existing, el)
9999
} else if (_isString) {

packages/runtime-vapor/src/renderEffect.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
getCurrentInstance,
1212
setCurrentInstance,
1313
} from './component'
14-
import { queueJob, queuePostRenderEffect } from './scheduler'
14+
import { queueJob, queuePostFlushCb } from './scheduler'
1515
import { VaporErrorCodes, callWithAsyncErrorHandling } from './errorHandling'
1616
import { invokeDirectiveHook } from './directives'
1717

@@ -40,14 +40,14 @@ export function renderEffect(cb: () => void) {
4040

4141
effect.run()
4242

43-
queuePostRenderEffect(() => {
43+
queuePostFlushCb(() => {
4444
instance.isUpdating = false
4545
if (dirs) {
4646
invokeDirectiveHook(instance, 'updated')
4747
}
4848
// updated hook
4949
if (u) {
50-
queuePostRenderEffect(u)
50+
queuePostFlushCb(u)
5151
}
5252
})
5353
} else {

packages/runtime-vapor/src/scheduler.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export function queueJob(job: SchedulerJob) {
5353
}
5454
}
5555

56-
export function queuePostRenderEffect(cb: SchedulerJobs) {
56+
export function queuePostFlushCb(cb: SchedulerJobs) {
5757
if (!isArray(cb)) {
5858
if (!(cb.flags! & SchedulerJobFlags.QUEUED)) {
5959
pendingPostFlushCbs.push(cb)
@@ -216,8 +216,8 @@ export const createVaporPreScheduler: SchedulerFactory =
216216
export const createVaporPostScheduler: SchedulerFactory =
217217
instance => (job, effect, immediateFirstRun, hasCb) => {
218218
if (!immediateFirstRun) {
219-
queuePostRenderEffect(job)
219+
queuePostFlushCb(job)
220220
} else if (!hasCb) {
221-
queuePostRenderEffect(effect.run.bind(effect))
221+
queuePostFlushCb(effect.run.bind(effect))
222222
}
223223
}

0 commit comments

Comments
 (0)