-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.js
More file actions
70 lines (60 loc) · 1.96 KB
/
Copy pathmain.js
File metadata and controls
70 lines (60 loc) · 1.96 KB
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
// creating own own event emmiter
// all methods of event emmiters
class Emmiter {
constructor() {
this._eventsCount = 0;
this._maxListeners = 10;
this._onceEvents = {};
this._events = {};
};
_events;
_onceEvents;
_eventsCount;
_maxListeners;
setMaxEventListeners(x) {
this._maxListeners = x;
};
on(eventName, listner) {
if (!this._events[eventName]) {
this._events[eventName] = [listner];
this._eventsCount++;
} else {
if (this._events[eventName].length >= this._maxListeners) {
console.warn(`Max listeners exceeded for event: ${eventName}`);
}
this._events[eventName].push(listner);
this._eventsCount++;
}
};
emit(eventName, ...args) {
if (this._events[eventName]) {
for (const fn of this._events[eventName]) {
fn(...args);
};
}
if (this._onceEvents[eventName]) {
for (const fno of this._onceEvents[eventName]) {
fno(...args);
this.removeListener(eventName, fno);
}
}
};
once(eventName, listner) {
if (!this._onceEvents[eventName]) {
this._onceEvents[eventName] = [listner]
this._eventsCount++;
} else {
if (this._onceEvents[eventName].length >= this._maxListeners) {
console.warn(`Max listeners exceeded for event: ${eventName}`);
};
this._onceEvents[eventName].push(listner);
this._eventsCount++;
}
};
removeListener(eventName, listeners) {
if (this._events[eventName]) {
this._events[eventName] = this._events[eventName].filter(lisnr => lisnr !== listeners);
}
if (this._onceEvents[eventName]) this._onceEvents[eventName] = this._onceEvents[eventName].filter(lisnr => lisnr !== listeners);
};
};