vue3-yuanma/packages/runtime-core/src/compat/instance.ts
2021-04-20 09:25:12 -04:00

104 lines
2.9 KiB
TypeScript

import { extend, NOOP, toDisplayString, toNumber } from '@vue/shared'
import { PublicPropertiesMap } from '../componentPublicInstance'
import { getCompatChildren } from './instanceChildren'
import {
DeprecationTypes,
assertCompatEnabled,
checkCompatEnabled,
isCompatEnabled
} from './compatConfig'
import { off, on, once } from './instanceEventEmitter'
import { getCompatListeners } from './instanceListeners'
import { shallowReadonly } from '@vue/reactivity'
import { legacySlotProxyHandlers } from './component'
import { compatH } from './renderFn'
import {
legacyBindObjectProps,
legacyRenderSlot,
legacyRenderStatic
} from './renderHelpers'
import { createCommentVNode, createTextVNode } from '../vnode'
import { renderList } from '../helpers/renderList'
export function installCompatInstanceProperties(map: PublicPropertiesMap) {
const set = (target: any, key: any, val: any) => {
target[key] = val
}
const del = (target: any, key: any) => {
delete target[key]
}
extend(map, {
$set: i => {
assertCompatEnabled(DeprecationTypes.INSTANCE_SET, i)
return set
},
$delete: i => {
assertCompatEnabled(DeprecationTypes.INSTANCE_DELETE, i)
return del
},
$mount: i => {
assertCompatEnabled(
DeprecationTypes.GLOBAL_MOUNT,
null /* this warning is global */
)
// root mount override from ./global.ts in installCompatMount
return i.ctx._compat_mount || NOOP
},
$destroy: i => {
assertCompatEnabled(DeprecationTypes.INSTANCE_DESTROY, i)
// root destroy override from ./global.ts in installCompatMount
return i.ctx._compat_destroy || NOOP
},
// overrides existing accessor
$slots: i => {
if (
isCompatEnabled(DeprecationTypes.RENDER_FUNCTION, i) &&
i.render &&
i.render._compatWrapped
) {
return new Proxy(i.slots, legacySlotProxyHandlers)
}
return __DEV__ ? shallowReadonly(i.slots) : i.slots
},
$scopedSlots: i => {
assertCompatEnabled(DeprecationTypes.INSTANCE_SCOPED_SLOTS, i)
return __DEV__ ? shallowReadonly(i.slots) : i.slots
},
// overrides existing accessor
$attrs: i => {
if (__DEV__ && i.type.inheritAttrs === false) {
checkCompatEnabled(DeprecationTypes.INSTANCE_ATTRS_CLASS_STYLE, i)
}
return __DEV__ ? shallowReadonly(i.attrs) : i.attrs
},
$on: i => on.bind(null, i),
$once: i => once.bind(null, i),
$off: i => off.bind(null, i),
$children: getCompatChildren,
$listeners: getCompatListeners,
// v2 render helpers
$createElement: () => compatH,
_self: i => i.proxy,
_c: () => compatH,
_n: () => toNumber,
_s: () => toDisplayString,
_l: () => renderList,
_t: i => legacyRenderSlot.bind(null, i),
_b: () => legacyBindObjectProps,
_e: () => createCommentVNode,
_v: () => createTextVNode,
_m: i => legacyRenderStatic.bind(null, i)
} as PublicPropertiesMap)
}