From 1def00e96e399d49e282cee0fa6291404d90f456 Mon Sep 17 00:00:00 2001 From: Evan You Date: Tue, 25 Sep 2018 20:37:46 -0400 Subject: [PATCH] feat: oldValue for directives --- packages/core/src/optional/directive.ts | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/packages/core/src/optional/directive.ts b/packages/core/src/optional/directive.ts index aeab07bb..8867e446 100644 --- a/packages/core/src/optional/directive.ts +++ b/packages/core/src/optional/directive.ts @@ -4,6 +4,7 @@ import { MountedComponent } from '../component' export interface DirectiveBinding { instance: MountedComponent value?: any + oldValue?: any arg?: string modifiers?: DirectiveModifiers } @@ -26,6 +27,8 @@ export interface Directive { export type DirectiveModifiers = Record +const valueCache = new WeakMap>() + export function applyDirective( vnode: VNode, directive: Directive, @@ -35,15 +38,27 @@ export function applyDirective( modifiers?: DirectiveModifiers ): VNode { const data = vnode.data || (vnode.data = {}) + let valueCacheForDir = valueCache.get(directive) as WeakMap + if (!valueCacheForDir) { + valueCacheForDir = new WeakMap() + valueCache.set(directive, valueCacheForDir) + } 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) => { + let oldValue + if (prevVNode !== void 0) { + oldValue = valueCacheForDir.get(prevVNode) + valueCacheForDir.delete(prevVNode) + } + valueCacheForDir.set(vnode, value) hook( vnode.el, { instance, value, + oldValue, arg, modifiers },