-
Notifications
You must be signed in to change notification settings - Fork 0
/
4.9.html
164 lines (143 loc) · 3.87 KB
/
4.9.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
158
159
160
161
162
163
164
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<div id="root"></div>
<script>
const obj = { foo: 1, bar: { name: "qqq" } };
const bucket = new WeakMap();
let activeEffect = null;
const effectsStack = [];
const data = new Proxy(obj, {
get(target, key) {
track(target, key);
return target[key];
},
set(target, key, value) {
target[key] = value;
trigger(target, key);
},
});
function track(target, key) {
if (!activeEffect) return true;
let depsMap = bucket.get(target);
if (!depsMap) {
bucket.set(target, (depsMap = new Map()));
}
let deps = depsMap.get(key);
if (!deps) {
depsMap.set(key, (deps = new Set()));
}
deps.add(activeEffect);
activeEffect.deps.push(deps);
}
function trigger(target, key) {
const depsMap = bucket.get(target);
if (!depsMap) return true;
const effects = depsMap.get(key);
const newEffects = new Set(effects);
const effectsToRuns = new Set();
newEffects &&
newEffects.forEach((effect) => {
if (effect !== activeEffect) effectsToRuns.add(effect);
});
effectsToRuns.forEach((effect) => {
if (effect.options.shedule) {
effect.options.shedule(effect);
} else {
effect();
}
});
}
function clearnUp(effectFn) {
effectFn.deps.forEach((deps) => {
deps.delete(effectFn);
});
effectFn.deps.length = 0;
}
function effect(fn, options = {}) {
let effectFn = () => {
clearnUp(effectFn);
activeEffect = effectFn;
effectsStack.push(activeEffect);
const res = fn();
effectsStack.pop();
activeEffect = effectsStack[effectsStack.length - 1];
return res;
};
effectFn.deps = [];
effectFn.options = options;
if (!options.lazy) {
effectFn();
}
return effectFn;
}
function computed(getter) {
let dirtied = true;
let value = null;
const effectFn = effect(getter, {
lazy: true,
shedule: () => {
if (!dirtied) {
dirtied = true;
trigger(obj, "value");
}
},
});
const obj = {
get value() {
if (dirtied) {
value = effectFn();
dirtied = false;
}
track(obj, "value");
return value;
},
};
return obj;
}
function traverse(value, seen = new Set()) {
if (typeof value !== "object" || value === null || seen.has(value))
return value;
seen.add(value);
for (const key in value) {
traverse(value[key], seen);
}
return value;
}
function watch(source, callbakc) {
let getter;
if (typeof source === "function") {
getter = source;
} else {
getter = () => traverse(source);
}
let oldValue;
const effectFn = effect(() => getter(), {
shedule: (fn) => {
const newValue = fn();
callbakc(newValue, oldValue);
oldValue = newValue;
},
lazy: true,
});
oldValue = effectFn();
}
watch(
() => data.foo,
(d1, d2) => {
console.log(d1);
root.innerHTML = d1;
}
);
setTimeout(() => {
data.foo = "qqq";
}, 1000);
</script>
</body>
</html>