forked from wuyawei/fe-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
197 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,77 @@ | ||
let ironman = { | ||
hobbies: ['1', '23', '56'] | ||
let data = { | ||
name: '渣渣辉' | ||
}; | ||
class Dep { | ||
constructor() { | ||
this.subs = new Map(); | ||
} | ||
addSub(key, sub) { | ||
const hasSub = this.subs.get(key); | ||
if (hasSub) { | ||
hasSub.add(sub); | ||
} else { | ||
this.subs.set(key, new Set([sub])); | ||
} | ||
} | ||
notify(key) { | ||
if (this.subs.get(key)) { | ||
this.subs.get(key).forEach(sub => { | ||
sub.update(); | ||
}); | ||
} | ||
} | ||
} | ||
let ironmanProxy = new Proxy(ironman.hobbies, { | ||
set (target, property, value) { | ||
target[property] = value | ||
console.log('change....', property, value) | ||
return true | ||
class Watcher { | ||
constructor(obj, key, cb) { | ||
this.obj = obj; | ||
this.key = key; | ||
this.cb = cb; // 回调 | ||
this.value = this.get(); // 获取老数据 | ||
} | ||
get() { | ||
Dep.target = this; | ||
let value = this.obj[this.key]; | ||
Dep.target = null; | ||
return value; | ||
} | ||
// 将订阅者放入待更新队列等待批量更新 | ||
update() { | ||
let newVal = this.obj[this.key]; | ||
if (this.value !== newVal) { | ||
this.cb(newVal); | ||
} | ||
} | ||
}) | ||
} | ||
function Observer(obj) { | ||
Object.keys(obj).forEach(key => { // 做深度监听 | ||
if (typeof obj[key] === 'object') { | ||
obj[key] = Observer(obj[key]); | ||
} | ||
}); | ||
let dep = new Dep(); | ||
let handler = { | ||
get: function (target, key, receiver) { | ||
Dep.target && dep.addSub(key, Dep.target); | ||
return Reflect.get(target, key, receiver); | ||
}, | ||
set: function (target, key, value, receiver) { | ||
let result = Reflect.set(target, key, value, receiver); | ||
dep.notify(key); | ||
return result; | ||
} | ||
}; | ||
return new Proxy(obj, handler) | ||
} | ||
function print1(data) { | ||
console.log('我系', data); | ||
} | ||
function print2(data) { | ||
console.log('我今年', data); | ||
} | ||
data = Observer(data); | ||
new Watcher(data, 'name', print1); | ||
data.name = '杨过'; // 我系 杨过 | ||
|
||
ironmanProxy.push('wine') | ||
console.log(ironman.hobbies) | ||
new Watcher(data, 'age', print2); | ||
data.age = '24'; // 我今年 24 | ||
console.log(data); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters