-
Notifications
You must be signed in to change notification settings - Fork 28
/
observer.js
203 lines (179 loc) · 4.78 KB
/
observer.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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/**
* Created by youngwind on 16/8/22.
* 观察对象
*/
import arrayAugmentations from '../observer/array-augmentations';
import objectAugmentations from '../observer/object-augmentations';
const ARRAY = 0;
const OBJECT = 1;
let uid = 0;
/**
* 观察者构造函数
* @param value {Object} 数据对象
* @param type {Int} 数据对象的类型(分为对象和数组)
* @constructor
*/
function Observer(value, type) {
this.value = value;
this.id = ++uid;
// TODO 这里enumerable一定要为false,否则会触发死循环, 原因未明
// 将当前对象存储到当前对象的$observer属性中
Object.defineProperty(value, '$observer', {
value: this,
enumerable: false,
writable: true,
configurable: true
});
if (type === ARRAY) {
value.__proto__ = arrayAugmentations; // eslint-disable-line
this.link(value);
} else if (type === OBJECT) {
value.__proto__ = objectAugmentations; // eslint-disable-line
this.walk(value);
}
}
/**
* 遍历数据对象
* @param obj {Object} 待遍历的数据对象
*/
Observer.prototype.walk = function (obj) {
let val;
for (let key in obj) {
if (!obj.hasOwnProperty(key)) return;
val = obj[key];
// 递归
this.observe(key, val);
this.convert(key, val);
}
};
/**
* 定义对象属性
* @param key {string} 属性键名
* @param val {Any} 属性值
*/
Observer.prototype.convert = function (key, val) {
let ob = this;
Object.defineProperty(this.value, key, {
enumerable: true,
configurable: true,
get: function () {
return val;
},
set: function (newVal) {
if (newVal === val) return;
val = newVal;
ob.notify('set', key, newVal);
ob.notify(`set:${key}`, key, newVal);
}
});
};
/**
* 调用创建observer函数
* 并且判断是否有父节点,如果有,则存储父节点到自身,
* 目的是为了方便后面事件传播使用
* @param key {string} 键值
* @param val {Any} 属性值
*/
Observer.prototype.observe = function (key, val) {
let ob = Observer.create(val);
if (!ob) return;
ob.parent = {
key,
ob: this
};
};
/**
* 这个方法是用来处理如下情况: var ary = [1,{name:liangshaofeng}]
* 也就是说,当数组的某些项是一个对象的时候,
* 那么需要给这个对象创建observer监听它
* @param items {Array} 待处理数组
*/
Observer.prototype.link = function (items) {
items.forEach((value, index) => {
this.observe(index, value);
});
};
/**
* 订阅事件
* @param event {string} 事件类型
* @param fn {Function} 对调函数
* @returns {Observer} 观察者对象
*/
Observer.prototype.on = function (event, fn) {
this._cbs = this._cbs || {};
if (!this._cbs[event]) {
this._cbs[event] = [];
}
this._cbs[event].push(fn);
// 这里return this是为了实现.on(...).on(...)这样的级联调用
return this;
};
/**
* 取消订阅事件
* @param event {string} 事件类型
* @param fn {Function} 回调函数
* @returns {Observer} 观察者对象
*/
Observer.prototype.off = function (event, fn) {
this._cbs = this._cbs || {};
// 取消所有订阅事件
if (!arguments.length) {
this._cbs = {};
return this;
}
let callbacks = this._cbs[event];
if (!callbacks) return this;
// 取消特定事件
if (arguments.length === 1) {
delete this._cbs[event];
return this;
}
// 取消特定事件的特定回调函数
for (let i = 0, cb; i < callbacks.length; i++) {
cb = callbacks[i];
if (cb === fn) {
callbacks.splice(i, 1);
break;
}
}
return this;
};
/**
* 触发消息, 并且将消息逐层往上传播
*
*/
Observer.prototype.notify = function (event, path, val) {
this.emit(event, path, val);
let parent = this.parent;
if (!parent) return;
let ob = parent.ob;
ob.notify(event, path, val);
};
/**
* 触发执行回调函数
* @param event {string} 事件类型
* @param event {path} 事件触发路径
*
*/
Observer.prototype.emit = function (event, path, val) {
this._cbs = this._cbs || {};
let callbacks = this._cbs[event];
if (!callbacks) return;
callbacks = callbacks.slice(0);
callbacks.forEach((cb, i) => {
callbacks[i].apply(this, arguments);
});
};
/**
* 根据不同的数据类型,调用observer构造函数
* @param value {Any} 数据
* @returns {Observer}
*/
Observer.create = function (value) {
if (Array.isArray(value)) {
return new Observer(value, ARRAY);
} else if (typeof value === 'object') {
return new Observer(value, OBJECT);
}
};
module.exports = Observer;