wip: switch to new implementation

This commit is contained in:
Evan You
2019-05-25 23:51:20 +08:00
parent 35effdee5a
commit 3cded86b98
24 changed files with 311 additions and 4412 deletions

View File

@@ -1,96 +0,0 @@
import { ComponentInstance } from './component'
import { isFunction } from '@vue/shared'
import { isRendering } from './componentRenderUtils'
import { isReservedKey, reservedMethods } from './componentOptions'
import { warn } from './warning'
const bindCache = new WeakMap()
// TODO: bound methods should also capture/handle errors
function getBoundMethod(fn: Function, target: any, receiver: any): Function {
let boundMethodsForTarget = bindCache.get(target)
if (boundMethodsForTarget === void 0) {
bindCache.set(target, (boundMethodsForTarget = new Map()))
}
let boundFn = boundMethodsForTarget.get(fn)
if (boundFn === void 0) {
boundMethodsForTarget.set(fn, (boundFn = fn.bind(receiver)))
}
return boundFn
}
const renderProxyHandlers = {
get(target: ComponentInstance<any, any>, key: string, receiver: any) {
let i: any
if (key === '_self') {
return target
} else if ((i = target._rawData) !== null && i.hasOwnProperty(key)) {
// data
// make sure to return from $data to register dependency
return target.$data[key]
} else if ((i = target.$options.props) != null && i.hasOwnProperty(key)) {
// props are only proxied if declared
return target.$props[key]
} else if (
(i = target._computedGetters) !== null &&
i.hasOwnProperty(key)
) {
// computed
return i[key]()
} else if (key[0] !== '_') {
if (__DEV__ && isRendering) {
if (key in reservedMethods) {
warn(
`"${key}" is a reserved method / lifecycle hook and should not be ` +
`used as a normal method during render.`
)
} else if (!(key in target)) {
warn(
`property "${key}" was accessed during render but does not exist ` +
`on instance.`
)
}
}
const value = Reflect.get(target, key, receiver)
if (key !== 'constructor' && isFunction(value)) {
// auto bind
return getBoundMethod(value, target, receiver)
} else {
return value
}
}
},
set(
target: ComponentInstance<any, any>,
key: string,
value: any,
receiver: any
): boolean {
let i: any
if (__DEV__) {
if (isReservedKey(key) && key in target) {
warn(`failed setting property "${key}": reserved fields are immutable.`)
return false
}
if ((i = target.$options.props) != null && i.hasOwnProperty(key)) {
warn(`failed setting property "${key}": props are immutable.`)
return false
}
}
if ((i = target._rawData) !== null && i.hasOwnProperty(key)) {
target.$data[key] = value
return true
} else {
return Reflect.set(target, key, value, receiver)
}
}
}
export type ComponentProxy<T = ComponentInstance> = T & { _self: T }
export function createRenderProxy<T extends ComponentInstance>(
instance: T
): ComponentProxy<T> {
debugger
return new Proxy(instance, renderProxyHandlers) as any
}