-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
55 lines (45 loc) · 1.17 KB
/
index.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
'use strict';
class VueEventer {
constructor(){
this.events = {};
}
on(event, func) {
this.events[event] = this.events[event] || [];
this.events[event].push({ func : func, once : false });
}
$on(event, func) {
this.on(event, func);
}
once(event, func) {
this.events[event] = this.events[event] || [];
this.events[event].push({ func : func, once : true });
}
$once(event, func) {
this.once(event, func);
}
off(event, func) {
if (this.events[event]) {
for (var i = 0; i < this.events[event].length; i++) {
if (this.events[event][i].func === func) {
this.events[event].splice(i, 1);
break;
}
};
}
}
$off(event, func) {
this.off(event, func);
}
emit(event, ...args) {
(this.events[event] || []).forEach((item) => {
if (item.once) {
this.off(event, item.func);
}
item.func(...args);
});
}
$emit(event, ...args) {
this.emit(event, ...args);
}
}
export default VueEventer;