-
Notifications
You must be signed in to change notification settings - Fork 5
/
simpleObserver.js
147 lines (144 loc) · 4.38 KB
/
simpleObserver.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
const globalData = {};
const globalAtction = {};
const sObserver = {
each(a, cb) {
for (let i = 0; i < a.length; i++) {
if (cb(a[i], i) === false) {
break;
}
}
},
eventMap: {},
dataCacheMap: {},
_merge2List(list,funcObj){
if(funcObj.key){
let added = false;
for(let i=0;i<list.length;i++){
let cur = list[i];
if(cur.key === funcObj.key){
list.splice(i,1,funcObj);
added = true;
}
}
if(!added){ // 加上这层判断,写起来就很严谨了
list.push(funcObj);
}
}else{
list.push(funcObj);
}
},
watch (event, func, index) {
var map = sObserver.eventMap;
// eslint-disable-next-line no-prototype-builtins
if (!map.hasOwnProperty(event)) {
map[event] = [];
}
const list = map[event];
//包装成对象
let funcObj = func;
if(typeof func === 'function'){
funcObj = {key:null,func};
}else{
funcObj = func;
}
if(arguments.length<3){
this._merge2List(list,funcObj);
}else{
if(index<list.length){
if(funcObj.key){
for(let i=0;i<list.length;i++){
let cur = list[i];
if(cur.key === funcObj.key){
list.splice(i,1);
}
}
}
if(index<list.length-1){
list.splice(index,0,funcObj);
}else{
list.push(funcObj);
}
}else{
this._merge2List(list,funcObj);
}
}
},
unWatch(event,func){
var map = sObserver.eventMap;
const list = map[event];
if (!(list&&list.length)) {
return false;
}
let position = -1;
for(let i=0;i<list.length;i++){
if((typeof list[i].func === 'function'&&(list[i].func.name == func.name))||list[i] == func){
position = i
}
}
if(position>-1){
list.splice(position,1);
return true;
}
return false;
},
watchWithCache(event, func) {
sObserver.watch.apply(sObserver,arguments);
// eslint-disable-next-line no-prototype-builtins
if (sObserver.dataCacheMap.hasOwnProperty(event)) {
try {
func(sObserver.dataCacheMap[event]);
} catch (e) {
console && console.log(e);
}
}
},
fire(event, data) {
const preList = sObserver.eventMap[event] || [];
sObserver.eventMap[event] = preList.filter(fnObj=> {return !('$$onlyonce' in fnObj);})
sObserver.each(sObserver.eventMap[event] || [], (cb) => {
try {
if('$$onlyonce' in cb) {
delete cb.$$onlyonce;
}
// 这里的cb 其实对应 each 函数 里的a[i]
// 至于第二个参数,好像没有怎么用到
cb.func(data);
} catch (e) {
console && console.log(e);
}
});
},
fireWithCache(event, data) {
sObserver.dataCacheMap[event] = data;
sObserver.fire(event, data);
},
once (event, func) {
// 将$$onlyonce挂载在函数上,成为func的一个属性,然后用 属性 in 对象判断是否存在
func.$$onlyonce = true;
sObserver.watch.apply(sObserver, arguments);
// 上面这一句代表的是1、sObserver 成为 watch函数内部的this了 2、arguments指代的就是 event func
},
setGData(key,data){
globalData[key] = data;
},
getGData(key){
// eslint-disable-next-line no-prototype-builtins
if(globalData.hasOwnProperty(key)){
return globalData[key]
}
return null;
},
setGAction(key,data){
globalAtction[key] = data;
},
getGAction(key){
return globalAtction[key] || null;
}
};
if(!window.$$sObserver){
window.$$sObserver = sObserver;
window.$$sObserver_fire = function(){
sObserver.fire.apply(sObserver, arguments);
};
}
export default sObserver;