-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathref.ts
67 lines (58 loc) · 1.5 KB
/
ref.ts
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
import { hasChange, isObject } from '../shared'
import { isTracking, trackEffects, triggerEffects } from './effect'
import { reactive } from './reactive'
class RefImpl {
private _value: any // 保存代理后的值
public dep // 依赖
private _rawValue: any // 保存原始值
public __v_isRef = true // 标识是否是ref
constructor(value) {
this._rawValue = value
this._value = convert(value)
this.dep = new Set()
}
get value() {
trackRefValue(this)
return this._value
}
set value(newValue) {
if (hasChange(newValue, this._rawValue)) {
this._rawValue = newValue
this._value = convert(newValue)
triggerEffects(this.dep)
}
}
}
function trackRefValue(ref) {
if (isTracking()) {
trackEffects(ref.dep)
}
}
// 如果是对象,就使用reactive代理
function convert(value) {
return isObject(value) ? reactive(value) : value
}
export function ref(value) {
return new RefImpl(value)
}
export function isRef(ref) {
return !!ref.__v_isRef
}
export function unRef(ref) {
return isRef(ref) ? ref.value : ref
}
// template里面调用了这个方法,所以可以不用使用.value的形式
export function proxyRefs(objectWithRefs) {
return new Proxy(objectWithRefs, {
get(target, key) {
return unRef(Reflect.get(target, key))
},
set(target, key, value) {
if(isRef(target[key]) && !isRef(value)){
return target[key].value = value
} else {
return Reflect.set(target, key, value)
}
},
})
}