-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheventpine.js
141 lines (107 loc) · 2.99 KB
/
eventpine.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
class EventPine{
events = {};
on(eventName, func, options){
if(options === void 0)
options = {};
if(!func.evSplit && eventName.includes(' ')){
eventName = eventName.split(' ');
func.evSplit = true; // For performance
for (var i = 0; i < eventName.length; i++)
this.on(eventName[i], func, options);
func.evSplit = false; // For performance
return this;
}
if(options.once === true){
if(func.once === void 0)
func.once = 1;
else func.once++;
}
else func.on = true;
if(eventName === '*') eventName = '_any';
var evs = this.events[eventName];
if(evs === void 0){
evs = this.events[eventName] = [];
if(this.events['event.new.name'] !== void 0) // Internal Event
this.emit('event.new.name', EventPine.ignoreInternal, eventName);
}
if(!evs.includes(func)){
evs.push(func);
if(this.events['event.new.callback'] !== void 0) // Internal Event
this.emit('event.new.callback', EventPine.ignoreInternal, eventName);
}
return this;
}
once(eventName, func){
this.on(eventName, func, {once:true});
return this;
}
off(eventName, func){
if(eventName === void 0){
if(this.events['event.deleted'] !== void 0) // Internal Event
this.emit('event.deleted', EventPine.ignoreInternal);
this.events = {};
return this;
}
if(eventName.includes(' ')){
eventName = eventName.split(' ');
func.evSplit = true; // For performance
for (var i = 0; i < eventName.length; i++)
this.off(eventName[i], func);
func.evSplit = false; // For performance
return this;
}
if(eventName === '*') eventName = '_any';
if(this.events[eventName] === void 0)
return this;
var evs;
if(func === void 0){
delete this.events[eventName];
if(this.events['event.deleted'] !== void 0) // Internal Event
this.emit('event.deleted', EventPine.ignoreInternal, eventName);
return this;
}
else{
evs = this.events[eventName];
while(true){
var i = evs.indexOf(func);
if(i === -1)
return this;
evs.splice(i, 1);
}
}
if(evs.length === 0){
delete this.events[eventName];
if(this.events['event.deleted'] !== void 0) // Internal Event
this.emit('event.deleted', EventPine.ignoreInternal, eventName);
}
return this;
}
emit(eventName, a,b,c,d,e){ // Max args = 5
if(this.events._any !== void 0
&& a !== EventPine.ignoreInternal
&& eventName !== '_any')
this.emit.call(this, '_any', eventName, a,b,c,d);
var evs = this.events[eventName];
if(evs === void 0 || evs.length === 0)
return false;
for (var i = 0; i < evs.length; i++){
var ev = evs[i];
ev.call(this, a,b,c,d,e);
if(ev.once === void 0)
continue;
ev.once--;
if(ev.once === 0){
delete ev.once;
if(ev.on === void 0)
evs.splice(i--, 1);
}
}
return true;
}
}
EventPine.ignoreInternal = {};
if(typeof exports === 'object' && typeof module !== 'undefined')
module.exports = EventPine;
else if(typeof window !== 'undefined')
window.EventPine = EventPine;
else this.EventPine = EventPine;