-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathliscap.js
136 lines (122 loc) · 3.85 KB
/
liscap.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
/**
* @file Library to limit the number of listeners on an element
* @author Benedict Gabriel <[email protected]>
* @version 1.0.0
* @since 1.0.0
* @license MIT
*
*
* @example <caption>Example usage of liscap</caption>
* const liscap = new liscap();
* const element = document.querySelector('button');
* liscap.addEventListener(element, 'click', () => console.log('Clicked'));
* liscap.lock();
* liscap.addEventListener(element, 'click', () => console.log('Clicked')); // Error: You can't add more listeners
* element.addEventListener('click', () => console.log('Clicked')); // Error: Max listeners exceeded
*/
// @ts-check
/**
* @class liscap
* @classdesc Create a new liscap instance
*/
var liscap = new (class liscap {
constructor() {
this._maxListeners = {};
this._lock = false;
}
/**
* addEventListener - Add an event listener to an element and limit the number of listeners on the element
* @param {HTMLElement | Window | Document} element - DOM element
* @param {String} type - Event type
* @param {EventListenerOrEventListenerObject} callback - Callback function
* @param {Object} options - Event listener options
* @returns {void | Error} - Returns void or throws an error
*/
addEventListener(element, type, callback = () => {}, options = {}) {
if (
!(
element instanceof HTMLElement ||
element instanceof Window ||
element instanceof Document
)
) {
throw new Error("Invalid element");
}
if (this._lock) {
throw new Error("You cannot add more listeners");
}
this._maxListeners[element] = (this._maxListeners[element] || 0) + 1;
element.addEventListener = (function (maxListeners) {
let count = 0;
const original = element.addEventListener;
return function (type, listener, options) {
if (count >= maxListeners[element]) {
throw new Error("Max listeners exceeded");
}
count++;
original.call(this, type, listener, options);
};
})(this._maxListeners);
element.addEventListener(type, callback, options);
}
/**
* removeEventListener - Remove an event listener from an element and decreases the number of listeners on the element
* @param {HTMLElement | Window | Document} element - DOM element
* @param {String} type - Event type
* @param {EventListenerOrEventListenerObject} callback - Callback function
* @param {Object} options - Event listener options
* @returns {void | Error} - Returns void or throws an error
*/
removeEventListener(element, type, callback, options = {}) {
if (this._lock) {
throw new Error("You cannot remove more listeners");
}
if (
!(
element instanceof HTMLElement ||
element instanceof Window ||
element instanceof Document
)
) {
throw new Error("Invalid element");
}
element.removeEventListener(type, callback, options);
}
/**
* lock - Lock the instance to prevent adding more listeners
* Note: This is a one-time operation and can't be undone
* @returns {void}
*/
lock() {
(() => {
[
window,
document,
document.documentElement,
document.body,
document.head,
].forEach((element) => {
if (element)
// @ts-ignore
this.addEventListener(element, "custom", () => {});
});
["button", "input", "select", "textarea", "form"].forEach((tag) => {
document.querySelectorAll(tag)?.forEach((element) => {
if (element)
// @ts-ignore
this.addEventListener(element, "custom", () => {});
});
});
})();
this._lock = true;
Object.freeze(this);
}
/**
* sayHello - Check if the liscap instance is active
* @returns {void}
*/
sayHello() {
console.log("Hello from liscap");
}
})();
module.exports = liscap;