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 },