reactive
packages\reactivity\src\reactive.ts
声明全局 代理Map/只读Map
const reactiveMap = new WeakMap<Target, any>()
const readonlyMap = new WeakMap<Target, any>()
reactive(target: object)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| function reactive(target: object) { if (target && (target as Target)[ReactiveFlags.IS_READONLY]) { return target } return createReactiveObject( target, false, mutableHandlers, mutableCollectionHandlers ) }
const mutableHandlers: ProxyHandler<object> = { get, set, deleteProperty, has, ownKeys }
|
shallowReactive
1 2 3 4 5 6
| createReactiveObject( target, false, shallowReactiveHandlers, shallowCollectionHandlers )
|
readonly
1 2 3 4 5 6
| createReactiveObject( target, true, readonlyHandlers, readonlyCollectionHandlers )
|
shallowReadonly
1 2 3 4 5 6
| createReactiveObject( target, true, shallowReadonlyHandlers, readonlyCollectionHandlers )
|
createReactiveObject
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
|
const objectToString = Object.prototype.toString const toTypeString = (value: unknown): string => objectToString.call(value)
const toRawType = (value: unknown): string => { return toTypeString(value).slice(8, -1) }
function targetTypeMap(rawType: string) { switch (rawType) { case 'Object': case 'Array': return TargetType.COMMON case 'Map': case 'Set': case 'WeakMap': case 'WeakSet': return TargetType.COLLECTION default: return TargetType.INVALID } }
function getTargetType(value: Target) { return value[ReactiveFlags.SKIP] || !Object.isExtensible(value) ? TargetType.INVALID : targetTypeMap(toRawType(value)) }
function createReactiveObject( target: Target, isReadonly: boolean, baseHandlers: ProxyHandler<any>, // [FX 做了什么] collectionHandlers: ProxyHandler<any> // [FX做了什么] ) { if (!isObject(target)) { if (__DEV__) { console.warn(`value cannot be made reactive: ${String(target)}`) } return target } if ( target[ReactiveFlags.RAW] && !(isReadonly && target[ReactiveFlags.IS_REACTIVE]) ) { return target } const proxyMap = isReadonly ? readonlyMap : reactiveMap const existingProxy = proxyMap.get(target) if (existingProxy) { return existingProxy } const targetType = getTargetType(target) if (targetType === TargetType.INVALID) { return target }
const proxy = new Proxy( target, targetType === TargetType.COLLECTION ? collectionHandlers : baseHandlers )
proxyMap.set(target, proxy) return proxy }
|
isReactive
1. 判断 value 若是只读, 如果是, 获取 value 得 __v_raw 对象(即原生对象)
递归 isReactive
2. 若不是 只读,直接判断 value 上是否含有 __v_isReactive
isReadonly
判断 value 上是否有 __v_isReadonly 标识
isProxy
判断 value 是否是被代理对象
markRaw(value)
创建一个 value 对象,不会被监听劫持