tiny-essentials 1.27.1 → 1.28.0
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/README.md +1 -0
- package/changelog/1/28/0.md +16 -0
- package/dist/v1/TinyAdvancedRaffle.min.js +1 -1
- package/dist/v1/TinyBasicsEs.min.js +1 -2
- package/dist/v1/TinyEssentials.min.js +1 -2
- package/dist/v1/TinyIframeEvents.min.js +1 -1
- package/dist/v1/TinyLocalStorage.min.js +1 -1
- package/dist/v1/TinyMediaPlayer.min.js +1 -1
- package/dist/v1/TinyNewWinEvents.min.js +1 -1
- package/dist/v1/TinyRadioFm.min.js +1 -1
- package/dist/v1/TinySmartScroller.min.js +1 -1
- package/dist/v1/TinyUploadClicker.min.js +1 -2
- package/dist/v1/basics/extendObjType/Buffer.cjs +13 -0
- package/dist/v1/basics/extendObjType/Buffer.d.mts +4 -0
- package/dist/v1/basics/extendObjType/Buffer.mjs +8 -0
- package/dist/v1/basics/objFilter.cjs +0 -11
- package/dist/v1/basics/objFilter.mjs +0 -10
- package/dist/v1/index.cjs +0 -2
- package/dist/v1/index.d.mts +1 -2
- package/dist/v1/index.mjs +1 -2
- package/dist/v1/libs/TinyAdvancedRaffle.cjs +7 -205
- package/dist/v1/libs/TinyAdvancedRaffle.d.mts +2 -121
- package/dist/v1/libs/TinyAdvancedRaffle.mjs +7 -179
- package/dist/v1/libs/TinyIframeEvents.cjs +8 -195
- package/dist/v1/libs/TinyIframeEvents.d.mts +3 -122
- package/dist/v1/libs/TinyIframeEvents.mjs +8 -170
- package/dist/v1/libs/TinyLocalStorage.cjs +5 -205
- package/dist/v1/libs/TinyLocalStorage.d.mts +3 -130
- package/dist/v1/libs/TinyLocalStorage.mjs +5 -178
- package/dist/v1/libs/TinyMediaPlayer.cjs +2 -2
- package/dist/v1/libs/TinyMediaPlayer.d.mts +2 -2
- package/dist/v1/libs/TinyMediaPlayer.mjs +2 -2
- package/dist/v1/libs/TinyNewWinEvents.cjs +10 -197
- package/dist/v1/libs/TinyNewWinEvents.d.mts +3 -122
- package/dist/v1/libs/TinyNewWinEvents.mjs +10 -172
- package/dist/v1/libs/TinyRadioFm.cjs +2 -3
- package/dist/v1/libs/TinyRadioFm.d.mts +2 -3
- package/dist/v1/libs/TinyRadioFm.mjs +2 -3
- package/dist/v1/libs/TinySmartScroller.cjs +4 -202
- package/dist/v1/libs/TinySmartScroller.d.mts +8 -143
- package/dist/v1/libs/TinySmartScroller.mjs +4 -176
- package/dist/v1/libs/TinyUploadClicker.cjs +1 -0
- package/docs/v1/Exports.md +0 -1
- package/docs/v1/README.md +0 -1
- package/docs/v1/basics/objFilter.md +1 -1
- package/docs/v1/libs/TinyIframeEvents.md +8 -8
- package/docs/v1/libs/TinyLocalStorage.md +3 -3
- package/docs/v1/libs/TinyNewWinEvents.md +8 -9
- package/package.json +6 -5
- package/dist/v1/TinyBasicsEs.min.js.LICENSE.txt +0 -8
- package/dist/v1/TinyEssentials.min.js.LICENSE.txt +0 -8
- package/dist/v1/TinyEvents.min.js +0 -1
- package/dist/v1/TinyUploadClicker.min.js.LICENSE.txt +0 -8
- package/dist/v1/build/TinyEvents.cjs +0 -7
- package/dist/v1/build/TinyEvents.d.mts +0 -3
- package/dist/v1/build/TinyEvents.mjs +0 -2
- package/dist/v1/libs/TinyEvents.cjs +0 -395
- package/dist/v1/libs/TinyEvents.d.mts +0 -164
- package/dist/v1/libs/TinyEvents.mjs +0 -360
- package/docs/v1/libs/TinyEvents.md +0 -187
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { EventEmitter } from 'events';
|
|
2
2
|
/**
|
|
3
3
|
* Defines the available normalization strategies for probability weight calculations.
|
|
4
4
|
* @typedef {'relative' | 'softmax'} Normalization
|
|
@@ -101,180 +101,7 @@ import TinyEvents from './TinyEvents.mjs';
|
|
|
101
101
|
*
|
|
102
102
|
* @class
|
|
103
103
|
*/
|
|
104
|
-
class TinyAdvancedRaffle {
|
|
105
|
-
#events = new TinyEvents();
|
|
106
|
-
/**
|
|
107
|
-
* Emits an event, triggering all registered handlers for that event.
|
|
108
|
-
*
|
|
109
|
-
* @param {string|string[]} event - The event name to emit.
|
|
110
|
-
* @param {...any} payload - Optional data to pass to each handler.
|
|
111
|
-
* @returns {boolean[]} True if any listeners were called, false otherwise.
|
|
112
|
-
*/
|
|
113
|
-
#emit(event, ...payload) {
|
|
114
|
-
return this.#events.emit(event, ...payload);
|
|
115
|
-
}
|
|
116
|
-
/**
|
|
117
|
-
* Enables or disables throwing an error when the maximum number of listeners is exceeded.
|
|
118
|
-
*
|
|
119
|
-
* @param {boolean} shouldThrow - If true, an error will be thrown when the max is exceeded.
|
|
120
|
-
*/
|
|
121
|
-
setThrowOnMaxListeners(shouldThrow) {
|
|
122
|
-
return this.#events.setThrowOnMaxListeners(shouldThrow);
|
|
123
|
-
}
|
|
124
|
-
/**
|
|
125
|
-
* Checks whether an error will be thrown when the max listener limit is exceeded.
|
|
126
|
-
*
|
|
127
|
-
* @returns {boolean} True if an error will be thrown, false if only a warning is shown.
|
|
128
|
-
*/
|
|
129
|
-
getThrowOnMaxListeners() {
|
|
130
|
-
return this.#events.getThrowOnMaxListeners();
|
|
131
|
-
}
|
|
132
|
-
/////////////////////////////////////////////////////////////
|
|
133
|
-
/**
|
|
134
|
-
* Adds a listener to the beginning of the listeners array for the specified event.
|
|
135
|
-
*
|
|
136
|
-
* @param {string|string[]} event - Event name.
|
|
137
|
-
* @param {handler} handler - The callback function.
|
|
138
|
-
*/
|
|
139
|
-
prependListener(event, handler) {
|
|
140
|
-
return this.#events.prependListener(event, handler);
|
|
141
|
-
}
|
|
142
|
-
/**
|
|
143
|
-
* Adds a one-time listener to the beginning of the listeners array for the specified event.
|
|
144
|
-
*
|
|
145
|
-
* @param {string|string[]} event - Event name.
|
|
146
|
-
* @param {handler} handler - The callback function.
|
|
147
|
-
* @returns {handler[]} - The wrapped handler used internally.
|
|
148
|
-
*/
|
|
149
|
-
prependListenerOnce(event, handler) {
|
|
150
|
-
return this.#events.prependListenerOnce(event, handler);
|
|
151
|
-
}
|
|
152
|
-
//////////////////////////////////////////////////////////////////////
|
|
153
|
-
/**
|
|
154
|
-
* Adds a event listener.
|
|
155
|
-
*
|
|
156
|
-
* @param {string|string[]} event - Event name, such as 'onScrollBoundary' or 'onAutoScroll'.
|
|
157
|
-
* @param {handler} handler - Callback function to be called when event fires.
|
|
158
|
-
*/
|
|
159
|
-
appendListener(event, handler) {
|
|
160
|
-
return this.#events.appendListener(event, handler);
|
|
161
|
-
}
|
|
162
|
-
/**
|
|
163
|
-
* Registers an event listener that runs only once, then is removed.
|
|
164
|
-
*
|
|
165
|
-
* @param {string|string[]} event - Event name, such as 'onScrollBoundary' or 'onAutoScroll'.
|
|
166
|
-
* @param {handler} handler - The callback function to run on event.
|
|
167
|
-
* @returns {handler[]} - The wrapped version of the handler.
|
|
168
|
-
*/
|
|
169
|
-
appendListenerOnce(event, handler) {
|
|
170
|
-
return this.#events.appendListenerOnce(event, handler);
|
|
171
|
-
}
|
|
172
|
-
/**
|
|
173
|
-
* Adds a event listener.
|
|
174
|
-
*
|
|
175
|
-
* @param {string|string[]} event - Event name, such as 'onScrollBoundary' or 'onAutoScroll'.
|
|
176
|
-
* @param {handler} handler - Callback function to be called when event fires.
|
|
177
|
-
*/
|
|
178
|
-
on(event, handler) {
|
|
179
|
-
return this.#events.on(event, handler);
|
|
180
|
-
}
|
|
181
|
-
/**
|
|
182
|
-
* Registers an event listener that runs only once, then is removed.
|
|
183
|
-
*
|
|
184
|
-
* @param {string|string[]} event - Event name, such as 'onScrollBoundary' or 'onAutoScroll'.
|
|
185
|
-
* @param {handler} handler - The callback function to run on event.
|
|
186
|
-
* @returns {handler[]} - The wrapped version of the handler.
|
|
187
|
-
*/
|
|
188
|
-
once(event, handler) {
|
|
189
|
-
return this.#events.once(event, handler);
|
|
190
|
-
}
|
|
191
|
-
////////////////////////////////////////////////////////////////////
|
|
192
|
-
/**
|
|
193
|
-
* Removes a previously registered event listener.
|
|
194
|
-
*
|
|
195
|
-
* @param {string|string[]} event - The name of the event to remove the handler from.
|
|
196
|
-
* @param {handler} handler - The specific callback function to remove.
|
|
197
|
-
*/
|
|
198
|
-
off(event, handler) {
|
|
199
|
-
return this.#events.off(event, handler);
|
|
200
|
-
}
|
|
201
|
-
/**
|
|
202
|
-
* Removes all event listeners of a specific type from the element.
|
|
203
|
-
*
|
|
204
|
-
* @param {string|string[]} event - The event type to remove (e.g. 'onScrollBoundary').
|
|
205
|
-
*/
|
|
206
|
-
offAll(event) {
|
|
207
|
-
return this.#events.offAll(event);
|
|
208
|
-
}
|
|
209
|
-
/**
|
|
210
|
-
* Removes all event listeners of all types from the element.
|
|
211
|
-
*/
|
|
212
|
-
offAllTypes() {
|
|
213
|
-
return this.#events.offAllTypes();
|
|
214
|
-
}
|
|
215
|
-
////////////////////////////////////////////////////////////
|
|
216
|
-
/**
|
|
217
|
-
* Returns the number of listeners for a given event.
|
|
218
|
-
*
|
|
219
|
-
* @param {string} event - The name of the event.
|
|
220
|
-
* @returns {number} Number of listeners for the event.
|
|
221
|
-
*/
|
|
222
|
-
listenerCount(event) {
|
|
223
|
-
return this.#events.listenerCount(event);
|
|
224
|
-
}
|
|
225
|
-
/**
|
|
226
|
-
* Returns a copy of the array of listeners for the specified event.
|
|
227
|
-
*
|
|
228
|
-
* @param {string} event - The name of the event.
|
|
229
|
-
* @returns {handler[]} Array of listener functions.
|
|
230
|
-
*/
|
|
231
|
-
listeners(event) {
|
|
232
|
-
return this.#events.listeners(event);
|
|
233
|
-
}
|
|
234
|
-
/**
|
|
235
|
-
* Returns a copy of the array of listeners for the specified event.
|
|
236
|
-
*
|
|
237
|
-
* @param {string} event - The name of the event.
|
|
238
|
-
* @returns {handler[]} Array of listener functions.
|
|
239
|
-
*/
|
|
240
|
-
onceListeners(event) {
|
|
241
|
-
return this.#events.onceListeners(event);
|
|
242
|
-
}
|
|
243
|
-
/**
|
|
244
|
-
* Returns a copy of the internal listeners array for the specified event,
|
|
245
|
-
* including wrapper functions like those used by `.once()`.
|
|
246
|
-
* @param {string | symbol} event - The event name.
|
|
247
|
-
* @returns {handler[]} An array of raw listener functions.
|
|
248
|
-
*/
|
|
249
|
-
allListeners(event) {
|
|
250
|
-
return this.#events.allListeners(event);
|
|
251
|
-
}
|
|
252
|
-
/**
|
|
253
|
-
* Returns an array of event names for which there are registered listeners.
|
|
254
|
-
*
|
|
255
|
-
* @returns {string[]} Array of registered event names.
|
|
256
|
-
*/
|
|
257
|
-
eventNames() {
|
|
258
|
-
return this.#events.eventNames();
|
|
259
|
-
}
|
|
260
|
-
//////////////////////////////////////////////////////
|
|
261
|
-
/**
|
|
262
|
-
* Sets the maximum number of listeners per event before a warning is shown.
|
|
263
|
-
*
|
|
264
|
-
* @param {number} n - The maximum number of listeners.
|
|
265
|
-
*/
|
|
266
|
-
setMaxListeners(n) {
|
|
267
|
-
return this.#events.setMaxListeners(n);
|
|
268
|
-
}
|
|
269
|
-
/**
|
|
270
|
-
* Gets the maximum number of listeners allowed per event.
|
|
271
|
-
*
|
|
272
|
-
* @returns {number} The maximum number of listeners.
|
|
273
|
-
*/
|
|
274
|
-
getMaxListeners() {
|
|
275
|
-
return this.#events.getMaxListeners();
|
|
276
|
-
}
|
|
277
|
-
///////////////////////////////////////////////////
|
|
104
|
+
class TinyAdvancedRaffle extends EventEmitter {
|
|
278
105
|
/**
|
|
279
106
|
* Whether this instance has been destroyed.
|
|
280
107
|
* @type {boolean}
|
|
@@ -582,6 +409,7 @@ class TinyAdvancedRaffle {
|
|
|
582
409
|
* @param {Normalization} [opts.normalization='relative'] - Probability normalization mode.
|
|
583
410
|
*/
|
|
584
411
|
constructor(opts = {}) {
|
|
412
|
+
super();
|
|
585
413
|
const { rng = null, seed = null, normalization = 'relative' } = opts;
|
|
586
414
|
this.#normalization = normalization;
|
|
587
415
|
this.#seed = seed;
|
|
@@ -661,7 +489,7 @@ class TinyAdvancedRaffle {
|
|
|
661
489
|
// Register in groups map
|
|
662
490
|
for (const g of groups)
|
|
663
491
|
this._ensureGroup(g).add(id);
|
|
664
|
-
this
|
|
492
|
+
this.emit('itemAdded', entry);
|
|
665
493
|
return entry;
|
|
666
494
|
}
|
|
667
495
|
/**
|
|
@@ -683,7 +511,7 @@ class TinyAdvancedRaffle {
|
|
|
683
511
|
s.delete(id);
|
|
684
512
|
}
|
|
685
513
|
this.#items.delete(id);
|
|
686
|
-
this
|
|
514
|
+
this.emit('itemRemoved', id);
|
|
687
515
|
this.resetFreq(id);
|
|
688
516
|
this.resetPity(id);
|
|
689
517
|
return true;
|
|
@@ -705,7 +533,7 @@ class TinyAdvancedRaffle {
|
|
|
705
533
|
if (!it)
|
|
706
534
|
throw new Error('Item not found');
|
|
707
535
|
it.baseWeight = Math.max(0, Number(weight) || 0);
|
|
708
|
-
this
|
|
536
|
+
this.emit('weightChanged', { id, weight: it.baseWeight });
|
|
709
537
|
}
|
|
710
538
|
/**
|
|
711
539
|
* Get an item by its ID.
|
|
@@ -1216,7 +1044,7 @@ class TinyAdvancedRaffle {
|
|
|
1216
1044
|
if (!item)
|
|
1217
1045
|
return null;
|
|
1218
1046
|
const result = { id: item.id, label: item.label, meta: { ...item.meta }, prob: chosen.p };
|
|
1219
|
-
this
|
|
1047
|
+
this.emit('draw', result);
|
|
1220
1048
|
// add frequence
|
|
1221
1049
|
if (result)
|
|
1222
1050
|
this.#freq.set(result.id, (this.#freq.get(result.id) || 0) + 1);
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
var events = require('events');
|
|
3
4
|
var objChecker = require('../basics/objChecker.cjs');
|
|
4
|
-
var TinyEvents = require('./TinyEvents.cjs');
|
|
5
5
|
|
|
6
6
|
/** @type {WeakMap<Window, TinyIframeEvents>} */
|
|
7
7
|
const instances = new WeakMap();
|
|
@@ -46,195 +46,7 @@ const instances = new WeakMap();
|
|
|
46
46
|
* - Queue management for messages sent before the connection is established.
|
|
47
47
|
* - Auto-reconnects and resets ports if the target iframe is reloaded.
|
|
48
48
|
*/
|
|
49
|
-
class TinyIframeEvents {
|
|
50
|
-
#events = new TinyEvents();
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* Enables or disables throwing an error when the maximum number of listeners is exceeded.
|
|
54
|
-
*
|
|
55
|
-
* @param {boolean} shouldThrow - If true, an error will be thrown when the max is exceeded.
|
|
56
|
-
*/
|
|
57
|
-
setThrowOnMaxListeners(shouldThrow) {
|
|
58
|
-
return this.#events.setThrowOnMaxListeners(shouldThrow);
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* Checks whether an error will be thrown when the max listener limit is exceeded.
|
|
63
|
-
*
|
|
64
|
-
* @returns {boolean} True if an error will be thrown, false if only a warning is shown.
|
|
65
|
-
*/
|
|
66
|
-
getThrowOnMaxListeners() {
|
|
67
|
-
return this.#events.getThrowOnMaxListeners();
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
/////////////////////////////////////////////////////////////
|
|
71
|
-
|
|
72
|
-
/**
|
|
73
|
-
* Adds a listener to the beginning of the listeners array for the specified event.
|
|
74
|
-
*
|
|
75
|
-
* @param {string|string[]} event - Event name.
|
|
76
|
-
* @param {handler} handler - The callback function.
|
|
77
|
-
*/
|
|
78
|
-
prependListener(event, handler) {
|
|
79
|
-
return this.#events.prependListener(event, handler);
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
/**
|
|
83
|
-
* Adds a one-time listener to the beginning of the listeners array for the specified event.
|
|
84
|
-
*
|
|
85
|
-
* @param {string|string[]} event - Event name.
|
|
86
|
-
* @param {handler} handler - The callback function.
|
|
87
|
-
* @returns {handler[]} - The wrapped handler used internally.
|
|
88
|
-
*/
|
|
89
|
-
prependListenerOnce(event, handler) {
|
|
90
|
-
return this.#events.prependListenerOnce(event, handler);
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
//////////////////////////////////////////////////////////////////////
|
|
94
|
-
|
|
95
|
-
/**
|
|
96
|
-
* Adds a event listener.
|
|
97
|
-
*
|
|
98
|
-
* @param {string|string[]} event - Event name, such as 'onScrollBoundary' or 'onAutoScroll'.
|
|
99
|
-
* @param {handler} handler - Callback function to be called when event fires.
|
|
100
|
-
*/
|
|
101
|
-
appendListener(event, handler) {
|
|
102
|
-
return this.#events.appendListener(event, handler);
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
/**
|
|
106
|
-
* Registers an event listener that runs only once, then is removed.
|
|
107
|
-
*
|
|
108
|
-
* @param {string|string[]} event - Event name, such as 'onScrollBoundary' or 'onAutoScroll'.
|
|
109
|
-
* @param {handler} handler - The callback function to run on event.
|
|
110
|
-
* @returns {handler[]} - The wrapped version of the handler.
|
|
111
|
-
*/
|
|
112
|
-
appendListenerOnce(event, handler) {
|
|
113
|
-
return this.#events.appendListenerOnce(event, handler);
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
/**
|
|
117
|
-
* Adds a event listener.
|
|
118
|
-
*
|
|
119
|
-
* @param {string|string[]} event - Event name, such as 'onScrollBoundary' or 'onAutoScroll'.
|
|
120
|
-
* @param {handler} handler - Callback function to be called when event fires.
|
|
121
|
-
*/
|
|
122
|
-
on(event, handler) {
|
|
123
|
-
return this.#events.on(event, handler);
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
/**
|
|
127
|
-
* Registers an event listener that runs only once, then is removed.
|
|
128
|
-
*
|
|
129
|
-
* @param {string|string[]} event - Event name, such as 'onScrollBoundary' or 'onAutoScroll'.
|
|
130
|
-
* @param {handler} handler - The callback function to run on event.
|
|
131
|
-
* @returns {handler[]} - The wrapped version of the handler.
|
|
132
|
-
*/
|
|
133
|
-
once(event, handler) {
|
|
134
|
-
return this.#events.once(event, handler);
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
////////////////////////////////////////////////////////////////////
|
|
138
|
-
|
|
139
|
-
/**
|
|
140
|
-
* Removes a previously registered event listener.
|
|
141
|
-
*
|
|
142
|
-
* @param {string|string[]} event - The name of the event to remove the handler from.
|
|
143
|
-
* @param {handler} handler - The specific callback function to remove.
|
|
144
|
-
*/
|
|
145
|
-
off(event, handler) {
|
|
146
|
-
return this.#events.off(event, handler);
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
/**
|
|
150
|
-
* Removes all event listeners of a specific type from the element.
|
|
151
|
-
*
|
|
152
|
-
* @param {string|string[]} event - The event type to remove (e.g. 'onScrollBoundary').
|
|
153
|
-
*/
|
|
154
|
-
offAll(event) {
|
|
155
|
-
return this.#events.offAll(event);
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
/**
|
|
159
|
-
* Removes all event listeners of all types from the element.
|
|
160
|
-
*/
|
|
161
|
-
offAllTypes() {
|
|
162
|
-
return this.#events.offAllTypes();
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
////////////////////////////////////////////////////////////
|
|
166
|
-
|
|
167
|
-
/**
|
|
168
|
-
* Returns the number of listeners for a given event.
|
|
169
|
-
*
|
|
170
|
-
* @param {string} event - The name of the event.
|
|
171
|
-
* @returns {number} Number of listeners for the event.
|
|
172
|
-
*/
|
|
173
|
-
listenerCount(event) {
|
|
174
|
-
return this.#events.listenerCount(event);
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
/**
|
|
178
|
-
* Returns a copy of the array of listeners for the specified event.
|
|
179
|
-
*
|
|
180
|
-
* @param {string} event - The name of the event.
|
|
181
|
-
* @returns {handler[]} Array of listener functions.
|
|
182
|
-
*/
|
|
183
|
-
listeners(event) {
|
|
184
|
-
return this.#events.listeners(event);
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
/**
|
|
188
|
-
* Returns a copy of the array of listeners for the specified event.
|
|
189
|
-
*
|
|
190
|
-
* @param {string} event - The name of the event.
|
|
191
|
-
* @returns {handler[]} Array of listener functions.
|
|
192
|
-
*/
|
|
193
|
-
onceListeners(event) {
|
|
194
|
-
return this.#events.onceListeners(event);
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
/**
|
|
198
|
-
* Returns a copy of the internal listeners array for the specified event,
|
|
199
|
-
* including wrapper functions like those used by `.once()`.
|
|
200
|
-
* @param {string | symbol} event - The event name.
|
|
201
|
-
* @returns {handler[]} An array of raw listener functions.
|
|
202
|
-
*/
|
|
203
|
-
allListeners(event) {
|
|
204
|
-
return this.#events.allListeners(event);
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
/**
|
|
208
|
-
* Returns an array of event names for which there are registered listeners.
|
|
209
|
-
*
|
|
210
|
-
* @returns {string[]} Array of registered event names.
|
|
211
|
-
*/
|
|
212
|
-
eventNames() {
|
|
213
|
-
return this.#events.eventNames();
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
//////////////////////////////////////////////////////
|
|
217
|
-
|
|
218
|
-
/**
|
|
219
|
-
* Sets the maximum number of listeners per event before a warning is shown.
|
|
220
|
-
*
|
|
221
|
-
* @param {number} n - The maximum number of listeners.
|
|
222
|
-
*/
|
|
223
|
-
setMaxListeners(n) {
|
|
224
|
-
return this.#events.setMaxListeners(n);
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
/**
|
|
228
|
-
* Gets the maximum number of listeners allowed per event.
|
|
229
|
-
*
|
|
230
|
-
* @returns {number} The maximum number of listeners.
|
|
231
|
-
*/
|
|
232
|
-
getMaxListeners() {
|
|
233
|
-
return this.#events.getMaxListeners();
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
///////////////////////////////////////////////////
|
|
237
|
-
|
|
49
|
+
class TinyIframeEvents extends events.EventEmitter {
|
|
238
50
|
/** @type {Window} */
|
|
239
51
|
#targetWindow;
|
|
240
52
|
|
|
@@ -283,7 +95,7 @@ class TinyIframeEvents {
|
|
|
283
95
|
if (this.#ready) {
|
|
284
96
|
handler();
|
|
285
97
|
} else {
|
|
286
|
-
this
|
|
98
|
+
this.once(this.#readyEventName, handler);
|
|
287
99
|
}
|
|
288
100
|
}
|
|
289
101
|
|
|
@@ -318,6 +130,7 @@ class TinyIframeEvents {
|
|
|
318
130
|
handshakeEventName = '__tinyIframeHandshake__',
|
|
319
131
|
readyEventName = '__tinyIframeReady__',
|
|
320
132
|
} = {}) {
|
|
133
|
+
super();
|
|
321
134
|
if (
|
|
322
135
|
targetIframe !== undefined &&
|
|
323
136
|
(!(targetIframe instanceof HTMLIFrameElement) || !targetIframe.contentWindow)
|
|
@@ -489,7 +302,7 @@ class TinyIframeEvents {
|
|
|
489
302
|
return;
|
|
490
303
|
}
|
|
491
304
|
|
|
492
|
-
this
|
|
305
|
+
this.emit(`win:${eventName}`, payload, event);
|
|
493
306
|
}
|
|
494
307
|
|
|
495
308
|
/**
|
|
@@ -500,7 +313,7 @@ class TinyIframeEvents {
|
|
|
500
313
|
if (this.#ready) return;
|
|
501
314
|
this.#ready = true;
|
|
502
315
|
this.#flushQueue();
|
|
503
|
-
this
|
|
316
|
+
this.emit(this.#readyEventName);
|
|
504
317
|
}
|
|
505
318
|
|
|
506
319
|
/**
|
|
@@ -525,7 +338,7 @@ class TinyIframeEvents {
|
|
|
525
338
|
* @throws {TypeError} If `eventName` is not a string.
|
|
526
339
|
* @throws {Error} If instance has been destroyed.
|
|
527
340
|
*/
|
|
528
|
-
|
|
341
|
+
winEmit(eventName, payload) {
|
|
529
342
|
if (typeof eventName !== 'string') throw new TypeError('Event name must be a string.');
|
|
530
343
|
if (this.#isDestroyed) throw new Error('Cannot emit: instance has been destroyed.');
|
|
531
344
|
|
|
@@ -573,7 +386,7 @@ class TinyIframeEvents {
|
|
|
573
386
|
this.#port = null;
|
|
574
387
|
}
|
|
575
388
|
|
|
576
|
-
this
|
|
389
|
+
this.removeAllListeners();
|
|
577
390
|
this.#pendingQueue = [];
|
|
578
391
|
instances.delete(this.#targetWindow);
|
|
579
392
|
}
|
|
@@ -86,7 +86,7 @@ export type IframeEventBase = {
|
|
|
86
86
|
* - Queue management for messages sent before the connection is established.
|
|
87
87
|
* - Auto-reconnects and resets ports if the target iframe is reloaded.
|
|
88
88
|
*/
|
|
89
|
-
declare class TinyIframeEvents {
|
|
89
|
+
declare class TinyIframeEvents extends EventEmitter<any> {
|
|
90
90
|
/**
|
|
91
91
|
* Creates a new TinyIframeEvents instance to manage secure communication.
|
|
92
92
|
* Automatically establishes a MessageChannel connection between contexts.
|
|
@@ -94,126 +94,6 @@ declare class TinyIframeEvents {
|
|
|
94
94
|
* @param {TinyIframeEventsConfig} config - Configuration object.
|
|
95
95
|
*/
|
|
96
96
|
constructor({ targetIframe, targetOrigin, secretEventName, handshakeEventName, readyEventName, }?: TinyIframeEventsConfig);
|
|
97
|
-
/**
|
|
98
|
-
* Enables or disables throwing an error when the maximum number of listeners is exceeded.
|
|
99
|
-
*
|
|
100
|
-
* @param {boolean} shouldThrow - If true, an error will be thrown when the max is exceeded.
|
|
101
|
-
*/
|
|
102
|
-
setThrowOnMaxListeners(shouldThrow: boolean): void;
|
|
103
|
-
/**
|
|
104
|
-
* Checks whether an error will be thrown when the max listener limit is exceeded.
|
|
105
|
-
*
|
|
106
|
-
* @returns {boolean} True if an error will be thrown, false if only a warning is shown.
|
|
107
|
-
*/
|
|
108
|
-
getThrowOnMaxListeners(): boolean;
|
|
109
|
-
/**
|
|
110
|
-
* Adds a listener to the beginning of the listeners array for the specified event.
|
|
111
|
-
*
|
|
112
|
-
* @param {string|string[]} event - Event name.
|
|
113
|
-
* @param {handler} handler - The callback function.
|
|
114
|
-
*/
|
|
115
|
-
prependListener(event: string | string[], handler: handler): void;
|
|
116
|
-
/**
|
|
117
|
-
* Adds a one-time listener to the beginning of the listeners array for the specified event.
|
|
118
|
-
*
|
|
119
|
-
* @param {string|string[]} event - Event name.
|
|
120
|
-
* @param {handler} handler - The callback function.
|
|
121
|
-
* @returns {handler[]} - The wrapped handler used internally.
|
|
122
|
-
*/
|
|
123
|
-
prependListenerOnce(event: string | string[], handler: handler): handler[];
|
|
124
|
-
/**
|
|
125
|
-
* Adds a event listener.
|
|
126
|
-
*
|
|
127
|
-
* @param {string|string[]} event - Event name, such as 'onScrollBoundary' or 'onAutoScroll'.
|
|
128
|
-
* @param {handler} handler - Callback function to be called when event fires.
|
|
129
|
-
*/
|
|
130
|
-
appendListener(event: string | string[], handler: handler): void;
|
|
131
|
-
/**
|
|
132
|
-
* Registers an event listener that runs only once, then is removed.
|
|
133
|
-
*
|
|
134
|
-
* @param {string|string[]} event - Event name, such as 'onScrollBoundary' or 'onAutoScroll'.
|
|
135
|
-
* @param {handler} handler - The callback function to run on event.
|
|
136
|
-
* @returns {handler[]} - The wrapped version of the handler.
|
|
137
|
-
*/
|
|
138
|
-
appendListenerOnce(event: string | string[], handler: handler): handler[];
|
|
139
|
-
/**
|
|
140
|
-
* Adds a event listener.
|
|
141
|
-
*
|
|
142
|
-
* @param {string|string[]} event - Event name, such as 'onScrollBoundary' or 'onAutoScroll'.
|
|
143
|
-
* @param {handler} handler - Callback function to be called when event fires.
|
|
144
|
-
*/
|
|
145
|
-
on(event: string | string[], handler: handler): void;
|
|
146
|
-
/**
|
|
147
|
-
* Registers an event listener that runs only once, then is removed.
|
|
148
|
-
*
|
|
149
|
-
* @param {string|string[]} event - Event name, such as 'onScrollBoundary' or 'onAutoScroll'.
|
|
150
|
-
* @param {handler} handler - The callback function to run on event.
|
|
151
|
-
* @returns {handler[]} - The wrapped version of the handler.
|
|
152
|
-
*/
|
|
153
|
-
once(event: string | string[], handler: handler): handler[];
|
|
154
|
-
/**
|
|
155
|
-
* Removes a previously registered event listener.
|
|
156
|
-
*
|
|
157
|
-
* @param {string|string[]} event - The name of the event to remove the handler from.
|
|
158
|
-
* @param {handler} handler - The specific callback function to remove.
|
|
159
|
-
*/
|
|
160
|
-
off(event: string | string[], handler: handler): void;
|
|
161
|
-
/**
|
|
162
|
-
* Removes all event listeners of a specific type from the element.
|
|
163
|
-
*
|
|
164
|
-
* @param {string|string[]} event - The event type to remove (e.g. 'onScrollBoundary').
|
|
165
|
-
*/
|
|
166
|
-
offAll(event: string | string[]): void;
|
|
167
|
-
/**
|
|
168
|
-
* Removes all event listeners of all types from the element.
|
|
169
|
-
*/
|
|
170
|
-
offAllTypes(): void;
|
|
171
|
-
/**
|
|
172
|
-
* Returns the number of listeners for a given event.
|
|
173
|
-
*
|
|
174
|
-
* @param {string} event - The name of the event.
|
|
175
|
-
* @returns {number} Number of listeners for the event.
|
|
176
|
-
*/
|
|
177
|
-
listenerCount(event: string): number;
|
|
178
|
-
/**
|
|
179
|
-
* Returns a copy of the array of listeners for the specified event.
|
|
180
|
-
*
|
|
181
|
-
* @param {string} event - The name of the event.
|
|
182
|
-
* @returns {handler[]} Array of listener functions.
|
|
183
|
-
*/
|
|
184
|
-
listeners(event: string): handler[];
|
|
185
|
-
/**
|
|
186
|
-
* Returns a copy of the array of listeners for the specified event.
|
|
187
|
-
*
|
|
188
|
-
* @param {string} event - The name of the event.
|
|
189
|
-
* @returns {handler[]} Array of listener functions.
|
|
190
|
-
*/
|
|
191
|
-
onceListeners(event: string): handler[];
|
|
192
|
-
/**
|
|
193
|
-
* Returns a copy of the internal listeners array for the specified event,
|
|
194
|
-
* including wrapper functions like those used by `.once()`.
|
|
195
|
-
* @param {string | symbol} event - The event name.
|
|
196
|
-
* @returns {handler[]} An array of raw listener functions.
|
|
197
|
-
*/
|
|
198
|
-
allListeners(event: string | symbol): handler[];
|
|
199
|
-
/**
|
|
200
|
-
* Returns an array of event names for which there are registered listeners.
|
|
201
|
-
*
|
|
202
|
-
* @returns {string[]} Array of registered event names.
|
|
203
|
-
*/
|
|
204
|
-
eventNames(): string[];
|
|
205
|
-
/**
|
|
206
|
-
* Sets the maximum number of listeners per event before a warning is shown.
|
|
207
|
-
*
|
|
208
|
-
* @param {number} n - The maximum number of listeners.
|
|
209
|
-
*/
|
|
210
|
-
setMaxListeners(n: number): void;
|
|
211
|
-
/**
|
|
212
|
-
* Gets the maximum number of listeners allowed per event.
|
|
213
|
-
*
|
|
214
|
-
* @returns {number} The maximum number of listeners.
|
|
215
|
-
*/
|
|
216
|
-
getMaxListeners(): number;
|
|
217
97
|
get targetWindow(): Window;
|
|
218
98
|
get targetIframeElement(): HTMLIFrameElement | undefined;
|
|
219
99
|
get targetOrigin(): string;
|
|
@@ -269,7 +149,7 @@ declare class TinyIframeEvents {
|
|
|
269
149
|
* @throws {TypeError} If `eventName` is not a string.
|
|
270
150
|
* @throws {Error} If instance has been destroyed.
|
|
271
151
|
*/
|
|
272
|
-
|
|
152
|
+
winEmit(eventName: string, payload: any): void;
|
|
273
153
|
/**
|
|
274
154
|
* Checks if the communication instance has been destroyed.
|
|
275
155
|
*
|
|
@@ -283,4 +163,5 @@ declare class TinyIframeEvents {
|
|
|
283
163
|
destroy(): void;
|
|
284
164
|
#private;
|
|
285
165
|
}
|
|
166
|
+
import { EventEmitter } from 'events';
|
|
286
167
|
//# sourceMappingURL=TinyIframeEvents.d.mts.map
|