vue3-yuanma/packages/runtime-core/src/compat/instance.ts

156 lines
4.4 KiB
TypeScript
Raw Normal View History

2021-04-22 13:49:25 +00:00
import {
extend,
looseEqual,
looseIndexOf,
NOOP,
toDisplayString,
toNumber
} from '@vue/shared'
2021-04-27 21:34:19 +00:00
import {
ComponentPublicInstance,
PublicPropertiesMap
} from '../componentPublicInstance'
2021-04-08 14:06:12 +00:00
import { getCompatChildren } from './instanceChildren'
2021-04-10 03:51:50 +00:00
import {
DeprecationTypes,
2021-04-10 03:51:50 +00:00
assertCompatEnabled,
isCompatEnabled
} from './compatConfig'
2021-04-08 14:06:12 +00:00
import { off, on, once } from './instanceEventEmitter'
import { getCompatListeners } from './instanceListeners'
2021-04-08 14:41:25 +00:00
import { shallowReadonly } from '@vue/reactivity'
2021-04-29 19:51:37 +00:00
import { legacySlotProxyHandlers } from './componentFunctional'
2021-04-20 13:25:12 +00:00
import { compatH } from './renderFn'
2021-04-21 19:09:18 +00:00
import { createCommentVNode, createTextVNode } from '../vnode'
import { renderList } from '../helpers/renderList'
2021-04-20 13:25:12 +00:00
import {
2021-04-22 13:49:25 +00:00
legacyBindDynamicKeys,
2021-04-21 19:09:18 +00:00
legacyBindObjectListeners,
2021-04-20 13:25:12 +00:00
legacyBindObjectProps,
2021-04-21 19:09:18 +00:00
legacyCheckKeyCodes,
2021-04-22 13:49:25 +00:00
legacyMarkOnce,
legacyPrependModifier,
2021-04-20 13:25:12 +00:00
legacyRenderSlot,
2021-04-21 19:09:18 +00:00
legacyRenderStatic,
legacyresolveScopedSlots
2021-04-20 13:25:12 +00:00
} from './renderHelpers'
2021-04-22 13:49:25 +00:00
import { resolveFilter } from '../helpers/resolveAssets'
2021-04-22 18:59:54 +00:00
import { resolveMergedOptions } from '../componentOptions'
2021-04-27 21:34:19 +00:00
import { Slots } from '../componentSlots'
export type LegacyPublicInstance = ComponentPublicInstance &
LegacyPublicProperties
export interface LegacyPublicProperties {
$set(target: object, key: string, value: any): void
$delete(target: object, key: string): void
$mount(el?: string | Element): this
$destroy(): void
$scopedSlots: Slots
$on(event: string | string[], fn: Function): this
$once(event: string, fn: Function): this
$off(event?: string | string[], fn?: Function): this
2021-04-27 21:34:19 +00:00
$children: LegacyPublicProperties[]
$listeners: Record<string, Function | Function[]>
}
2021-04-05 22:13:29 +00:00
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, {
2021-04-09 22:52:14 +00:00
$set: i => {
assertCompatEnabled(DeprecationTypes.INSTANCE_SET, i)
2021-04-05 22:13:29 +00:00
return set
},
2021-04-08 14:21:14 +00:00
2021-04-09 22:52:14 +00:00
$delete: i => {
assertCompatEnabled(DeprecationTypes.INSTANCE_DELETE, i)
2021-04-05 22:13:29 +00:00
return del
},
2021-04-08 14:21:14 +00:00
2021-04-05 22:13:29 +00:00
$mount: i => {
2021-04-09 22:52:14 +00:00
assertCompatEnabled(
DeprecationTypes.GLOBAL_MOUNT,
null /* this warning is global */
)
2021-04-05 22:13:29 +00:00
// root mount override from ./global.ts in installCompatMount
return i.ctx._compat_mount || NOOP
},
2021-04-08 14:21:14 +00:00
2021-04-05 22:13:29 +00:00
$destroy: i => {
2021-04-09 22:52:14 +00:00
assertCompatEnabled(DeprecationTypes.INSTANCE_DESTROY, i)
2021-04-05 22:13:29 +00:00
// root destroy override from ./global.ts in installCompatMount
return i.ctx._compat_destroy || NOOP
2021-04-06 19:58:12 +00:00
},
2021-04-08 14:21:14 +00:00
2021-04-09 22:52:14 +00:00
// overrides existing accessor
$slots: i => {
2021-04-11 15:15:40 +00:00
if (
isCompatEnabled(DeprecationTypes.RENDER_FUNCTION, i) &&
i.render &&
i.render._compatWrapped
) {
2021-04-09 22:52:14 +00:00
return new Proxy(i.slots, legacySlotProxyHandlers)
}
2021-04-10 03:51:50 +00:00
return __DEV__ ? shallowReadonly(i.slots) : i.slots
2021-04-09 22:52:14 +00:00
},
2021-04-08 14:21:14 +00:00
$scopedSlots: i => {
2021-04-09 22:52:14 +00:00
assertCompatEnabled(DeprecationTypes.INSTANCE_SCOPED_SLOTS, i)
2021-04-08 14:41:25 +00:00
return __DEV__ ? shallowReadonly(i.slots) : i.slots
},
2021-04-06 19:58:12 +00:00
$on: i => on.bind(null, i),
$once: i => once.bind(null, i),
2021-04-06 20:51:11 +00:00
$off: i => off.bind(null, i),
2021-04-08 14:21:14 +00:00
2021-04-08 14:06:12 +00:00
$children: getCompatChildren,
2021-04-22 21:50:49 +00:00
$listeners: getCompatListeners
} as PublicPropertiesMap)
2021-04-20 13:25:12 +00:00
2021-04-30 19:50:32 +00:00
/* istanbul ignore if */
2021-04-22 21:50:49 +00:00
if (isCompatEnabled(DeprecationTypes.PRIVATE_APIS, null)) {
extend(map, {
$vnode: i => i.vnode,
2021-04-22 21:30:54 +00:00
2021-04-22 21:50:49 +00:00
// inject addtional properties into $options for compat
$options: i => {
let res = resolveMergedOptions(i)
if (res === i.type) res = i.type.__merged = extend({}, res)
res.parent = i.proxy!.$parent
res.propsData = i.vnode.props
return res
},
2021-04-22 18:59:54 +00:00
2021-04-22 21:50:49 +00:00
// v2 render helpers
$createElement: () => compatH,
_self: i => i.proxy,
_uid: i => i.uid,
_c: () => compatH,
_o: () => legacyMarkOnce,
_n: () => toNumber,
_s: () => toDisplayString,
_l: () => renderList,
_t: i => legacyRenderSlot.bind(null, i),
_q: () => looseEqual,
_i: () => looseIndexOf,
_m: i => legacyRenderStatic.bind(null, i),
_f: () => resolveFilter,
_k: i => legacyCheckKeyCodes.bind(null, i),
_b: () => legacyBindObjectProps,
_v: () => createTextVNode,
_e: () => createCommentVNode,
_u: () => legacyresolveScopedSlots,
_g: () => legacyBindObjectListeners,
_d: () => legacyBindDynamicKeys,
_p: () => legacyPrependModifier
} as PublicPropertiesMap)
}
2021-04-05 22:13:29 +00:00
}