tiny-essentials 1.22.2 → 1.22.4
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 +69 -6
- package/dist/v1/libs/TinyHtml.d.mts +44 -6
- package/dist/v1/libs/TinyHtml.mjs +64 -6
- 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 +4 -2
- package/package.json +1 -1
|
@@ -124,9 +124,9 @@ class TinyAdvancedRaffle {
|
|
|
124
124
|
/**
|
|
125
125
|
* Emits an event, triggering all registered handlers for that event.
|
|
126
126
|
*
|
|
127
|
-
* @param {string} event - The event name to emit.
|
|
127
|
+
* @param {string|string[]} event - The event name to emit.
|
|
128
128
|
* @param {...any} payload - Optional data to pass to each handler.
|
|
129
|
-
* @returns {boolean} True if any listeners were called, false otherwise.
|
|
129
|
+
* @returns {boolean[]} True if any listeners were called, false otherwise.
|
|
130
130
|
*/
|
|
131
131
|
#emit(event, ...payload) {
|
|
132
132
|
return this.#events.emit(event, ...payload);
|
|
@@ -155,7 +155,7 @@ class TinyAdvancedRaffle {
|
|
|
155
155
|
/**
|
|
156
156
|
* Adds a listener to the beginning of the listeners array for the specified event.
|
|
157
157
|
*
|
|
158
|
-
* @param {string} event - Event name.
|
|
158
|
+
* @param {string|string[]} event - Event name.
|
|
159
159
|
* @param {handler} handler - The callback function.
|
|
160
160
|
*/
|
|
161
161
|
prependListener(event, handler) {
|
|
@@ -165,9 +165,9 @@ class TinyAdvancedRaffle {
|
|
|
165
165
|
/**
|
|
166
166
|
* Adds a one-time listener to the beginning of the listeners array for the specified event.
|
|
167
167
|
*
|
|
168
|
-
* @param {string} event - Event name.
|
|
168
|
+
* @param {string|string[]} event - Event name.
|
|
169
169
|
* @param {handler} handler - The callback function.
|
|
170
|
-
* @returns {handler} - The wrapped handler used internally.
|
|
170
|
+
* @returns {handler[]} - The wrapped handler used internally.
|
|
171
171
|
*/
|
|
172
172
|
prependListenerOnce(event, handler) {
|
|
173
173
|
return this.#events.prependListenerOnce(event, handler);
|
|
@@ -178,7 +178,7 @@ class TinyAdvancedRaffle {
|
|
|
178
178
|
/**
|
|
179
179
|
* Adds a event listener.
|
|
180
180
|
*
|
|
181
|
-
* @param {string} event - Event name, such as 'onScrollBoundary' or 'onAutoScroll'.
|
|
181
|
+
* @param {string|string[]} event - Event name, such as 'onScrollBoundary' or 'onAutoScroll'.
|
|
182
182
|
* @param {handler} handler - Callback function to be called when event fires.
|
|
183
183
|
*/
|
|
184
184
|
appendListener(event, handler) {
|
|
@@ -188,9 +188,9 @@ class TinyAdvancedRaffle {
|
|
|
188
188
|
/**
|
|
189
189
|
* Registers an event listener that runs only once, then is removed.
|
|
190
190
|
*
|
|
191
|
-
* @param {string} event - Event name, such as 'onScrollBoundary' or 'onAutoScroll'.
|
|
191
|
+
* @param {string|string[]} event - Event name, such as 'onScrollBoundary' or 'onAutoScroll'.
|
|
192
192
|
* @param {handler} handler - The callback function to run on event.
|
|
193
|
-
* @returns {handler} - The wrapped version of the handler.
|
|
193
|
+
* @returns {handler[]} - The wrapped version of the handler.
|
|
194
194
|
*/
|
|
195
195
|
appendListenerOnce(event, handler) {
|
|
196
196
|
return this.#events.appendListenerOnce(event, handler);
|
|
@@ -199,7 +199,7 @@ class TinyAdvancedRaffle {
|
|
|
199
199
|
/**
|
|
200
200
|
* Adds a event listener.
|
|
201
201
|
*
|
|
202
|
-
* @param {string} event - Event name, such as 'onScrollBoundary' or 'onAutoScroll'.
|
|
202
|
+
* @param {string|string[]} event - Event name, such as 'onScrollBoundary' or 'onAutoScroll'.
|
|
203
203
|
* @param {handler} handler - Callback function to be called when event fires.
|
|
204
204
|
*/
|
|
205
205
|
on(event, handler) {
|
|
@@ -209,9 +209,9 @@ class TinyAdvancedRaffle {
|
|
|
209
209
|
/**
|
|
210
210
|
* Registers an event listener that runs only once, then is removed.
|
|
211
211
|
*
|
|
212
|
-
* @param {string} event - Event name, such as 'onScrollBoundary' or 'onAutoScroll'.
|
|
212
|
+
* @param {string|string[]} event - Event name, such as 'onScrollBoundary' or 'onAutoScroll'.
|
|
213
213
|
* @param {handler} handler - The callback function to run on event.
|
|
214
|
-
* @returns {handler} - The wrapped version of the handler.
|
|
214
|
+
* @returns {handler[]} - The wrapped version of the handler.
|
|
215
215
|
*/
|
|
216
216
|
once(event, handler) {
|
|
217
217
|
return this.#events.once(event, handler);
|
|
@@ -222,7 +222,7 @@ class TinyAdvancedRaffle {
|
|
|
222
222
|
/**
|
|
223
223
|
* Removes a previously registered event listener.
|
|
224
224
|
*
|
|
225
|
-
* @param {string} event - The name of the event to remove the handler from.
|
|
225
|
+
* @param {string|string[]} event - The name of the event to remove the handler from.
|
|
226
226
|
* @param {handler} handler - The specific callback function to remove.
|
|
227
227
|
*/
|
|
228
228
|
off(event, handler) {
|
|
@@ -232,7 +232,7 @@ class TinyAdvancedRaffle {
|
|
|
232
232
|
/**
|
|
233
233
|
* Removes all event listeners of a specific type from the element.
|
|
234
234
|
*
|
|
235
|
-
* @param {string} event - The event type to remove (e.g. 'onScrollBoundary').
|
|
235
|
+
* @param {string|string[]} event - The event type to remove (e.g. 'onScrollBoundary').
|
|
236
236
|
*/
|
|
237
237
|
offAll(event) {
|
|
238
238
|
return this.#events.offAll(event);
|
|
@@ -288,61 +288,61 @@ declare class TinyAdvancedRaffle {
|
|
|
288
288
|
/**
|
|
289
289
|
* Adds a listener to the beginning of the listeners array for the specified event.
|
|
290
290
|
*
|
|
291
|
-
* @param {string} event - Event name.
|
|
291
|
+
* @param {string|string[]} event - Event name.
|
|
292
292
|
* @param {handler} handler - The callback function.
|
|
293
293
|
*/
|
|
294
|
-
prependListener(event: string, handler: handler): void;
|
|
294
|
+
prependListener(event: string | string[], handler: handler): void;
|
|
295
295
|
/**
|
|
296
296
|
* Adds a one-time listener to the beginning of the listeners array for the specified event.
|
|
297
297
|
*
|
|
298
|
-
* @param {string} event - Event name.
|
|
298
|
+
* @param {string|string[]} event - Event name.
|
|
299
299
|
* @param {handler} handler - The callback function.
|
|
300
|
-
* @returns {handler} - The wrapped handler used internally.
|
|
300
|
+
* @returns {handler[]} - The wrapped handler used internally.
|
|
301
301
|
*/
|
|
302
|
-
prependListenerOnce(event: string, handler: handler): handler;
|
|
302
|
+
prependListenerOnce(event: string | string[], handler: handler): handler[];
|
|
303
303
|
/**
|
|
304
304
|
* Adds a event listener.
|
|
305
305
|
*
|
|
306
|
-
* @param {string} event - Event name, such as 'onScrollBoundary' or 'onAutoScroll'.
|
|
306
|
+
* @param {string|string[]} event - Event name, such as 'onScrollBoundary' or 'onAutoScroll'.
|
|
307
307
|
* @param {handler} handler - Callback function to be called when event fires.
|
|
308
308
|
*/
|
|
309
|
-
appendListener(event: string, handler: handler): void;
|
|
309
|
+
appendListener(event: string | string[], handler: handler): void;
|
|
310
310
|
/**
|
|
311
311
|
* Registers an event listener that runs only once, then is removed.
|
|
312
312
|
*
|
|
313
|
-
* @param {string} event - Event name, such as 'onScrollBoundary' or 'onAutoScroll'.
|
|
313
|
+
* @param {string|string[]} event - Event name, such as 'onScrollBoundary' or 'onAutoScroll'.
|
|
314
314
|
* @param {handler} handler - The callback function to run on event.
|
|
315
|
-
* @returns {handler} - The wrapped version of the handler.
|
|
315
|
+
* @returns {handler[]} - The wrapped version of the handler.
|
|
316
316
|
*/
|
|
317
|
-
appendListenerOnce(event: string, handler: handler): handler;
|
|
317
|
+
appendListenerOnce(event: string | string[], handler: handler): handler[];
|
|
318
318
|
/**
|
|
319
319
|
* Adds a event listener.
|
|
320
320
|
*
|
|
321
|
-
* @param {string} event - Event name, such as 'onScrollBoundary' or 'onAutoScroll'.
|
|
321
|
+
* @param {string|string[]} event - Event name, such as 'onScrollBoundary' or 'onAutoScroll'.
|
|
322
322
|
* @param {handler} handler - Callback function to be called when event fires.
|
|
323
323
|
*/
|
|
324
|
-
on(event: string, handler: handler): void;
|
|
324
|
+
on(event: string | string[], handler: handler): void;
|
|
325
325
|
/**
|
|
326
326
|
* Registers an event listener that runs only once, then is removed.
|
|
327
327
|
*
|
|
328
|
-
* @param {string} event - Event name, such as 'onScrollBoundary' or 'onAutoScroll'.
|
|
328
|
+
* @param {string|string[]} event - Event name, such as 'onScrollBoundary' or 'onAutoScroll'.
|
|
329
329
|
* @param {handler} handler - The callback function to run on event.
|
|
330
|
-
* @returns {handler} - The wrapped version of the handler.
|
|
330
|
+
* @returns {handler[]} - The wrapped version of the handler.
|
|
331
331
|
*/
|
|
332
|
-
once(event: string, handler: handler): handler;
|
|
332
|
+
once(event: string | string[], handler: handler): handler[];
|
|
333
333
|
/**
|
|
334
334
|
* Removes a previously registered event listener.
|
|
335
335
|
*
|
|
336
|
-
* @param {string} event - The name of the event to remove the handler from.
|
|
336
|
+
* @param {string|string[]} event - The name of the event to remove the handler from.
|
|
337
337
|
* @param {handler} handler - The specific callback function to remove.
|
|
338
338
|
*/
|
|
339
|
-
off(event: string, handler: handler): void;
|
|
339
|
+
off(event: string | string[], handler: handler): void;
|
|
340
340
|
/**
|
|
341
341
|
* Removes all event listeners of a specific type from the element.
|
|
342
342
|
*
|
|
343
|
-
* @param {string} event - The event type to remove (e.g. 'onScrollBoundary').
|
|
343
|
+
* @param {string|string[]} event - The event type to remove (e.g. 'onScrollBoundary').
|
|
344
344
|
*/
|
|
345
|
-
offAll(event: string): void;
|
|
345
|
+
offAll(event: string | string[]): void;
|
|
346
346
|
/**
|
|
347
347
|
* Removes all event listeners of all types from the element.
|
|
348
348
|
*/
|
|
@@ -106,9 +106,9 @@ class TinyAdvancedRaffle {
|
|
|
106
106
|
/**
|
|
107
107
|
* Emits an event, triggering all registered handlers for that event.
|
|
108
108
|
*
|
|
109
|
-
* @param {string} event - The event name to emit.
|
|
109
|
+
* @param {string|string[]} event - The event name to emit.
|
|
110
110
|
* @param {...any} payload - Optional data to pass to each handler.
|
|
111
|
-
* @returns {boolean} True if any listeners were called, false otherwise.
|
|
111
|
+
* @returns {boolean[]} True if any listeners were called, false otherwise.
|
|
112
112
|
*/
|
|
113
113
|
#emit(event, ...payload) {
|
|
114
114
|
return this.#events.emit(event, ...payload);
|
|
@@ -133,7 +133,7 @@ class TinyAdvancedRaffle {
|
|
|
133
133
|
/**
|
|
134
134
|
* Adds a listener to the beginning of the listeners array for the specified event.
|
|
135
135
|
*
|
|
136
|
-
* @param {string} event - Event name.
|
|
136
|
+
* @param {string|string[]} event - Event name.
|
|
137
137
|
* @param {handler} handler - The callback function.
|
|
138
138
|
*/
|
|
139
139
|
prependListener(event, handler) {
|
|
@@ -142,9 +142,9 @@ class TinyAdvancedRaffle {
|
|
|
142
142
|
/**
|
|
143
143
|
* Adds a one-time listener to the beginning of the listeners array for the specified event.
|
|
144
144
|
*
|
|
145
|
-
* @param {string} event - Event name.
|
|
145
|
+
* @param {string|string[]} event - Event name.
|
|
146
146
|
* @param {handler} handler - The callback function.
|
|
147
|
-
* @returns {handler} - The wrapped handler used internally.
|
|
147
|
+
* @returns {handler[]} - The wrapped handler used internally.
|
|
148
148
|
*/
|
|
149
149
|
prependListenerOnce(event, handler) {
|
|
150
150
|
return this.#events.prependListenerOnce(event, handler);
|
|
@@ -153,7 +153,7 @@ class TinyAdvancedRaffle {
|
|
|
153
153
|
/**
|
|
154
154
|
* Adds a event listener.
|
|
155
155
|
*
|
|
156
|
-
* @param {string} event - Event name, such as 'onScrollBoundary' or 'onAutoScroll'.
|
|
156
|
+
* @param {string|string[]} event - Event name, such as 'onScrollBoundary' or 'onAutoScroll'.
|
|
157
157
|
* @param {handler} handler - Callback function to be called when event fires.
|
|
158
158
|
*/
|
|
159
159
|
appendListener(event, handler) {
|
|
@@ -162,9 +162,9 @@ class TinyAdvancedRaffle {
|
|
|
162
162
|
/**
|
|
163
163
|
* Registers an event listener that runs only once, then is removed.
|
|
164
164
|
*
|
|
165
|
-
* @param {string} event - Event name, such as 'onScrollBoundary' or 'onAutoScroll'.
|
|
165
|
+
* @param {string|string[]} event - Event name, such as 'onScrollBoundary' or 'onAutoScroll'.
|
|
166
166
|
* @param {handler} handler - The callback function to run on event.
|
|
167
|
-
* @returns {handler} - The wrapped version of the handler.
|
|
167
|
+
* @returns {handler[]} - The wrapped version of the handler.
|
|
168
168
|
*/
|
|
169
169
|
appendListenerOnce(event, handler) {
|
|
170
170
|
return this.#events.appendListenerOnce(event, handler);
|
|
@@ -172,7 +172,7 @@ class TinyAdvancedRaffle {
|
|
|
172
172
|
/**
|
|
173
173
|
* Adds a event listener.
|
|
174
174
|
*
|
|
175
|
-
* @param {string} event - Event name, such as 'onScrollBoundary' or 'onAutoScroll'.
|
|
175
|
+
* @param {string|string[]} event - Event name, such as 'onScrollBoundary' or 'onAutoScroll'.
|
|
176
176
|
* @param {handler} handler - Callback function to be called when event fires.
|
|
177
177
|
*/
|
|
178
178
|
on(event, handler) {
|
|
@@ -181,9 +181,9 @@ class TinyAdvancedRaffle {
|
|
|
181
181
|
/**
|
|
182
182
|
* Registers an event listener that runs only once, then is removed.
|
|
183
183
|
*
|
|
184
|
-
* @param {string} event - Event name, such as 'onScrollBoundary' or 'onAutoScroll'.
|
|
184
|
+
* @param {string|string[]} event - Event name, such as 'onScrollBoundary' or 'onAutoScroll'.
|
|
185
185
|
* @param {handler} handler - The callback function to run on event.
|
|
186
|
-
* @returns {handler} - The wrapped version of the handler.
|
|
186
|
+
* @returns {handler[]} - The wrapped version of the handler.
|
|
187
187
|
*/
|
|
188
188
|
once(event, handler) {
|
|
189
189
|
return this.#events.once(event, handler);
|
|
@@ -192,7 +192,7 @@ class TinyAdvancedRaffle {
|
|
|
192
192
|
/**
|
|
193
193
|
* Removes a previously registered event listener.
|
|
194
194
|
*
|
|
195
|
-
* @param {string} event - The name of the event to remove the handler from.
|
|
195
|
+
* @param {string|string[]} event - The name of the event to remove the handler from.
|
|
196
196
|
* @param {handler} handler - The specific callback function to remove.
|
|
197
197
|
*/
|
|
198
198
|
off(event, handler) {
|
|
@@ -201,7 +201,7 @@ class TinyAdvancedRaffle {
|
|
|
201
201
|
/**
|
|
202
202
|
* Removes all event listeners of a specific type from the element.
|
|
203
203
|
*
|
|
204
|
-
* @param {string} event - The event type to remove (e.g. 'onScrollBoundary').
|
|
204
|
+
* @param {string|string[]} event - The event type to remove (e.g. 'onScrollBoundary').
|
|
205
205
|
*/
|
|
206
206
|
offAll(event) {
|
|
207
207
|
return this.#events.offAll(event);
|
|
@@ -34,6 +34,19 @@ class TinyEvents {
|
|
|
34
34
|
/** @type {boolean} */
|
|
35
35
|
#throwMaxListeners = false;
|
|
36
36
|
|
|
37
|
+
/**
|
|
38
|
+
* Normalizes the event parameter into an array of strings.
|
|
39
|
+
*
|
|
40
|
+
* @param {string|string[]} event
|
|
41
|
+
* @param {string} method
|
|
42
|
+
* @returns {string[]}
|
|
43
|
+
*/
|
|
44
|
+
#normalizeEvents(event, method) {
|
|
45
|
+
if (typeof event === 'string') return [event];
|
|
46
|
+
if (Array.isArray(event) && event.every((e) => typeof e === 'string')) return event;
|
|
47
|
+
throw new TypeError(`${method}(event): event must be a string or string[]`);
|
|
48
|
+
}
|
|
49
|
+
|
|
37
50
|
/**
|
|
38
51
|
* Enables or disables throwing an error when the maximum number of listeners is exceeded.
|
|
39
52
|
*
|
|
@@ -85,38 +98,39 @@ class TinyEvents {
|
|
|
85
98
|
/**
|
|
86
99
|
* Adds a listener to the beginning of the listeners array for the specified event.
|
|
87
100
|
*
|
|
88
|
-
* @param {string} event - Event name.
|
|
101
|
+
* @param {string|string[]} event - Event name.
|
|
89
102
|
* @param {handler} handler - The callback function.
|
|
90
103
|
*/
|
|
91
104
|
prependListener(event, handler) {
|
|
92
|
-
|
|
93
|
-
throw new TypeError('prepend(event, handler): event name must be a string');
|
|
105
|
+
const events = this.#normalizeEvents(event, 'prependListener');
|
|
94
106
|
if (typeof handler !== 'function')
|
|
95
|
-
throw new TypeError('
|
|
96
|
-
this.#prepend(
|
|
107
|
+
throw new TypeError('prependListener(event, handler): handler must be a function');
|
|
108
|
+
for (const ev of events) this.#prepend(ev, handler);
|
|
97
109
|
}
|
|
98
110
|
|
|
99
111
|
/**
|
|
100
112
|
* Adds a one-time listener to the beginning of the listeners array for the specified event.
|
|
101
113
|
*
|
|
102
|
-
* @param {string} event - Event name.
|
|
114
|
+
* @param {string|string[]} event - Event name.
|
|
103
115
|
* @param {handler} handler - The callback function.
|
|
104
|
-
* @returns {handler} - The wrapped handler used internally.
|
|
116
|
+
* @returns {handler[]} - The wrapped handler used internally.
|
|
105
117
|
*/
|
|
106
118
|
prependListenerOnce(event, handler) {
|
|
107
|
-
|
|
108
|
-
throw new TypeError('prependOnceListener(event, handler): event name must be a string');
|
|
119
|
+
const events = this.#normalizeEvents(event, 'prependListenerOnce');
|
|
109
120
|
if (typeof handler !== 'function')
|
|
110
|
-
throw new TypeError('
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
const
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
121
|
+
throw new TypeError('prependListenerOnce(event, handler): handler must be a function');
|
|
122
|
+
|
|
123
|
+
const wrappedHandlers = [];
|
|
124
|
+
for (const ev of events) {
|
|
125
|
+
/** @type {handler} */
|
|
126
|
+
const wrapped = (...args) => {
|
|
127
|
+
this.off(ev, wrapped);
|
|
128
|
+
handler(...args);
|
|
129
|
+
};
|
|
130
|
+
this.#prepend(ev, wrapped, { once: true });
|
|
131
|
+
wrappedHandlers.push(wrapped);
|
|
132
|
+
}
|
|
133
|
+
return wrappedHandlers;
|
|
120
134
|
}
|
|
121
135
|
|
|
122
136
|
////////////////////////////////////////////////////////////
|
|
@@ -124,38 +138,39 @@ class TinyEvents {
|
|
|
124
138
|
/**
|
|
125
139
|
* Adds a event listener.
|
|
126
140
|
*
|
|
127
|
-
* @param {string} event - Event name, such as 'onScrollBoundary' or 'onAutoScroll'.
|
|
141
|
+
* @param {string|string[]} event - Event name, such as 'onScrollBoundary' or 'onAutoScroll'.
|
|
128
142
|
* @param {handler} handler - Callback function to be called when event fires.
|
|
129
143
|
* @param {Object} [settings={}] - Optional settings.
|
|
130
144
|
* @param {boolean} [settings.once=false] - This is a once event.
|
|
131
145
|
*/
|
|
132
146
|
#on(event, handler, { once = false } = {}) {
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
eventData =
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
const
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
+
const events = this.#normalizeEvents(event, 'on');
|
|
148
|
+
for (const ev of events) {
|
|
149
|
+
let eventData = this.#listeners.get(ev);
|
|
150
|
+
if (!Array.isArray(eventData)) {
|
|
151
|
+
eventData = [];
|
|
152
|
+
this.#listeners.set(ev, eventData);
|
|
153
|
+
}
|
|
154
|
+
eventData.push({ handler, config: { once } });
|
|
155
|
+
// Warn if listener count exceeds the max allowed
|
|
156
|
+
const max = this.#maxListeners;
|
|
157
|
+
if (max > 0 && eventData.length > max) {
|
|
158
|
+
const warnMessage =
|
|
159
|
+
`Possible memory leak detected. ${eventData.length} "${ev}" listeners added. ` +
|
|
160
|
+
`Use setMaxListeners() to increase limit.`;
|
|
161
|
+
if (!this.#throwMaxListeners) console.warn(warnMessage);
|
|
162
|
+
else throw new Error(warnMessage);
|
|
163
|
+
}
|
|
147
164
|
}
|
|
148
165
|
}
|
|
149
166
|
|
|
150
167
|
/**
|
|
151
168
|
* Adds a event listener.
|
|
152
169
|
*
|
|
153
|
-
* @param {string} event - Event name, such as 'onScrollBoundary' or 'onAutoScroll'.
|
|
170
|
+
* @param {string|string[]} event - Event name, such as 'onScrollBoundary' or 'onAutoScroll'.
|
|
154
171
|
* @param {handler} handler - Callback function to be called when event fires.
|
|
155
172
|
*/
|
|
156
173
|
on(event, handler) {
|
|
157
|
-
if (typeof event !== 'string')
|
|
158
|
-
throw new TypeError('on(event, handler): event name must be a string');
|
|
159
174
|
if (typeof handler !== 'function')
|
|
160
175
|
throw new TypeError('on(event, handler): handler must be a function');
|
|
161
176
|
return this.#on(event, handler);
|
|
@@ -164,28 +179,32 @@ class TinyEvents {
|
|
|
164
179
|
/**
|
|
165
180
|
* Registers an event listener that runs only once, then is removed.
|
|
166
181
|
*
|
|
167
|
-
* @param {string} event - Event name, such as 'onScrollBoundary' or 'onAutoScroll'.
|
|
182
|
+
* @param {string|string[]} event - Event name, such as 'onScrollBoundary' or 'onAutoScroll'.
|
|
168
183
|
* @param {handler} handler - The callback function to run on event.
|
|
169
|
-
* @returns {handler} - The wrapped version of the handler.
|
|
184
|
+
* @returns {handler[]} - The wrapped version of the handler.
|
|
170
185
|
*/
|
|
171
186
|
once(event, handler) {
|
|
172
|
-
|
|
187
|
+
const events = this.#normalizeEvents(event, 'once');
|
|
173
188
|
if (typeof handler !== 'function')
|
|
174
189
|
throw new TypeError('once(event, handler): handler must be a function');
|
|
175
190
|
|
|
176
|
-
|
|
177
|
-
const
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
191
|
+
const wrappedHandlers = [];
|
|
192
|
+
for (const ev of events) {
|
|
193
|
+
/** @type {handler} */
|
|
194
|
+
const wrapped = (...args) => {
|
|
195
|
+
this.off(ev, wrapped);
|
|
196
|
+
handler(...args);
|
|
197
|
+
};
|
|
198
|
+
this.#on(ev, wrapped, { once: true });
|
|
199
|
+
wrappedHandlers.push(wrapped);
|
|
200
|
+
}
|
|
201
|
+
return wrappedHandlers;
|
|
183
202
|
}
|
|
184
203
|
|
|
185
204
|
/**
|
|
186
205
|
* Adds a event listener.
|
|
187
206
|
*
|
|
188
|
-
* @param {string} event - Event name, such as 'onScrollBoundary' or 'onAutoScroll'.
|
|
207
|
+
* @param {string|string[]} event - Event name, such as 'onScrollBoundary' or 'onAutoScroll'.
|
|
189
208
|
* @param {handler} handler - Callback function to be called when event fires.
|
|
190
209
|
*/
|
|
191
210
|
appendListener(event, handler) {
|
|
@@ -195,9 +214,9 @@ class TinyEvents {
|
|
|
195
214
|
/**
|
|
196
215
|
* Registers an event listener that runs only once, then is removed.
|
|
197
216
|
*
|
|
198
|
-
* @param {string} event - Event name, such as 'onScrollBoundary' or 'onAutoScroll'.
|
|
217
|
+
* @param {string|string[]} event - Event name, such as 'onScrollBoundary' or 'onAutoScroll'.
|
|
199
218
|
* @param {handler} handler - The callback function to run on event.
|
|
200
|
-
* @returns {handler} - The wrapped version of the handler.
|
|
219
|
+
* @returns {handler[]} - The wrapped version of the handler.
|
|
201
220
|
*/
|
|
202
221
|
appendListenerOnce(event, handler) {
|
|
203
222
|
return this.once(event, handler);
|
|
@@ -208,33 +227,34 @@ class TinyEvents {
|
|
|
208
227
|
/**
|
|
209
228
|
* Removes a previously registered event listener.
|
|
210
229
|
*
|
|
211
|
-
* @param {string} event - The name of the event to remove the handler from.
|
|
230
|
+
* @param {string|string[]} event - The name of the event to remove the handler from.
|
|
212
231
|
* @param {handler} handler - The specific callback function to remove.
|
|
213
232
|
*/
|
|
214
233
|
off(event, handler) {
|
|
215
|
-
|
|
216
|
-
throw new TypeError('off(event, handler): event name must be a string');
|
|
234
|
+
const events = this.#normalizeEvents(event, 'off');
|
|
217
235
|
if (typeof handler !== 'function')
|
|
218
236
|
throw new TypeError('off(event, handler): handler must be a function');
|
|
219
237
|
|
|
220
|
-
const
|
|
221
|
-
|
|
238
|
+
for (const ev of events) {
|
|
239
|
+
const listeners = this.#listeners.get(ev);
|
|
240
|
+
if (!Array.isArray(listeners)) continue;
|
|
222
241
|
|
|
223
|
-
|
|
224
|
-
|
|
242
|
+
const index = listeners.findIndex((listener) => listener.handler === handler);
|
|
243
|
+
if (index !== -1) listeners.splice(index, 1);
|
|
225
244
|
|
|
226
|
-
|
|
227
|
-
|
|
245
|
+
// Optionally clean up empty arrays (optional)
|
|
246
|
+
if (listeners.length === 0) this.#listeners.delete(ev);
|
|
247
|
+
}
|
|
228
248
|
}
|
|
229
249
|
|
|
230
250
|
/**
|
|
231
251
|
* Removes all event listeners of a specific type from the element.
|
|
232
252
|
*
|
|
233
|
-
* @param {string} event - The event type to remove (e.g. 'onScrollBoundary').
|
|
253
|
+
* @param {string|string[]} event - The event type to remove (e.g. 'onScrollBoundary').
|
|
234
254
|
*/
|
|
235
255
|
offAll(event) {
|
|
236
|
-
|
|
237
|
-
this.#listeners.delete(
|
|
256
|
+
const events = this.#normalizeEvents(event, 'offAll');
|
|
257
|
+
for (const ev of events) this.#listeners.delete(ev);
|
|
238
258
|
}
|
|
239
259
|
|
|
240
260
|
/**
|
|
@@ -321,20 +341,25 @@ class TinyEvents {
|
|
|
321
341
|
/**
|
|
322
342
|
* Emits an event, triggering all registered handlers for that event.
|
|
323
343
|
*
|
|
324
|
-
* @param {string} event - The event name to emit.
|
|
344
|
+
* @param {string|string[]} event - The event name to emit.
|
|
325
345
|
* @param {...any} payload - Optional data to pass to each handler.
|
|
326
|
-
* @returns {boolean} True if any listeners were called, false otherwise.
|
|
346
|
+
* @returns {boolean[]} True if any listeners were called, false otherwise.
|
|
327
347
|
*/
|
|
328
348
|
emit(event, ...payload) {
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
349
|
+
const events = this.#normalizeEvents(event, 'emit');
|
|
350
|
+
const called = [];
|
|
351
|
+
for (const ev of events) {
|
|
352
|
+
const listeners = this.#listeners.get(ev);
|
|
353
|
+
if (!Array.isArray(listeners) || listeners.length === 0) {
|
|
354
|
+
called.push(false);
|
|
355
|
+
continue;
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
// Call all listeners with the provided data
|
|
359
|
+
listeners.forEach((listener) => listener.handler(...payload));
|
|
360
|
+
called.push(true);
|
|
361
|
+
}
|
|
362
|
+
return called;
|
|
338
363
|
}
|
|
339
364
|
|
|
340
365
|
///////////////////////////////////
|
|
@@ -42,61 +42,61 @@ declare class TinyEvents {
|
|
|
42
42
|
/**
|
|
43
43
|
* Adds a listener to the beginning of the listeners array for the specified event.
|
|
44
44
|
*
|
|
45
|
-
* @param {string} event - Event name.
|
|
45
|
+
* @param {string|string[]} event - Event name.
|
|
46
46
|
* @param {handler} handler - The callback function.
|
|
47
47
|
*/
|
|
48
|
-
prependListener(event: string, handler: handler): void;
|
|
48
|
+
prependListener(event: string | string[], handler: handler): void;
|
|
49
49
|
/**
|
|
50
50
|
* Adds a one-time listener to the beginning of the listeners array for the specified event.
|
|
51
51
|
*
|
|
52
|
-
* @param {string} event - Event name.
|
|
52
|
+
* @param {string|string[]} event - Event name.
|
|
53
53
|
* @param {handler} handler - The callback function.
|
|
54
|
-
* @returns {handler} - The wrapped handler used internally.
|
|
54
|
+
* @returns {handler[]} - The wrapped handler used internally.
|
|
55
55
|
*/
|
|
56
|
-
prependListenerOnce(event: string, handler: handler): handler;
|
|
56
|
+
prependListenerOnce(event: string | string[], handler: handler): handler[];
|
|
57
57
|
/**
|
|
58
58
|
* Adds a event listener.
|
|
59
59
|
*
|
|
60
|
-
* @param {string} event - Event name, such as 'onScrollBoundary' or 'onAutoScroll'.
|
|
60
|
+
* @param {string|string[]} event - Event name, such as 'onScrollBoundary' or 'onAutoScroll'.
|
|
61
61
|
* @param {handler} handler - Callback function to be called when event fires.
|
|
62
62
|
*/
|
|
63
|
-
on(event: string, handler: handler): void;
|
|
63
|
+
on(event: string | string[], handler: handler): void;
|
|
64
64
|
/**
|
|
65
65
|
* Registers an event listener that runs only once, then is removed.
|
|
66
66
|
*
|
|
67
|
-
* @param {string} event - Event name, such as 'onScrollBoundary' or 'onAutoScroll'.
|
|
67
|
+
* @param {string|string[]} event - Event name, such as 'onScrollBoundary' or 'onAutoScroll'.
|
|
68
68
|
* @param {handler} handler - The callback function to run on event.
|
|
69
|
-
* @returns {handler} - The wrapped version of the handler.
|
|
69
|
+
* @returns {handler[]} - The wrapped version of the handler.
|
|
70
70
|
*/
|
|
71
|
-
once(event: string, handler: handler): handler;
|
|
71
|
+
once(event: string | string[], handler: handler): handler[];
|
|
72
72
|
/**
|
|
73
73
|
* Adds a event listener.
|
|
74
74
|
*
|
|
75
|
-
* @param {string} event - Event name, such as 'onScrollBoundary' or 'onAutoScroll'.
|
|
75
|
+
* @param {string|string[]} event - Event name, such as 'onScrollBoundary' or 'onAutoScroll'.
|
|
76
76
|
* @param {handler} handler - Callback function to be called when event fires.
|
|
77
77
|
*/
|
|
78
|
-
appendListener(event: string, handler: handler): void;
|
|
78
|
+
appendListener(event: string | string[], handler: handler): void;
|
|
79
79
|
/**
|
|
80
80
|
* Registers an event listener that runs only once, then is removed.
|
|
81
81
|
*
|
|
82
|
-
* @param {string} event - Event name, such as 'onScrollBoundary' or 'onAutoScroll'.
|
|
82
|
+
* @param {string|string[]} event - Event name, such as 'onScrollBoundary' or 'onAutoScroll'.
|
|
83
83
|
* @param {handler} handler - The callback function to run on event.
|
|
84
|
-
* @returns {handler} - The wrapped version of the handler.
|
|
84
|
+
* @returns {handler[]} - The wrapped version of the handler.
|
|
85
85
|
*/
|
|
86
|
-
appendListenerOnce(event: string, handler: handler): handler;
|
|
86
|
+
appendListenerOnce(event: string | string[], handler: handler): handler[];
|
|
87
87
|
/**
|
|
88
88
|
* Removes a previously registered event listener.
|
|
89
89
|
*
|
|
90
|
-
* @param {string} event - The name of the event to remove the handler from.
|
|
90
|
+
* @param {string|string[]} event - The name of the event to remove the handler from.
|
|
91
91
|
* @param {handler} handler - The specific callback function to remove.
|
|
92
92
|
*/
|
|
93
|
-
off(event: string, handler: handler): void;
|
|
93
|
+
off(event: string | string[], handler: handler): void;
|
|
94
94
|
/**
|
|
95
95
|
* Removes all event listeners of a specific type from the element.
|
|
96
96
|
*
|
|
97
|
-
* @param {string} event - The event type to remove (e.g. 'onScrollBoundary').
|
|
97
|
+
* @param {string|string[]} event - The event type to remove (e.g. 'onScrollBoundary').
|
|
98
98
|
*/
|
|
99
|
-
offAll(event: string): void;
|
|
99
|
+
offAll(event: string | string[]): void;
|
|
100
100
|
/**
|
|
101
101
|
* Removes all event listeners of all types from the element.
|
|
102
102
|
*/
|
|
@@ -138,11 +138,11 @@ declare class TinyEvents {
|
|
|
138
138
|
/**
|
|
139
139
|
* Emits an event, triggering all registered handlers for that event.
|
|
140
140
|
*
|
|
141
|
-
* @param {string} event - The event name to emit.
|
|
141
|
+
* @param {string|string[]} event - The event name to emit.
|
|
142
142
|
* @param {...any} payload - Optional data to pass to each handler.
|
|
143
|
-
* @returns {boolean} True if any listeners were called, false otherwise.
|
|
143
|
+
* @returns {boolean[]} True if any listeners were called, false otherwise.
|
|
144
144
|
*/
|
|
145
|
-
emit(event: string, ...payload: any[]): boolean;
|
|
145
|
+
emit(event: string | string[], ...payload: any[]): boolean[];
|
|
146
146
|
/**
|
|
147
147
|
* Sets the maximum number of listeners per event before a warning is shown.
|
|
148
148
|
*
|