-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdependent-watcher.js
48 lines (40 loc) · 1.38 KB
/
dependent-watcher.js
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
let target = null;
class Dep {
constructor() {
this.subscriberList = [];
}
// 添加订阅方法
depend() {
if (target && !this.subscriberList.includes(target)) {
this.subscriberList.push(target);
}
}
// 发布者更新消息,触发所有的订阅方法
notify() {
this.subscriberList.forEach(sub => sub());
}
}
// 中间依赖的平台
const dep = new Dep();
// 发布者(data 对象中的属性,为了方便处理,直接扩出来了,写成对象也行)
let bookName = "Hello World";
let bookContent = "This is a book";
let publishInfo = ''; // 消息提示
// 订阅者(method 方法)
const target = function () {
publishInfo = `The book ${bookName}‘s content is ${bookContent}`;
}
// 订阅者还未订阅信息
console.log(publishInfo); // ‘’
// 订阅者在平台上添加订阅事件,并推送用户目前的最新信息
dep.depend();
target();
console.log(publishInfo) // The book Hello World‘s content is This is a book
// publisher 更新消息
bookContent = 'The content is "Hello World"';
// 平台还未处理前,原信息没有改变
console.log(publishInfo) // The book Hello World‘s content is This is a book
// 平台观察到 publisher 更新内容了,更新了用户的通知信息
dep.notify();
// 呈现更新后的信息
console.log(publishInfo) // The book Hello World‘s content is The content is "Hello World"