tiny-essentials 1.22.3 → 1.22.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/v1/TinyAdvancedRaffle.min.js +1 -1
- package/dist/v1/TinyDragger.min.js +1 -1
- package/dist/v1/TinyEssentials.min.js +1 -1
- package/dist/v1/TinyEvents.min.js +1 -1
- package/dist/v1/TinyHtml.min.js +1 -1
- package/dist/v1/TinyIframeEvents.min.js +1 -1
- package/dist/v1/TinyLocalStorage.min.js +1 -1
- package/dist/v1/TinyNewWinEvents.min.js +1 -1
- package/dist/v1/TinySmartScroller.min.js +1 -1
- package/dist/v1/TinyUploadClicker.min.js +1 -1
- package/dist/v1/libs/TinyAdvancedRaffle.cjs +13 -13
- package/dist/v1/libs/TinyAdvancedRaffle.d.mts +19 -19
- package/dist/v1/libs/TinyAdvancedRaffle.mjs +13 -13
- package/dist/v1/libs/TinyEvents.cjs +98 -73
- package/dist/v1/libs/TinyEvents.d.mts +22 -22
- package/dist/v1/libs/TinyEvents.mjs +103 -77
- package/dist/v1/libs/TinyHtml.cjs +335 -17
- package/dist/v1/libs/TinyHtml.d.mts +209 -8
- package/dist/v1/libs/TinyHtml.mjs +320 -20
- package/dist/v1/libs/TinyIframeEvents.cjs +11 -11
- package/dist/v1/libs/TinyIframeEvents.d.mts +19 -19
- package/dist/v1/libs/TinyIframeEvents.mjs +11 -11
- package/dist/v1/libs/TinyLocalStorage.cjs +12 -12
- package/dist/v1/libs/TinyLocalStorage.d.mts +21 -21
- package/dist/v1/libs/TinyLocalStorage.mjs +12 -12
- package/dist/v1/libs/TinyNewWinEvents.cjs +11 -11
- package/dist/v1/libs/TinyNewWinEvents.d.mts +19 -19
- package/dist/v1/libs/TinyNewWinEvents.mjs +11 -11
- package/dist/v1/libs/TinySmartScroller.cjs +12 -12
- package/dist/v1/libs/TinySmartScroller.d.mts +53 -30
- package/dist/v1/libs/TinySmartScroller.mjs +12 -12
- package/docs/v1/libs/TinyEvents.md +93 -105
- package/docs/v1/libs/TinyHtml.md +39 -2
- package/package.json +1 -1
|
@@ -28,6 +28,20 @@ class TinyEvents {
|
|
|
28
28
|
#maxListeners = 10;
|
|
29
29
|
/** @type {boolean} */
|
|
30
30
|
#throwMaxListeners = false;
|
|
31
|
+
/**
|
|
32
|
+
* Normalizes the event parameter into an array of strings.
|
|
33
|
+
*
|
|
34
|
+
* @param {string|string[]} event
|
|
35
|
+
* @param {string} method
|
|
36
|
+
* @returns {string[]}
|
|
37
|
+
*/
|
|
38
|
+
#normalizeEvents(event, method) {
|
|
39
|
+
if (typeof event === 'string')
|
|
40
|
+
return [event];
|
|
41
|
+
if (Array.isArray(event) && event.every((e) => typeof e === 'string'))
|
|
42
|
+
return event;
|
|
43
|
+
throw new TypeError(`${method}(event): event must be a string or string[]`);
|
|
44
|
+
}
|
|
31
45
|
/**
|
|
32
46
|
* Enables or disables throwing an error when the maximum number of listeners is exceeded.
|
|
33
47
|
*
|
|
@@ -75,72 +89,76 @@ class TinyEvents {
|
|
|
75
89
|
/**
|
|
76
90
|
* Adds a listener to the beginning of the listeners array for the specified event.
|
|
77
91
|
*
|
|
78
|
-
* @param {string} event - Event name.
|
|
92
|
+
* @param {string|string[]} event - Event name.
|
|
79
93
|
* @param {handler} handler - The callback function.
|
|
80
94
|
*/
|
|
81
95
|
prependListener(event, handler) {
|
|
82
|
-
|
|
83
|
-
throw new TypeError('prepend(event, handler): event name must be a string');
|
|
96
|
+
const events = this.#normalizeEvents(event, 'prependListener');
|
|
84
97
|
if (typeof handler !== 'function')
|
|
85
|
-
throw new TypeError('
|
|
86
|
-
|
|
98
|
+
throw new TypeError('prependListener(event, handler): handler must be a function');
|
|
99
|
+
for (const ev of events)
|
|
100
|
+
this.#prepend(ev, handler);
|
|
87
101
|
}
|
|
88
102
|
/**
|
|
89
103
|
* Adds a one-time listener to the beginning of the listeners array for the specified event.
|
|
90
104
|
*
|
|
91
|
-
* @param {string} event - Event name.
|
|
105
|
+
* @param {string|string[]} event - Event name.
|
|
92
106
|
* @param {handler} handler - The callback function.
|
|
93
|
-
* @returns {handler} - The wrapped handler used internally.
|
|
107
|
+
* @returns {handler[]} - The wrapped handler used internally.
|
|
94
108
|
*/
|
|
95
109
|
prependListenerOnce(event, handler) {
|
|
96
|
-
|
|
97
|
-
throw new TypeError('prependOnceListener(event, handler): event name must be a string');
|
|
110
|
+
const events = this.#normalizeEvents(event, 'prependListenerOnce');
|
|
98
111
|
if (typeof handler !== 'function')
|
|
99
|
-
throw new TypeError('
|
|
100
|
-
|
|
101
|
-
const
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
112
|
+
throw new TypeError('prependListenerOnce(event, handler): handler must be a function');
|
|
113
|
+
const wrappedHandlers = [];
|
|
114
|
+
for (const ev of events) {
|
|
115
|
+
/** @type {handler} */
|
|
116
|
+
const wrapped = (...args) => {
|
|
117
|
+
this.off(ev, wrapped);
|
|
118
|
+
handler(...args);
|
|
119
|
+
};
|
|
120
|
+
this.#prepend(ev, wrapped, { once: true });
|
|
121
|
+
wrappedHandlers.push(wrapped);
|
|
122
|
+
}
|
|
123
|
+
return wrappedHandlers;
|
|
107
124
|
}
|
|
108
125
|
////////////////////////////////////////////////////////////
|
|
109
126
|
/**
|
|
110
127
|
* Adds a event listener.
|
|
111
128
|
*
|
|
112
|
-
* @param {string} event - Event name, such as 'onScrollBoundary' or 'onAutoScroll'.
|
|
129
|
+
* @param {string|string[]} event - Event name, such as 'onScrollBoundary' or 'onAutoScroll'.
|
|
113
130
|
* @param {handler} handler - Callback function to be called when event fires.
|
|
114
131
|
* @param {Object} [settings={}] - Optional settings.
|
|
115
132
|
* @param {boolean} [settings.once=false] - This is a once event.
|
|
116
133
|
*/
|
|
117
134
|
#on(event, handler, { once = false } = {}) {
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
eventData =
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
const
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
135
|
+
const events = this.#normalizeEvents(event, 'on');
|
|
136
|
+
for (const ev of events) {
|
|
137
|
+
let eventData = this.#listeners.get(ev);
|
|
138
|
+
if (!Array.isArray(eventData)) {
|
|
139
|
+
eventData = [];
|
|
140
|
+
this.#listeners.set(ev, eventData);
|
|
141
|
+
}
|
|
142
|
+
eventData.push({ handler, config: { once } });
|
|
143
|
+
// Warn if listener count exceeds the max allowed
|
|
144
|
+
const max = this.#maxListeners;
|
|
145
|
+
if (max > 0 && eventData.length > max) {
|
|
146
|
+
const warnMessage = `Possible memory leak detected. ${eventData.length} "${ev}" listeners added. ` +
|
|
147
|
+
`Use setMaxListeners() to increase limit.`;
|
|
148
|
+
if (!this.#throwMaxListeners)
|
|
149
|
+
console.warn(warnMessage);
|
|
150
|
+
else
|
|
151
|
+
throw new Error(warnMessage);
|
|
152
|
+
}
|
|
133
153
|
}
|
|
134
154
|
}
|
|
135
155
|
/**
|
|
136
156
|
* Adds a event listener.
|
|
137
157
|
*
|
|
138
|
-
* @param {string} event - Event name, such as 'onScrollBoundary' or 'onAutoScroll'.
|
|
158
|
+
* @param {string|string[]} event - Event name, such as 'onScrollBoundary' or 'onAutoScroll'.
|
|
139
159
|
* @param {handler} handler - Callback function to be called when event fires.
|
|
140
160
|
*/
|
|
141
161
|
on(event, handler) {
|
|
142
|
-
if (typeof event !== 'string')
|
|
143
|
-
throw new TypeError('on(event, handler): event name must be a string');
|
|
144
162
|
if (typeof handler !== 'function')
|
|
145
163
|
throw new TypeError('on(event, handler): handler must be a function');
|
|
146
164
|
return this.#on(event, handler);
|
|
@@ -148,28 +166,30 @@ class TinyEvents {
|
|
|
148
166
|
/**
|
|
149
167
|
* Registers an event listener that runs only once, then is removed.
|
|
150
168
|
*
|
|
151
|
-
* @param {string} event - Event name, such as 'onScrollBoundary' or 'onAutoScroll'.
|
|
169
|
+
* @param {string|string[]} event - Event name, such as 'onScrollBoundary' or 'onAutoScroll'.
|
|
152
170
|
* @param {handler} handler - The callback function to run on event.
|
|
153
|
-
* @returns {handler} - The wrapped version of the handler.
|
|
171
|
+
* @returns {handler[]} - The wrapped version of the handler.
|
|
154
172
|
*/
|
|
155
173
|
once(event, handler) {
|
|
156
|
-
|
|
157
|
-
throw new TypeError('The event name must be a string.');
|
|
174
|
+
const events = this.#normalizeEvents(event, 'once');
|
|
158
175
|
if (typeof handler !== 'function')
|
|
159
176
|
throw new TypeError('once(event, handler): handler must be a function');
|
|
160
|
-
|
|
161
|
-
const
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
177
|
+
const wrappedHandlers = [];
|
|
178
|
+
for (const ev of events) {
|
|
179
|
+
/** @type {handler} */
|
|
180
|
+
const wrapped = (...args) => {
|
|
181
|
+
this.off(ev, wrapped);
|
|
182
|
+
handler(...args);
|
|
183
|
+
};
|
|
184
|
+
this.#on(ev, wrapped, { once: true });
|
|
185
|
+
wrappedHandlers.push(wrapped);
|
|
186
|
+
}
|
|
187
|
+
return wrappedHandlers;
|
|
168
188
|
}
|
|
169
189
|
/**
|
|
170
190
|
* Adds a event listener.
|
|
171
191
|
*
|
|
172
|
-
* @param {string} event - Event name, such as 'onScrollBoundary' or 'onAutoScroll'.
|
|
192
|
+
* @param {string|string[]} event - Event name, such as 'onScrollBoundary' or 'onAutoScroll'.
|
|
173
193
|
* @param {handler} handler - Callback function to be called when event fires.
|
|
174
194
|
*/
|
|
175
195
|
appendListener(event, handler) {
|
|
@@ -178,9 +198,9 @@ class TinyEvents {
|
|
|
178
198
|
/**
|
|
179
199
|
* Registers an event listener that runs only once, then is removed.
|
|
180
200
|
*
|
|
181
|
-
* @param {string} event - Event name, such as 'onScrollBoundary' or 'onAutoScroll'.
|
|
201
|
+
* @param {string|string[]} event - Event name, such as 'onScrollBoundary' or 'onAutoScroll'.
|
|
182
202
|
* @param {handler} handler - The callback function to run on event.
|
|
183
|
-
* @returns {handler} - The wrapped version of the handler.
|
|
203
|
+
* @returns {handler[]} - The wrapped version of the handler.
|
|
184
204
|
*/
|
|
185
205
|
appendListenerOnce(event, handler) {
|
|
186
206
|
return this.once(event, handler);
|
|
@@ -189,33 +209,34 @@ class TinyEvents {
|
|
|
189
209
|
/**
|
|
190
210
|
* Removes a previously registered event listener.
|
|
191
211
|
*
|
|
192
|
-
* @param {string} event - The name of the event to remove the handler from.
|
|
212
|
+
* @param {string|string[]} event - The name of the event to remove the handler from.
|
|
193
213
|
* @param {handler} handler - The specific callback function to remove.
|
|
194
214
|
*/
|
|
195
215
|
off(event, handler) {
|
|
196
|
-
|
|
197
|
-
throw new TypeError('off(event, handler): event name must be a string');
|
|
216
|
+
const events = this.#normalizeEvents(event, 'off');
|
|
198
217
|
if (typeof handler !== 'function')
|
|
199
218
|
throw new TypeError('off(event, handler): handler must be a function');
|
|
200
|
-
const
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
219
|
+
for (const ev of events) {
|
|
220
|
+
const listeners = this.#listeners.get(ev);
|
|
221
|
+
if (!Array.isArray(listeners))
|
|
222
|
+
continue;
|
|
223
|
+
const index = listeners.findIndex((listener) => listener.handler === handler);
|
|
224
|
+
if (index !== -1)
|
|
225
|
+
listeners.splice(index, 1);
|
|
226
|
+
// Optionally clean up empty arrays (optional)
|
|
227
|
+
if (listeners.length === 0)
|
|
228
|
+
this.#listeners.delete(ev);
|
|
229
|
+
}
|
|
209
230
|
}
|
|
210
231
|
/**
|
|
211
232
|
* Removes all event listeners of a specific type from the element.
|
|
212
233
|
*
|
|
213
|
-
* @param {string} event - The event type to remove (e.g. 'onScrollBoundary').
|
|
234
|
+
* @param {string|string[]} event - The event type to remove (e.g. 'onScrollBoundary').
|
|
214
235
|
*/
|
|
215
236
|
offAll(event) {
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
237
|
+
const events = this.#normalizeEvents(event, 'offAll');
|
|
238
|
+
for (const ev of events)
|
|
239
|
+
this.#listeners.delete(ev);
|
|
219
240
|
}
|
|
220
241
|
/**
|
|
221
242
|
* Removes all event listeners of all types from the element.
|
|
@@ -291,19 +312,24 @@ class TinyEvents {
|
|
|
291
312
|
/**
|
|
292
313
|
* Emits an event, triggering all registered handlers for that event.
|
|
293
314
|
*
|
|
294
|
-
* @param {string} event - The event name to emit.
|
|
315
|
+
* @param {string|string[]} event - The event name to emit.
|
|
295
316
|
* @param {...any} payload - Optional data to pass to each handler.
|
|
296
|
-
* @returns {boolean} True if any listeners were called, false otherwise.
|
|
317
|
+
* @returns {boolean[]} True if any listeners were called, false otherwise.
|
|
297
318
|
*/
|
|
298
319
|
emit(event, ...payload) {
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
const
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
320
|
+
const events = this.#normalizeEvents(event, 'emit');
|
|
321
|
+
const called = [];
|
|
322
|
+
for (const ev of events) {
|
|
323
|
+
const listeners = this.#listeners.get(ev);
|
|
324
|
+
if (!Array.isArray(listeners) || listeners.length === 0) {
|
|
325
|
+
called.push(false);
|
|
326
|
+
continue;
|
|
327
|
+
}
|
|
328
|
+
// Call all listeners with the provided data
|
|
329
|
+
listeners.forEach((listener) => listener.handler(...payload));
|
|
330
|
+
called.push(true);
|
|
331
|
+
}
|
|
332
|
+
return called;
|
|
307
333
|
}
|
|
308
334
|
///////////////////////////////////
|
|
309
335
|
/**
|