]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
feat: applyDirective
authorEvan You <yyx990803@gmail.com>
Tue, 25 Sep 2018 18:56:31 +0000 (14:56 -0400)
committerEvan You <yyx990803@gmail.com>
Tue, 25 Sep 2018 18:56:31 +0000 (14:56 -0400)
packages/core/src/component.ts
packages/core/src/createRenderer.ts
packages/core/src/directive.ts [new file with mode: 0644]
packages/core/src/index.ts

index 34ef7e51335301df69213dad8c355cbe88868c57..cf5d7c5cf6512f896befddd5842e25761cab8612 100644 (file)
@@ -48,8 +48,8 @@ export interface MountedComponent<D = Data, P = Data> extends Component {
   mounted?(): void
   beforeUpdate?(vnode: VNode): void
   updated?(vnode: VNode): void
-  beforeDestroy?(): void
-  destroyed?(): void
+  beforeUnmount?(): void
+  unmounted?(): void
   errorCaptured?(): (err: Error, type: ErrorTypes) => boolean | void
 
   _updateHandle: Autorun
index b6ff8dfd84bde7af47fa9c6bb0d52b81509db9dd..7ddd730df5f5947a6981083749d9454ba84b577e 100644 (file)
@@ -1270,8 +1270,8 @@ export function createRenderer(options: RendererOptions) {
   }
 
   function unmountComponentInstance(instance: MountedComponent) {
-    if (instance.beforeDestroy) {
-      instance.beforeDestroy.call(instance.$proxy)
+    if (instance.beforeUnmount) {
+      instance.beforeUnmount.call(instance.$proxy)
     }
     if (instance.$vnode) {
       unmount(instance.$vnode)
@@ -1279,8 +1279,8 @@ export function createRenderer(options: RendererOptions) {
     stop(instance._updateHandle)
     teardownComponentInstance(instance)
     instance._destroyed = true
-    if (instance.destroyed) {
-      instance.destroyed.call(instance.$proxy)
+    if (instance.unmounted) {
+      instance.unmounted.call(instance.$proxy)
     }
   }
 
diff --git a/packages/core/src/directive.ts b/packages/core/src/directive.ts
new file mode 100644 (file)
index 0000000..5c0cdce
--- /dev/null
@@ -0,0 +1,58 @@
+import { VNode } from './vdom'
+import { MountedComponent } from './component'
+
+export interface DirectiveBinding {
+  instance: MountedComponent
+  value?: any
+  arg?: string
+  modifiers?: DirectiveModifiers
+}
+
+export type DirectiveHook = (
+  el: any,
+  binding: DirectiveBinding,
+  vnode: VNode,
+  prevVNode: VNode | void
+) => void
+
+export interface Directive {
+  beforeMount: DirectiveHook
+  mounted: DirectiveHook
+  beforeUpdate: DirectiveHook
+  updated: DirectiveHook
+  beforeUnmount: DirectiveHook
+  unmounted: DirectiveHook
+}
+
+type DirectiveModifiers = Record<string, boolean>
+
+export function applyDirective(
+  vnode: VNode,
+  directive: Directive,
+  instance: MountedComponent,
+  value?: any,
+  arg?: string,
+  modifiers?: DirectiveModifiers
+): VNode {
+  const data = vnode.data || (vnode.data = {})
+  for (const key in directive) {
+    const hook = directive[key as keyof Directive]
+    const hookKey = `vnode` + key[0].toUpperCase() + key.slice(1)
+    const vnodeHook = (vnode: VNode, prevVNode?: VNode) => {
+      hook(
+        vnode.el,
+        {
+          instance,
+          value,
+          arg,
+          modifiers
+        },
+        vnode,
+        prevVNode
+      )
+    }
+    const existing = data[hookKey]
+    data[hookKey] = existing ? [].concat(existing, vnodeHook as any) : vnodeHook
+  }
+  return vnode
+}
index e838d8063a099ebe7ca2260f9a2b76d48af2b338..d2312b93d35f46c89e209b1e8b4717b9f9fcf260 100644 (file)
@@ -15,6 +15,7 @@ export { nextTick } from '@vue/scheduler'
 
 // internal api
 export { createComponentInstance } from './componentUtils'
+export { applyDirective } from './directive'
 
 // flags & types
 export { ComponentClass, FunctionalComponent } from './component'