当前位置:网站首页>Track and trigger
Track and trigger
2022-04-21 22:44:00 【hamimelon2020】
<!DOCTYPE html>
<html>
<head>
<title>document.write() Example</title>
</head>
<body>
<script type="text/javascript">
const data = {
ok:true, text:'hello world'}
const bucket = new WeakMap()
let activeEffect
function effect(fn) {
const effectFn = () => {
activeEffect = effectFn
fn()
}
effectFn.deps = []
effectFn()
}
const obj = new Proxy(data, {
get(target, key) {
// Collect side effect functions
track(target, key);
console.log(target[key])
return target[key]
},
set(target, key, newVal) {
target[key] = newVal;
// Remove side effects and perform
trigger(target, key);
}
})
// encapsulation track function , Tracking changes
function track(target, key) {
if (!activeEffect) return
let depsMap = bucket.get(target)
//console.log(depsMap)
console.log(bucket)
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)
}
// encapsulation trigger function , Trigger the deputy general manager to re execute with the function
function trigger(target, key) {
const depsMap = bucket.get(target)
if (!depsMap) return
const effects = depsMap.get(key)
console.log(effects)
effects && effects.forEach(fn => fn())
}
effect(function effectFn(){
// console.log('effectFn');
document.body.innerText = obj.ok ? obj.text : 'not'
})
setTimeout(() => {
obj.ok = false // Print effectFn
},2000)
console.log(effects)
</script>
</body>
</html>
版权声明
本文为[hamimelon2020]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204212037094183.html
边栏推荐
- CC10000. ZABBIX———————————————
- mysql读写分离
- 2022年中级会计职称考试会计实务练习题及答案
- Get user information - wechat applet
- L1-058 6翻了 (15 分)
- Redis advanced: data deletion and elimination strategy, master-slave replication, sentinel mode, cluster, enterprise solution
- Yarn online dynamic resource tuning
- Algorithm -- merge K ascending linked lists (kotlin)
- Black box test - data reading and output mode
- [sans titre]
猜你喜欢

Reading notes "autumn garden", "driftwood", "my fragrance"

解锁OpenHarmony技术日!年度盛会,即将揭幕!

1956 college entrance examination mathematics

2022年一级注册建筑师考试建筑材料与构造复习题及答案

Redis advanced: data deletion and elimination strategy, master-slave replication, sentinel mode, cluster, enterprise solution

Introduction to cyclone IV e series

Daily practice questions (3)

Opencv -- histogram processing

2022年中级会计职称考试会计实务练习题及答案

Lesson 5: correlation coefficient
随机推荐
Rvb2601 startup process
Reproduce the pathways language model using colossal AI
L1-063 吃鱼还是吃肉 (10 分)
【matlab】matlab绘图操作技巧
It is revealed that Xiaomi has new machines this month, and many of its products are ready to go
P1053 [NOIP2005 提高组] 篝火晚会
YARN线上动态资源调优
Review questions and answers of building materials and structures in 2022 first-class registered architect examination
D:MATLAB. N practical skills -MATLAB Chinese Forum essence summary
[matlab] matlab drawing operation skills
openCV——几何变换
L3-1 then don't worry (30 points) - pit point, test point analysis
Number to thousand separator number
君禾股份:2021年度营收增长稳健,受益产品出口业绩再创新高
Yarn online dynamic resource tuning
Sorting methods (8 kinds) detailed explanation 7 - counting sorting
L1-055 who is the winner (10 points)
CC00004. ZABBIX———————————————
Module 3: Outsourcing student management system - architecture design document
Fundamentals of Power Electronics