Skip to content

Commit

Permalink
feat: use postFlush instead of promise
Browse files Browse the repository at this point in the history
  • Loading branch information
Doctor-wu committed Feb 25, 2024
1 parent 2faa221 commit cc7df9a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
9 changes: 9 additions & 0 deletions packages/runtime-core/src/directives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ export type DirectiveHook<T = any, Prev = VNode<any, T> | null, V = any> = (
binding: DirectiveBinding<V>,
vnode: VNode<any, T>,
prevVNode: Prev,
// register a callback witch will be called
// after all dirHooks have been executed
postFlushDirs?: (fn: () => void) => void,
) => void

export type SSRDirectiveHook = (
Expand Down Expand Up @@ -129,6 +132,7 @@ export function invokeDirectiveHook(
) {
const bindings = vnode.dirs!
const oldBindings = prevVNode && prevVNode.dirs!
const postFlushDirsQueue: (() => void)[] = []
for (let i = 0; i < bindings.length; i++) {
const binding = bindings[i]
if (oldBindings) {
Expand All @@ -147,8 +151,13 @@ export function invokeDirectiveHook(
binding,
vnode,
prevVNode,
(fn: () => void) => {
postFlushDirsQueue.push(fn)
},
])
resetTracking()
}
}
postFlushDirsQueue.forEach(postDir => postDir())
postFlushDirsQueue.length = 0
}
22 changes: 9 additions & 13 deletions packages/runtime-dom/src/directives/vShow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ interface VShowElement extends HTMLElement {
[vShowOriginalDisplay]: string
}

const resolvedPromise = Promise.resolve()

export const vShow: ObjectDirective<VShowElement> & { name?: 'show' } = {
beforeMount(el, { value }, { transition }) {
el[vShowOriginalDisplay] =
Expand All @@ -24,17 +22,19 @@ export const vShow: ObjectDirective<VShowElement> & { name?: 'show' } = {
transition.enter(el)
}
},
updated(el, { value, oldValue }, { transition }) {
updated(el, { value, oldValue }, { transition }, _, postFlush) {
if (
!value === !oldValue &&
(el.style.display === el[vShowOriginalDisplay] || (!value && transition))
)
return
if (transition) {
if (value) {
transition.beforeEnter(el)
setDisplay(el, true)
transition.enter(el)
postFlush!(() => {
transition.beforeEnter(el)
setDisplay(el, true)
transition.enter(el)
})
} else {
transition.leave(el, () => {
setDisplay(el, false)
Expand All @@ -44,7 +44,9 @@ export const vShow: ObjectDirective<VShowElement> & { name?: 'show' } = {
// #10038
// when multi vShow be applied to the same element
// and the sync setDisplay will impact other vShow
postSetDisplay(el, value)
postFlush!(() => {
setDisplay(el, value)
})
}
},
beforeUnmount(el, { value }) {
Expand All @@ -60,12 +62,6 @@ function setDisplay(el: VShowElement, value: unknown): void {
el.style.display = value ? el[vShowOriginalDisplay] : 'none'
}

function postSetDisplay(el: VShowElement, value: unknown): void {
resolvedPromise.then(() => {
setDisplay(el, value)
})
}

// SSR vnode transforms, only used when user includes client-oriented render
// function in SSR
export function initVShowForSSR() {
Expand Down

0 comments on commit cc7df9a

Please sign in to comment.