refactor(reactivity): reduce code of type check (#377)
This commit is contained in:
parent
a489f98a66
commit
d76cfba7fb
@ -1,4 +1,4 @@
|
||||
import { isObject, toTypeString } from '@vue/shared'
|
||||
import { isObject, toRawType } from '@vue/shared'
|
||||
import { mutableHandlers, readonlyHandlers } from './baseHandlers'
|
||||
import {
|
||||
mutableCollectionHandlers,
|
||||
@ -29,16 +29,14 @@ const nonReactiveValues = new WeakSet<any>()
|
||||
|
||||
const collectionTypes = new Set<Function>([Set, Map, WeakMap, WeakSet])
|
||||
const isObservableType = /*#__PURE__*/ makeMap(
|
||||
['Object', 'Array', 'Map', 'Set', 'WeakMap', 'WeakSet']
|
||||
.map(t => `[object ${t}]`)
|
||||
.join(',')
|
||||
'Object,Array,Map,Set,WeakMap,WeakSet'
|
||||
)
|
||||
|
||||
const canObserve = (value: any): boolean => {
|
||||
return (
|
||||
!value._isVue &&
|
||||
!value._isVNode &&
|
||||
isObservableType(toTypeString(value)) &&
|
||||
isObservableType(toRawType(value)) &&
|
||||
!nonReactiveValues.has(value)
|
||||
)
|
||||
}
|
||||
|
@ -10,7 +10,7 @@ import {
|
||||
isObject,
|
||||
isReservedProp,
|
||||
hasOwn,
|
||||
toTypeString,
|
||||
toRawType,
|
||||
PatchFlags,
|
||||
makeMap
|
||||
} from '@vue/shared'
|
||||
@ -390,10 +390,6 @@ function styleValue(value: unknown, type: string): string {
|
||||
}
|
||||
}
|
||||
|
||||
function toRawType(value: unknown): string {
|
||||
return toTypeString(value).slice(8, -1)
|
||||
}
|
||||
|
||||
function isExplicable(type: string): boolean {
|
||||
const explicitTypes = ['string', 'number', 'boolean']
|
||||
return explicitTypes.some(elem => type.toLowerCase() === elem)
|
||||
|
@ -48,6 +48,10 @@ export const objectToString = Object.prototype.toString
|
||||
export const toTypeString = (value: unknown): string =>
|
||||
objectToString.call(value)
|
||||
|
||||
export function toRawType(value: unknown): string {
|
||||
return toTypeString(value).slice(8, -1)
|
||||
}
|
||||
|
||||
export const isPlainObject = (val: unknown): val is object =>
|
||||
toTypeString(val) === '[object Object]'
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user