-
Notifications
You must be signed in to change notification settings - Fork 3
/
proxy.html
157 lines (120 loc) Β· 3.23 KB
/
proxy.html
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<input id="r" type="range" min="0" max="255" />
<input id="g" type="range" min="0" max="255" />
<input id="b" type="range" min="0" max="255" />
<div id="color" style="width: 100px; height: 100px;background-color: rgb(255, 255, 255);"> </div>
</body>
<script>
let handlers = new Map(); // callback
let reactivities = new Map();
let usedReactivities = [];
let object = {
a: 1,
b: 2
}
function reactive(obj) {
if (reactivities.has(obj)) {
return reactivities.get(obj)
}
let proxy = new Proxy(obj, {
get(obj, prop) {
usedReactivities.push([obj, prop])
if (typeof obj[prop] === "object")
return reactive(obj[prop]);
return obj[prop]
},
set(obj, prop, val) {
obj[prop] = val
// console.log(handlers)
if (handlers.get(obj)) {
if (handlers.get(obj).get(prop)) {
for (let handler of handlers.get(obj).get(prop)) {
handler()
}
}
}
return obj[prop]
}
})
reactivities.set(obj, proxy)
reactivities.set(proxy, proxy)
return proxy
}
// δΎθ΅ζΆι
// ε
ηε¬ εζΆι
function effect(handler) {
usedReactivities = []
handler()
// console.log(usedReactivities)
for(let usedReactivity of usedReactivities) {
let [obj, prop] = usedReactivity
// console.log([obj, prop])
if (!handlers.has(obj)) {
handlers.set(obj, new Map())
}
if (!handlers.get(obj).has(prop)) {
handlers.get(obj).set(prop, [])
}
handlers.get(obj).get(prop).push(handler)
}
}
// let dummy;
// let proxy = reactive({a: 1})
// console.log(() => dummy = proxy.a)
// console.log(dummy)
// proxy.a = 2
// console.log(dummy)
// let v12;
// let p1 = reactive({a: 1});
// let p2 = reactive({a: 2});
// effect(() => v12 = p1.a + p2.a)
// effect(() => v1 = p1.a)
// effect(() => v2 = p2.a)
// let v;
// let p = reactive(object)
// effect(() => v = p.a.x)
// console.log(v)
// p.a = {x: 4};
// console.log(v)
let p = reactive({ r: 100, g: 100, b: 100 })
effect(() => {
document.getElementById("r").value = p.r
})
effect(() => {
document.getElementById("g").value = p.g
})
effect(() => {
document.getElementById("b").value = p.b
})
document.getElementById("r").addEventListener("input", event => {
p.r = event.target.value
})
document.getElementById("g").addEventListener("input", event => {
p.g = event.target.value
})
document.getElementById("b").addEventListener("input", event => {
p.b = event.target.value
})
effect(() => {
document.getElementById("color").style.backgroundColor = `rgb(${p.r}, ${p.g}, ${p.b})`
})
// let proxy = new Proxy(obj, {
// get (obj, prop) {
// console.log('obj', obj)
// console.log('prop', prop)
// return obj[prop]
// }
// defineProperty(obj, prop, desc) {
// console.log(arguments)
// return Object.defineProperty(obj, prop. desc)
// }
// })
</script>
</html>