Skip to content

Commit 21446ad

Browse files
committed
feat: use postFlush instead of promise
1 parent f149f2a commit 21446ad

File tree

2 files changed

+18
-13
lines changed

2 files changed

+18
-13
lines changed

packages/runtime-core/src/directives.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ export type DirectiveHook<T = any, Prev = VNode<any, T> | null, V = any> = (
4040
binding: DirectiveBinding<V>,
4141
vnode: VNode<any, T>,
4242
prevVNode: Prev,
43+
// register a callback witch will be called
44+
// after all dirHooks have been executed
45+
postFlushDirs?: (fn: () => void) => void,
4346
) => void
4447

4548
export type SSRDirectiveHook = (
@@ -129,6 +132,7 @@ export function invokeDirectiveHook(
129132
) {
130133
const bindings = vnode.dirs!
131134
const oldBindings = prevVNode && prevVNode.dirs!
135+
const postFlushDirsQueue: (() => void)[] = []
132136
for (let i = 0; i < bindings.length; i++) {
133137
const binding = bindings[i]
134138
if (oldBindings) {
@@ -147,8 +151,13 @@ export function invokeDirectiveHook(
147151
binding,
148152
vnode,
149153
prevVNode,
154+
(fn: () => void) => {
155+
postFlushDirsQueue.push(fn)
156+
},
150157
])
151158
resetTracking()
152159
}
153160
}
161+
postFlushDirsQueue.forEach(postDir => postDir())
162+
postFlushDirsQueue.length = 0
154163
}

packages/runtime-dom/src/directives/vShow.ts

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ interface VShowElement extends HTMLElement {
77
[vShowOldKey]: string
88
}
99

10-
const resolvedPromise = Promise.resolve()
11-
1210
export const vShow: ObjectDirective<VShowElement> & { name?: 'show' } = {
1311
beforeMount(el, { value }, { transition }) {
1412
el[vShowOldKey] = el.style.display === 'none' ? '' : el.style.display
@@ -23,17 +21,19 @@ export const vShow: ObjectDirective<VShowElement> & { name?: 'show' } = {
2321
transition.enter(el)
2422
}
2523
},
26-
updated(el, { value, oldValue }, { transition }) {
24+
updated(el, { value, oldValue }, { transition }, _, postFlush) {
2725
if (
2826
!value === !oldValue &&
2927
(el.style.display === el[vShowOldKey] || (!value && transition))
3028
)
3129
return
3230
if (transition) {
3331
if (value) {
34-
transition.beforeEnter(el)
35-
setDisplay(el, true)
36-
transition.enter(el)
32+
postFlush!(() => {
33+
transition.beforeEnter(el)
34+
setDisplay(el, true)
35+
transition.enter(el)
36+
})
3737
} else {
3838
transition.leave(el, () => {
3939
setDisplay(el, false)
@@ -43,7 +43,9 @@ export const vShow: ObjectDirective<VShowElement> & { name?: 'show' } = {
4343
// #10038
4444
// when multi vShow be applied to the same element
4545
// and the sync setDisplay will impact other vShow
46-
postSetDisplay(el, value)
46+
postFlush!(() => {
47+
setDisplay(el, value)
48+
})
4749
}
4850
},
4951
beforeUnmount(el, { value }) {
@@ -59,12 +61,6 @@ function setDisplay(el: VShowElement, value: unknown): void {
5961
el.style.display = value ? el[vShowOldKey] : 'none'
6062
}
6163

62-
function postSetDisplay(el: VShowElement, value: unknown): void {
63-
resolvedPromise.then(() => {
64-
setDisplay(el, value)
65-
})
66-
}
67-
6864
// SSR vnode transforms, only used when user includes client-oriented render
6965
// function in SSR
7066
export function initVShowForSSR() {

0 commit comments

Comments
 (0)