Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(runtime-vapor): add beforeUpdate hook for custom directive #66

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 22 additions & 7 deletions packages/runtime-vapor/src/directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export type DirectiveHookName =
| 'created'
| 'beforeMount'
| 'mounted'
// | 'beforeUpdate'
| 'beforeUpdate'
| 'updated'
| 'beforeUnmount'
| 'unmounted'
Expand Down Expand Up @@ -95,10 +95,21 @@ export function withDirectives<T extends Node>(

callDirectiveHook(node, binding, 'created')

effect(() => {
if (!instance.isMountedRef.value) return
callDirectiveHook(node, binding, 'updated')
})
effect(
() => {
if (!instance.isMountedRef.value) return
callDirectiveHook(node, binding, 'beforeUpdate')
},
{ flush: 'pre' },
)

effect(
() => {
if (!instance.isMountedRef.value) return
callDirectiveHook(node, binding, 'updated')
},
{ flush: 'post' },
)
}

return node
Expand Down Expand Up @@ -129,9 +140,13 @@ function callDirectiveHook(
if (!hook) return

const newValue = binding.source ? binding.source() : undefined
if (name === 'updated' && binding.value === newValue) return
if (
['updated', 'beforeUpdate'].includes(name) &&
newValue === binding.oldValue
)
return

binding.oldValue = binding.value
binding.value = newValue
hook(node, binding)
if (name !== 'beforeUpdate') binding.oldValue = binding.value
}
44 changes: 34 additions & 10 deletions packages/runtime-vapor/src/scheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,53 @@ import { ReactiveEffect } from '@vue/reactivity'

const p = Promise.resolve()

let queued: any[] | undefined
type Queued = { value?: any[] }
let preQueued: Queued = {}
let renderQueued: Queued = {}
let postQueued: Queued = {}

function queue(fn: any) {
if (!queued) {
queued = [fn]
function queue(fn: any, queued: Queued) {
if (!queued.value) {
queued.value = [fn]
p.then(flush)
} else {
queued.push(fn)
queued.value.push(fn)
}
}

function flush() {
for (let i = 0; i < queued!.length; i++) {
queued![i]()
flushAQueued(preQueued)
flushAQueued(renderQueued)
flushAQueued(postQueued)
}

function flushAQueued(queued: Queued) {
if (queued.value) {
for (let i = 0; i < queued.value.length; i++) {
queued.value[i]()
}
}
queued = undefined
queued.value = undefined
}

export const nextTick = (fn?: any) => (fn ? p.then(fn) : p)

export function effect(fn: any) {
export type EffectOptions = {
flush?: 'pre' | 'post' | 'render'
}

export function effect(fn: any, options?: EffectOptions) {
let run: () => void
const e = new ReactiveEffect(fn, () => queue(run))

const flushMode = options?.flush
const queued =
flushMode === 'pre'
? preQueued
: flushMode === 'post'
? postQueued
: renderQueued // default

const e = new ReactiveEffect(fn, () => queue(run, queued))
run = e.run.bind(e)
run()
}