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

refactor(runtime-vapor): simplify directive mechanism #278

Merged
merged 6 commits into from
Nov 13, 2024
Merged
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
2 changes: 1 addition & 1 deletion packages/runtime-vapor/src/apiCreateFor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ function getItem(
} else if (typeof source === 'number') {
return [idx + 1, idx, undefined]
} else if (isObject(source)) {
if (source && source[Symbol.iterator as any]) {
if (source[Symbol.iterator as any]) {
source = Array.from(source as Iterable<any>)
return [source[idx], idx, undefined]
} else {
Expand Down
6 changes: 2 additions & 4 deletions packages/runtime-vapor/src/apiRender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,15 +128,14 @@ function mountComponent(
}

// hook: beforeMount
invokeLifecycle(instance, VaporLifecycleHooks.BEFORE_MOUNT, 'beforeMount')
invokeLifecycle(instance, VaporLifecycleHooks.BEFORE_MOUNT)

insert(instance.block!, instance.container)

// hook: mounted
invokeLifecycle(
instance,
VaporLifecycleHooks.MOUNTED,
'mounted',
instance => (instance.isMounted = true),
true,
)
Expand All @@ -156,7 +155,7 @@ export function unmountComponent(instance: ComponentInternalInstance): void {
const { container, scope } = instance

// hook: beforeUnmount
invokeLifecycle(instance, VaporLifecycleHooks.BEFORE_UNMOUNT, 'beforeUnmount')
invokeLifecycle(instance, VaporLifecycleHooks.BEFORE_UNMOUNT)

scope.stop()
container.textContent = ''
Expand All @@ -165,7 +164,6 @@ export function unmountComponent(instance: ComponentInternalInstance): void {
invokeLifecycle(
instance,
VaporLifecycleHooks.UNMOUNTED,
'unmounted',
instance => queuePostFlushCb(() => (instance.isUnmounted = true)),
true,
)
Expand Down
6 changes: 1 addition & 5 deletions packages/runtime-vapor/src/componentLifecycle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ import { invokeArrayFns } from '@vue/shared'
import type { VaporLifecycleHooks } from './enums'
import { type ComponentInternalInstance, setCurrentInstance } from './component'
import { queuePostFlushCb } from './scheduler'
import type { DirectiveHookName } from './directives'

export function invokeLifecycle(
instance: ComponentInternalInstance,
lifecycle: VaporLifecycleHooks,
directive: DirectiveHookName,
cb?: (instance: ComponentInternalInstance) => void,
post?: boolean,
): void {
Expand All @@ -27,8 +25,6 @@ export function invokeLifecycle(
}

function invokeSub() {
instance.comps.forEach(comp =>
invokeLifecycle(comp, lifecycle, directive, cb, post),
)
instance.comps.forEach(comp => invokeLifecycle(comp, lifecycle, cb, post))
}
}
101 changes: 62 additions & 39 deletions packages/runtime-vapor/src/directives.ts
Original file line number Diff line number Diff line change
@@ -1,54 +1,30 @@
import { isBuiltInDirective } from '@vue/shared'
import { type ComponentInternalInstance, currentInstance } from './component'
import {
type ComponentInternalInstance,
currentInstance,
isVaporComponent,
} from './component'
import { warn } from './warning'
import { normalizeBlock } from './dom/element'
import { getCurrentScope } from '@vue/reactivity'
import { VaporErrorCodes, callWithAsyncErrorHandling } from './errorHandling'

export type DirectiveModifiers<M extends string = string> = Record<M, boolean>

export interface DirectiveBinding<T = any, V = any, M extends string = string> {
instance: ComponentInternalInstance
source?: () => V
value: V
oldValue: V | null
source: () => V
arg?: string
modifiers?: DirectiveModifiers<M>
dir: ObjectDirective<T, V, M>
dir: Directive<T, V, M>
}

export type DirectiveBindingsMap = Map<Node, DirectiveBinding[]>

export type DirectiveHook<
T = any | null,
V = any,
M extends string = string,
> = (node: T, binding: DirectiveBinding<T, V, M>) => void

// create node -> `created` -> node operation -> `beforeMount` -> node mounted -> `mounted`
// effect update -> `beforeUpdate` -> node updated -> `updated`
// `beforeUnmount`-> node unmount -> `unmounted`
export type DirectiveHookName =
| 'created'
| 'beforeMount'
| 'mounted'
| 'beforeUpdate'
| 'updated'
| 'beforeUnmount'
| 'unmounted'
export type ObjectDirective<T = any, V = any, M extends string = string> = {
[K in DirectiveHookName]?: DirectiveHook<T, V, M> | undefined
} & {
/** Watch value deeply */
deep?: boolean | number
}

export type FunctionDirective<
T = any,
V = any,
M extends string = string,
> = DirectiveHook<T, V, M>

export type Directive<T = any, V = any, M extends string = string> =
| ObjectDirective<T, V, M>
| FunctionDirective<T, V, M>
export type Directive<T = any, V = any, M extends string = string> = (
node: T,
binding: DirectiveBinding<T, V, M>,
) => void

export function validateDirectiveName(name: string): void {
if (isBuiltInDirective(name)) {
Expand Down Expand Up @@ -77,7 +53,54 @@ export function withDirectives<T extends ComponentInternalInstance | Node>(
return nodeOrComponent
}

// NOOP
let node: Node
if (isVaporComponent(nodeOrComponent)) {
const root = getComponentNode(nodeOrComponent)
if (!root) return nodeOrComponent
node = root
} else {
node = nodeOrComponent
}

const instance = currentInstance!
const parentScope = getCurrentScope()

if (__DEV__ && !parentScope) {
warn(`Directives should be used inside of RenderEffectScope.`)
}

for (const directive of directives) {
let [dir, source = () => undefined, arg, modifiers] = directive
if (!dir) continue

const binding: DirectiveBinding = {
dir,
source,
instance,
arg,
modifiers,
}

callWithAsyncErrorHandling(dir, instance, VaporErrorCodes.DIRECTIVE_HOOK, [
node,
binding,
])
}

return nodeOrComponent
}

function getComponentNode(component: ComponentInternalInstance) {
if (!component.block) return

const nodes = normalizeBlock(component.block)
if (nodes.length !== 1) {
warn(
`Runtime directive used on component with non-element root node. ` +
`The directives will not function as intended.`,
)
return
}

return nodes[0]
}
Loading