tiny-essentials 1.19.2 → 1.20.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.
Files changed (34) hide show
  1. package/dist/v1/TinyEssentials.js +1009 -22
  2. package/dist/v1/TinyEssentials.min.js +1 -1
  3. package/dist/v1/TinyIframeEvents.js +854 -0
  4. package/dist/v1/TinyIframeEvents.min.js +1 -0
  5. package/dist/v1/TinyLocalStorage.js +109 -21
  6. package/dist/v1/TinyLocalStorage.min.js +1 -1
  7. package/dist/v1/TinyNewWinEvents.js +888 -0
  8. package/dist/v1/TinyNewWinEvents.min.js +1 -0
  9. package/dist/v1/TinyUploadClicker.js +1022 -37
  10. package/dist/v1/TinyUploadClicker.min.js +1 -1
  11. package/dist/v1/build/TinyIframeEvents.cjs +7 -0
  12. package/dist/v1/build/TinyIframeEvents.d.mts +3 -0
  13. package/dist/v1/build/TinyIframeEvents.mjs +2 -0
  14. package/dist/v1/build/TinyNewWinEvents.cjs +7 -0
  15. package/dist/v1/build/TinyNewWinEvents.d.mts +3 -0
  16. package/dist/v1/build/TinyNewWinEvents.mjs +2 -0
  17. package/dist/v1/index.cjs +4 -0
  18. package/dist/v1/index.d.mts +3 -1
  19. package/dist/v1/index.mjs +3 -1
  20. package/dist/v1/libs/TinyIframeEvents.cjs +409 -0
  21. package/dist/v1/libs/TinyIframeEvents.d.mts +193 -0
  22. package/dist/v1/libs/TinyIframeEvents.mjs +348 -0
  23. package/dist/v1/libs/TinyLocalStorage.cjs +109 -21
  24. package/dist/v1/libs/TinyLocalStorage.d.mts +29 -0
  25. package/dist/v1/libs/TinyLocalStorage.mjs +97 -21
  26. package/dist/v1/libs/TinyNewWinEvents.cjs +486 -0
  27. package/dist/v1/libs/TinyNewWinEvents.d.mts +241 -0
  28. package/dist/v1/libs/TinyNewWinEvents.mjs +437 -0
  29. package/docs/v1/README.md +2 -0
  30. package/docs/v1/libs/TinyIframeEvents.md +149 -0
  31. package/docs/v1/libs/TinyLocalStorage.md +53 -0
  32. package/docs/v1/libs/TinyNewWinEvents.md +186 -0
  33. package/docs/v1/libs/TinyNotifyCenter.md +1 -1
  34. package/package.json +1 -1
@@ -0,0 +1,888 @@
1
+ /******/ (() => { // webpackBootstrap
2
+ /******/ "use strict";
3
+ /******/ // The require scope
4
+ /******/ var __webpack_require__ = {};
5
+ /******/
6
+ /************************************************************************/
7
+ /******/ /* webpack/runtime/define property getters */
8
+ /******/ (() => {
9
+ /******/ // define getter functions for harmony exports
10
+ /******/ __webpack_require__.d = (exports, definition) => {
11
+ /******/ for(var key in definition) {
12
+ /******/ if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {
13
+ /******/ Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });
14
+ /******/ }
15
+ /******/ }
16
+ /******/ };
17
+ /******/ })();
18
+ /******/
19
+ /******/ /* webpack/runtime/hasOwnProperty shorthand */
20
+ /******/ (() => {
21
+ /******/ __webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))
22
+ /******/ })();
23
+ /******/
24
+ /************************************************************************/
25
+ var __webpack_exports__ = {};
26
+
27
+ // EXPORTS
28
+ __webpack_require__.d(__webpack_exports__, {
29
+ TinyNewWinEvents: () => (/* reexport */ libs_TinyNewWinEvents)
30
+ });
31
+
32
+ ;// ./src/v1/libs/TinyEvents.mjs
33
+ /**
34
+ * A generic event listener callback function.
35
+ *
36
+ * @callback handler
37
+ * @param {...any} payload - The data payload passed when the event is triggered.
38
+ * @returns {void}
39
+ */
40
+
41
+ /**
42
+ * TinyEvents provides a minimalistic event emitter system similar to Node.js's EventEmitter,
43
+ * enabling components to subscribe to, emit, and manage events and their listeners.
44
+ *
45
+ * Features include:
46
+ * - Adding/removing event listeners (`on`, `off`, `offAll`, `offAllTypes`)
47
+ * - One-time listeners (`once`)
48
+ * - Emitting events (`emit`)
49
+ * - Listener inspection and limits (`listenerCount`, `listeners`, `eventNames`)
50
+ * - Maximum listener control (`setMaxListeners`, `getMaxListeners`)
51
+ *
52
+ * This class is useful for lightweight, dependency-free publish/subscribe event handling
53
+ * within modular JavaScript applications.
54
+ *
55
+ * @class
56
+ */
57
+ class TinyEvents {
58
+ /** @type {Map<string, { handler: handler; config: { once: boolean } }[]>} */
59
+ #listeners = new Map();
60
+
61
+ /** @type {number} */
62
+ #maxListeners = 10;
63
+
64
+ /** @type {boolean} */
65
+ #throwMaxListeners = false;
66
+
67
+ /**
68
+ * Enables or disables throwing an error when the maximum number of listeners is exceeded.
69
+ *
70
+ * @param {boolean} shouldThrow - If true, an error will be thrown when the max is exceeded.
71
+ */
72
+ setThrowOnMaxListeners(shouldThrow) {
73
+ if (typeof shouldThrow !== 'boolean')
74
+ throw new TypeError('setThrowOnMaxListeners(value): value must be a boolean');
75
+ this.#throwMaxListeners = shouldThrow;
76
+ }
77
+
78
+ /**
79
+ * Checks whether an error will be thrown when the max listener limit is exceeded.
80
+ *
81
+ * @returns {boolean} True if an error will be thrown, false if only a warning is shown.
82
+ */
83
+ getThrowOnMaxListeners() {
84
+ return this.#throwMaxListeners;
85
+ }
86
+
87
+ ///////////////////////////////////////////////////
88
+
89
+ /**
90
+ * Internal method to prepend a listener with options.
91
+ *
92
+ * @param {string} event - Event name.
93
+ * @param {handler} handler - The callback function.
94
+ * @param {Object} [settings={}] - Optional settings.
95
+ * @param {boolean} [settings.once=false] - If the listener should be executed once.
96
+ */
97
+ #prepend(event, handler, { once = false } = {}) {
98
+ let eventData = this.#listeners.get(event);
99
+ if (!Array.isArray(eventData)) {
100
+ eventData = [];
101
+ this.#listeners.set(event, eventData);
102
+ }
103
+ eventData.unshift({ handler, config: { once } });
104
+
105
+ const max = this.#maxListeners;
106
+ if (max > 0 && eventData.length > max) {
107
+ const warnMessage =
108
+ `Possible memory leak detected. ${eventData.length} "${event}" listeners added. ` +
109
+ `Use setMaxListeners() to increase limit.`;
110
+ if (!this.#throwMaxListeners) console.warn(warnMessage);
111
+ else throw new Error(warnMessage);
112
+ }
113
+ }
114
+
115
+ /**
116
+ * Adds a listener to the beginning of the listeners array for the specified event.
117
+ *
118
+ * @param {string} event - Event name.
119
+ * @param {handler} handler - The callback function.
120
+ */
121
+ prependListener(event, handler) {
122
+ if (typeof event !== 'string')
123
+ throw new TypeError('prepend(event, handler): event name must be a string');
124
+ if (typeof handler !== 'function')
125
+ throw new TypeError('prepend(event, handler): handler must be a function');
126
+ this.#prepend(event, handler);
127
+ }
128
+
129
+ /**
130
+ * Adds a one-time listener to the beginning of the listeners array for the specified event.
131
+ *
132
+ * @param {string} event - Event name.
133
+ * @param {handler} handler - The callback function.
134
+ * @returns {handler} - The wrapped handler used internally.
135
+ */
136
+ prependListenerOnce(event, handler) {
137
+ if (typeof event !== 'string')
138
+ throw new TypeError('prependOnceListener(event, handler): event name must be a string');
139
+ if (typeof handler !== 'function')
140
+ throw new TypeError('prependOnceListener(event, handler): handler must be a function');
141
+
142
+ /** @type {handler} */
143
+ const wrapped = (...args) => {
144
+ this.off(event, wrapped);
145
+ handler(...args);
146
+ };
147
+
148
+ this.#prepend(event, wrapped, { once: true });
149
+ return wrapped;
150
+ }
151
+
152
+ ////////////////////////////////////////////////////////////
153
+
154
+ /**
155
+ * Adds a event listener.
156
+ *
157
+ * @param {string} event - Event name, such as 'onScrollBoundary' or 'onAutoScroll'.
158
+ * @param {handler} handler - Callback function to be called when event fires.
159
+ * @param {Object} [settings={}] - Optional settings.
160
+ * @param {boolean} [settings.once=false] - This is a once event.
161
+ */
162
+ #on(event, handler, { once = false } = {}) {
163
+ let eventData = this.#listeners.get(event);
164
+ if (!Array.isArray(eventData)) {
165
+ eventData = [];
166
+ this.#listeners.set(event, eventData);
167
+ }
168
+ eventData.push({ handler, config: { once } });
169
+ // Warn if listener count exceeds the max allowed
170
+ const max = this.#maxListeners;
171
+ if (max > 0 && eventData.length > max) {
172
+ const warnMessage =
173
+ `Possible memory leak detected. ${eventData.length} "${event}" listeners added. ` +
174
+ `Use setMaxListeners() to increase limit.`;
175
+ if (!this.#throwMaxListeners) console.warn(warnMessage);
176
+ else throw new Error(warnMessage);
177
+ }
178
+ }
179
+
180
+ /**
181
+ * Adds a event listener.
182
+ *
183
+ * @param {string} event - Event name, such as 'onScrollBoundary' or 'onAutoScroll'.
184
+ * @param {handler} handler - Callback function to be called when event fires.
185
+ */
186
+ on(event, handler) {
187
+ if (typeof event !== 'string')
188
+ throw new TypeError('on(event, handler): event name must be a string');
189
+ if (typeof handler !== 'function')
190
+ throw new TypeError('on(event, handler): handler must be a function');
191
+ return this.#on(event, handler);
192
+ }
193
+
194
+ /**
195
+ * Registers an event listener that runs only once, then is removed.
196
+ *
197
+ * @param {string} event - Event name, such as 'onScrollBoundary' or 'onAutoScroll'.
198
+ * @param {handler} handler - The callback function to run on event.
199
+ * @returns {handler} - The wrapped version of the handler.
200
+ */
201
+ once(event, handler) {
202
+ if (typeof event !== 'string') throw new TypeError('The event name must be a string.');
203
+ if (typeof handler !== 'function')
204
+ throw new TypeError('once(event, handler): handler must be a function');
205
+
206
+ /** @type {handler} */
207
+ const wrapped = (e) => {
208
+ this.off(event, wrapped);
209
+ if (typeof handler === 'function') handler(e);
210
+ };
211
+ this.#on(event, wrapped, { once: true });
212
+ return wrapped;
213
+ }
214
+
215
+ /**
216
+ * Adds a event listener.
217
+ *
218
+ * @param {string} event - Event name, such as 'onScrollBoundary' or 'onAutoScroll'.
219
+ * @param {handler} handler - Callback function to be called when event fires.
220
+ */
221
+ appendListener(event, handler) {
222
+ return this.on(event, handler);
223
+ }
224
+
225
+ /**
226
+ * Registers an event listener that runs only once, then is removed.
227
+ *
228
+ * @param {string} event - Event name, such as 'onScrollBoundary' or 'onAutoScroll'.
229
+ * @param {handler} handler - The callback function to run on event.
230
+ * @returns {handler} - The wrapped version of the handler.
231
+ */
232
+ appendListenerOnce(event, handler) {
233
+ return this.once(event, handler);
234
+ }
235
+
236
+ ///////////////////////////////////////////////
237
+
238
+ /**
239
+ * Removes a previously registered event listener.
240
+ *
241
+ * @param {string} event - The name of the event to remove the handler from.
242
+ * @param {handler} handler - The specific callback function to remove.
243
+ */
244
+ off(event, handler) {
245
+ if (typeof event !== 'string')
246
+ throw new TypeError('off(event, handler): event name must be a string');
247
+ if (typeof handler !== 'function')
248
+ throw new TypeError('off(event, handler): handler must be a function');
249
+
250
+ const listeners = this.#listeners.get(event);
251
+ if (!Array.isArray(listeners)) return;
252
+
253
+ const index = listeners.findIndex((listener) => listener.handler === handler);
254
+ if (index !== -1) listeners.splice(index, 1);
255
+
256
+ // Optionally clean up empty arrays (optional)
257
+ if (listeners.length === 0) this.#listeners.delete(event);
258
+ }
259
+
260
+ /**
261
+ * Removes all event listeners of a specific type from the element.
262
+ *
263
+ * @param {string} event - The event type to remove (e.g. 'onScrollBoundary').
264
+ */
265
+ offAll(event) {
266
+ if (typeof event !== 'string') throw new TypeError('The event name must be a string.');
267
+ this.#listeners.delete(event);
268
+ }
269
+
270
+ /**
271
+ * Removes all event listeners of all types from the element.
272
+ */
273
+ offAllTypes() {
274
+ this.#listeners.clear();
275
+ }
276
+
277
+ /////////////////////////////////////////////
278
+
279
+ /**
280
+ * Returns the number of listeners for a given event.
281
+ *
282
+ * @param {string} event - The name of the event.
283
+ * @returns {number} Number of listeners for the event.
284
+ */
285
+ listenerCount(event) {
286
+ if (typeof event !== 'string')
287
+ throw new TypeError('listenerCount(event): event name must be a string');
288
+
289
+ const listeners = this.#listeners.get(event);
290
+ return Array.isArray(listeners) ? listeners.length : 0;
291
+ }
292
+
293
+ /**
294
+ * Returns a copy of the array of listeners for the specified event.
295
+ *
296
+ * @param {string} event - The name of the event.
297
+ * @returns {handler[]} Array of listener functions.
298
+ */
299
+ listeners(event) {
300
+ if (typeof event !== 'string')
301
+ throw new TypeError('listeners(event): event name must be a string');
302
+
303
+ const listeners = this.#listeners.get(event);
304
+ return Array.isArray(listeners)
305
+ ? [...listeners]
306
+ .filter((listener) => !listener.config.once)
307
+ .map((listener) => listener.handler)
308
+ : [];
309
+ }
310
+
311
+ /**
312
+ * Returns a copy of the array of listeners for the specified event.
313
+ *
314
+ * @param {string} event - The name of the event.
315
+ * @returns {handler[]} Array of listener functions.
316
+ */
317
+ onceListeners(event) {
318
+ if (typeof event !== 'string')
319
+ throw new TypeError('onceListeners(event): event name must be a string');
320
+
321
+ const listeners = this.#listeners.get(event);
322
+ return Array.isArray(listeners)
323
+ ? [...listeners]
324
+ .filter((listener) => listener.config.once)
325
+ .map((listener) => listener.handler)
326
+ : [];
327
+ }
328
+
329
+ /**
330
+ * Returns a copy of the internal listeners array for the specified event,
331
+ * including wrapper functions like those used by `.once()`.
332
+ * @param {string | symbol} event - The event name.
333
+ * @returns {handler[]} An array of raw listener functions.
334
+ */
335
+ allListeners(event) {
336
+ if (typeof event !== 'string')
337
+ throw new TypeError('allListeners(event): event name must be a string');
338
+ const listeners = this.#listeners.get(event);
339
+ return Array.isArray(listeners) ? [...listeners].map((listener) => listener.handler) : [];
340
+ }
341
+
342
+ /**
343
+ * Returns an array of event names for which there are registered listeners.
344
+ *
345
+ * @returns {string[]} Array of registered event names.
346
+ */
347
+ eventNames() {
348
+ return [...this.#listeners.keys()];
349
+ }
350
+
351
+ /**
352
+ * Emits an event, triggering all registered handlers for that event.
353
+ *
354
+ * @param {string} event - The event name to emit.
355
+ * @param {...any} payload - Optional data to pass to each handler.
356
+ * @returns {boolean} True if any listeners were called, false otherwise.
357
+ */
358
+ emit(event, ...payload) {
359
+ if (typeof event !== 'string')
360
+ throw new TypeError('emit(event, data): event name must be a string');
361
+
362
+ const listeners = this.#listeners.get(event);
363
+ if (!Array.isArray(listeners) || listeners.length === 0) return false;
364
+
365
+ // Call all listeners with the provided data
366
+ listeners.forEach((listener) => listener.handler(...payload));
367
+ return true;
368
+ }
369
+
370
+ ///////////////////////////////////
371
+
372
+ /**
373
+ * Sets the maximum number of listeners per event before a warning is shown.
374
+ *
375
+ * @param {number} n - The maximum number of listeners.
376
+ */
377
+ setMaxListeners(n) {
378
+ if (!Number.isInteger(n) || n < 0)
379
+ throw new TypeError('setMaxListeners(n): n must be a non-negative integer');
380
+ this.#maxListeners = n;
381
+ }
382
+
383
+ /**
384
+ * Gets the maximum number of listeners allowed per event.
385
+ *
386
+ * @returns {number} The maximum number of listeners.
387
+ */
388
+ getMaxListeners() {
389
+ return this.#maxListeners;
390
+ }
391
+ }
392
+
393
+ /* harmony default export */ const libs_TinyEvents = (TinyEvents);
394
+
395
+ ;// ./src/v1/libs/TinyNewWinEvents.mjs
396
+
397
+
398
+ /**
399
+ * Stores polling intervals associated with window references.
400
+ * Used to detect when the window is closed.
401
+ *
402
+ * @type {WeakMap<Window, NodeJS.Timeout>}
403
+ */
404
+ const pollClosedInterval = new WeakMap();
405
+
406
+ /**
407
+ * @callback handler
408
+ * A function to handle incoming event payloads.
409
+ * @param {any} payload - The data sent by the emitter.
410
+ * @param {MessageEvent<any>} event - Metadata about the message.
411
+ */
412
+
413
+ /**
414
+ * TinyNewWinEvents provides structured communication between a main window
415
+ * and a child window (created using window.open) using postMessage.
416
+ *
417
+ * It supports routing, queuing messages until handshake is ready,
418
+ * connection status checks, and close detection.
419
+ *
420
+ * @class
421
+ */
422
+ class TinyNewWinEvents {
423
+ #events = new libs_TinyEvents();
424
+
425
+ /**
426
+ * Enables or disables throwing an error when the maximum number of listeners is exceeded.
427
+ *
428
+ * @param {boolean} shouldThrow - If true, an error will be thrown when the max is exceeded.
429
+ */
430
+ setThrowOnMaxListeners(shouldThrow) {
431
+ return this.#events.setThrowOnMaxListeners(shouldThrow);
432
+ }
433
+
434
+ /**
435
+ * Checks whether an error will be thrown when the max listener limit is exceeded.
436
+ *
437
+ * @returns {boolean} True if an error will be thrown, false if only a warning is shown.
438
+ */
439
+ getThrowOnMaxListeners() {
440
+ return this.#events.getThrowOnMaxListeners();
441
+ }
442
+
443
+ /////////////////////////////////////////////////////////////
444
+
445
+ /**
446
+ * Adds a listener to the beginning of the listeners array for the specified event.
447
+ *
448
+ * @param {string} event - Event name.
449
+ * @param {handler} handler - The callback function.
450
+ */
451
+ prependListener(event, handler) {
452
+ return this.#events.prependListener(event, handler);
453
+ }
454
+
455
+ /**
456
+ * Adds a one-time listener to the beginning of the listeners array for the specified event.
457
+ *
458
+ * @param {string} event - Event name.
459
+ * @param {handler} handler - The callback function.
460
+ * @returns {handler} - The wrapped handler used internally.
461
+ */
462
+ prependListenerOnce(event, handler) {
463
+ return this.#events.prependListenerOnce(event, handler);
464
+ }
465
+
466
+ //////////////////////////////////////////////////////////////////////
467
+
468
+ /**
469
+ * Adds a event listener.
470
+ *
471
+ * @param {string} event - Event name, such as 'onScrollBoundary' or 'onAutoScroll'.
472
+ * @param {handler} handler - Callback function to be called when event fires.
473
+ */
474
+ appendListener(event, handler) {
475
+ return this.#events.appendListener(event, handler);
476
+ }
477
+
478
+ /**
479
+ * Registers an event listener that runs only once, then is removed.
480
+ *
481
+ * @param {string} event - Event name, such as 'onScrollBoundary' or 'onAutoScroll'.
482
+ * @param {handler} handler - The callback function to run on event.
483
+ * @returns {handler} - The wrapped version of the handler.
484
+ */
485
+ appendListenerOnce(event, handler) {
486
+ return this.#events.appendListenerOnce(event, handler);
487
+ }
488
+
489
+ /**
490
+ * Adds a event listener.
491
+ *
492
+ * @param {string} event - Event name, such as 'onScrollBoundary' or 'onAutoScroll'.
493
+ * @param {handler} handler - Callback function to be called when event fires.
494
+ */
495
+ on(event, handler) {
496
+ return this.#events.on(event, handler);
497
+ }
498
+
499
+ /**
500
+ * Registers an event listener that runs only once, then is removed.
501
+ *
502
+ * @param {string} event - Event name, such as 'onScrollBoundary' or 'onAutoScroll'.
503
+ * @param {handler} handler - The callback function to run on event.
504
+ * @returns {handler} - The wrapped version of the handler.
505
+ */
506
+ once(event, handler) {
507
+ return this.#events.once(event, handler);
508
+ }
509
+
510
+ ////////////////////////////////////////////////////////////////////
511
+
512
+ /**
513
+ * Removes a previously registered event listener.
514
+ *
515
+ * @param {string} event - The name of the event to remove the handler from.
516
+ * @param {handler} handler - The specific callback function to remove.
517
+ */
518
+ off(event, handler) {
519
+ return this.#events.off(event, handler);
520
+ }
521
+
522
+ /**
523
+ * Removes all event listeners of a specific type from the element.
524
+ *
525
+ * @param {string} event - The event type to remove (e.g. 'onScrollBoundary').
526
+ */
527
+ offAll(event) {
528
+ return this.#events.offAll(event);
529
+ }
530
+
531
+ /**
532
+ * Removes all event listeners of all types from the element.
533
+ */
534
+ offAllTypes() {
535
+ return this.#events.offAllTypes();
536
+ }
537
+
538
+ ////////////////////////////////////////////////////////////
539
+
540
+ /**
541
+ * Returns the number of listeners for a given event.
542
+ *
543
+ * @param {string} event - The name of the event.
544
+ * @returns {number} Number of listeners for the event.
545
+ */
546
+ listenerCount(event) {
547
+ return this.#events.listenerCount(event);
548
+ }
549
+
550
+ /**
551
+ * Returns a copy of the array of listeners for the specified event.
552
+ *
553
+ * @param {string} event - The name of the event.
554
+ * @returns {handler[]} Array of listener functions.
555
+ */
556
+ listeners(event) {
557
+ return this.#events.listeners(event);
558
+ }
559
+
560
+ /**
561
+ * Returns a copy of the array of listeners for the specified event.
562
+ *
563
+ * @param {string} event - The name of the event.
564
+ * @returns {handler[]} Array of listener functions.
565
+ */
566
+ onceListeners(event) {
567
+ return this.#events.onceListeners(event);
568
+ }
569
+
570
+ /**
571
+ * Returns a copy of the internal listeners array for the specified event,
572
+ * including wrapper functions like those used by `.once()`.
573
+ * @param {string | symbol} event - The event name.
574
+ * @returns {handler[]} An array of raw listener functions.
575
+ */
576
+ allListeners(event) {
577
+ return this.#events.allListeners(event);
578
+ }
579
+
580
+ /**
581
+ * Returns an array of event names for which there are registered listeners.
582
+ *
583
+ * @returns {string[]} Array of registered event names.
584
+ */
585
+ eventNames() {
586
+ return this.#events.eventNames();
587
+ }
588
+
589
+ //////////////////////////////////////////////////////
590
+
591
+ /**
592
+ * Sets the maximum number of listeners per event before a warning is shown.
593
+ *
594
+ * @param {number} n - The maximum number of listeners.
595
+ */
596
+ setMaxListeners(n) {
597
+ return this.#events.setMaxListeners(n);
598
+ }
599
+
600
+ /**
601
+ * Gets the maximum number of listeners allowed per event.
602
+ *
603
+ * @returns {number} The maximum number of listeners.
604
+ */
605
+ getMaxListeners() {
606
+ return this.#events.getMaxListeners();
607
+ }
608
+
609
+ ///////////////////////////////////////////////////
610
+
611
+ /** @type {Window|null} Reference to the opened or parent window */
612
+ #windowRef;
613
+
614
+ /** @type {string} Expected origin for postMessage communication */
615
+ #targetOrigin;
616
+
617
+ /** @type {{ route: string, payload: any }[]} Queue of messages emitted before connection is ready */
618
+ #pendingQueue = [];
619
+
620
+ /** @type {boolean} True if handshake between windows is complete */
621
+ #ready = false;
622
+
623
+ /** @type {boolean} True if this instance is the main window (host) */
624
+ #isHost = false;
625
+
626
+ /** @type {NodeJS.Timeout|null} Interval for polling child window closure */
627
+ #pollClosedInterval = null;
628
+
629
+ /** @type {string} Internal message type for handshake */
630
+ #readyEventName = '__TNE_READY__';
631
+
632
+ /** @type {string} Internal message type for routed communication */
633
+ #routeEventName = '__TNE_ROUTE__';
634
+
635
+ /**
636
+ * Gets the internal handshake event name.
637
+ * @returns {string}
638
+ */
639
+ get readyEventName() {
640
+ return this.#readyEventName;
641
+ }
642
+
643
+ /**
644
+ * Sets the internal handshake event name.
645
+ * @param {string} name
646
+ * @throws {TypeError} If the value is not a string.
647
+ */
648
+ set readyEventName(name) {
649
+ if (typeof name !== 'string') throw new TypeError('TinyNewWinEvents: readyEventName must be a string.');
650
+ this.#readyEventName = name;
651
+ }
652
+
653
+ /**
654
+ * Gets the internal route event name.
655
+ * @returns {string}
656
+ */
657
+ get routeEventName() {
658
+ return this.#routeEventName;
659
+ }
660
+
661
+ /**
662
+ * Sets the internal route event name.
663
+ * @param {string} name
664
+ * @throws {TypeError} If the value is not a string.
665
+ */
666
+ set routeEventName(name) {
667
+ if (typeof name !== 'string') throw new TypeError('TinyNewWinEvents: routeEventName must be a string.');
668
+ this.#routeEventName = name;
669
+ }
670
+
671
+ /**
672
+ * Initializes a TinyNewWinEvents instance for communication.
673
+ *
674
+ * @param {Object} [settings={}] Configuration object.
675
+ * @param {string} [settings.targetOrigin] Origin to enforce in postMessage.
676
+ * @param {string} [settings.url] URL string to open.
677
+ * @param {string} [settings.name] Window name (required if opening a new window).
678
+ * @param {string} [settings.features] Features string for `window.open`.
679
+ *
680
+ * @throws {Error} If `name` is "_blank", which is not allowed.
681
+ * @throws {TypeError} If `targetOrigin`, `url`, or `features` are not strings (when provided).
682
+ * @throws {Error} If the window reference is invalid or already being tracked.
683
+ */
684
+ constructor({ targetOrigin, url, name, features } = {}) {
685
+ if (typeof name === 'string' && name === '_blank')
686
+ throw new Error(
687
+ 'TinyNewWinEvents: The window name "_blank" is not supported. Please use a custom name to allow tracking.',
688
+ );
689
+ if (typeof targetOrigin !== 'undefined' && typeof targetOrigin !== 'string')
690
+ throw new TypeError('TinyNewWinEvents: The "targetOrigin" option must be a string.');
691
+ if (typeof url !== 'undefined' && typeof url !== 'string')
692
+ throw new TypeError('TinyNewWinEvents: The "url" option must be a string.');
693
+ if (typeof features !== 'undefined' && typeof features !== 'string')
694
+ throw new TypeError('TinyNewWinEvents: The "features" option must be a string if provided.');
695
+
696
+ // Open Page
697
+ if (typeof url === 'undefined') this.#windowRef = window.opener;
698
+ // Main Page
699
+ else {
700
+ this.#windowRef = typeof url === 'string' ? window.open(url, name, features) : url;
701
+ this.#isHost = true;
702
+ }
703
+
704
+ if (!this.#windowRef || pollClosedInterval.has(this.#windowRef))
705
+ throw new Error('Invalid or duplicate window reference.');
706
+ this.#targetOrigin = targetOrigin ?? window.location.origin;
707
+ this._handleMessage = this.#handleMessage.bind(this);
708
+ window.addEventListener('message', this._handleMessage, false);
709
+
710
+ // Sends handshake if it is host (main window)
711
+ if (!this.#isHost) {
712
+ this.#postRaw(this.#readyEventName, null);
713
+ this.#startCloseWatcher();
714
+ }
715
+ }
716
+
717
+ /**
718
+ * Returns the internal window reference.
719
+ *
720
+ * @returns {Window|null}
721
+ */
722
+ getWin() {
723
+ return this.#windowRef;
724
+ }
725
+
726
+ /**
727
+ * Internal message handler.
728
+ *
729
+ * @param {MessageEvent} event
730
+ * @returns {void}
731
+ */
732
+ #handleMessage(event) {
733
+ if (!event.source || (this.#windowRef && event.source !== this.#windowRef)) return;
734
+ const { type, route, payload } = event.data || {};
735
+
736
+ if (type === this.#readyEventName) {
737
+ this.#ready = true;
738
+ this.#flushQueue();
739
+ this.#startCloseWatcher(); // start watcher after handshake (for child window)
740
+ if (this.#isHost) this.#postRaw(this.#readyEventName, null);
741
+ return;
742
+ }
743
+
744
+ if (type === this.#routeEventName) this.#events.emit(route, payload, event);
745
+ }
746
+
747
+ /**
748
+ * Sends all pending messages queued before handshake completion.
749
+ *
750
+ * @returns {void}
751
+ */
752
+ #flushQueue() {
753
+ while (this.#pendingQueue.length) {
754
+ const data = this.#pendingQueue.shift();
755
+ if (data) {
756
+ const { route, payload } = data;
757
+ this.emit(route, payload);
758
+ }
759
+ }
760
+ }
761
+
762
+ /**
763
+ * Sends a raw postMessage with given type and payload.
764
+ *
765
+ * @param {string} type Internal message type
766
+ * @param {any} payload Data to send
767
+ * @param {string} [route=''] Optional route name
768
+ * @returns {void}
769
+ */
770
+ #postRaw(type, payload, route = '') {
771
+ if (this.#windowRef && this.#windowRef.closed) return;
772
+ this.#windowRef?.postMessage({ type, route, payload }, this.#targetOrigin);
773
+ }
774
+
775
+ /**
776
+ * Closes the child window (only allowed from the host).
777
+ *
778
+ * @returns {void}
779
+ */
780
+ close() {
781
+ if (!this.#isHost) throw new Error('Only host can close the window.');
782
+ if (this.#windowRef && !this.#windowRef.closed) this.#windowRef.close();
783
+ }
784
+
785
+ /**
786
+ * Emits a message to the other window on a specific route.
787
+ * If the handshake is not yet complete, the message is queued.
788
+ * Throws an error if the instance has already been destroyed.
789
+ *
790
+ * @param {string} route - Route name used to identify the message handler.
791
+ * @param {any} payload - Data to send along with the message.
792
+ * @throws {Error} If the instance is already destroyed.
793
+ * @returns {void}
794
+ */
795
+ emit(route, payload) {
796
+ if (typeof route !== 'string') throw new TypeError('Event name must be a string.');
797
+ if (this.isDestroyed()) throw new Error('Cannot emit: instance has been destroyed.');
798
+ if (!this.#ready) {
799
+ this.#pendingQueue.push({ route, payload });
800
+ return;
801
+ }
802
+ this.#postRaw(this.#routeEventName, payload, route);
803
+ }
804
+
805
+ /**
806
+ * Checks if the connection is active and the window is still open.
807
+ *
808
+ * @returns {boolean}
809
+ */
810
+ isConnected() {
811
+ return this.#ready && this.#windowRef && !this.#windowRef.closed ? true : false;
812
+ }
813
+
814
+ /**
815
+ * Starts polling to detect when the window is closed.
816
+ *
817
+ * @returns {void}
818
+ */
819
+ #startCloseWatcher() {
820
+ if (!this.#windowRef || this.#pollClosedInterval) return;
821
+ this.#pollClosedInterval = setInterval(() => {
822
+ if (this.#windowRef?.closed) {
823
+ this.#events.emit('WINDOW_REF_CLOSED');
824
+ this.destroy();
825
+ }
826
+ }, 500);
827
+ pollClosedInterval.set(this.#windowRef, this.#pollClosedInterval);
828
+ }
829
+
830
+ /**
831
+ * Registers a callback for when the window is closed.
832
+ *
833
+ * @param {handler} callback Callback to run on close
834
+ * @returns {void}
835
+ */
836
+ onClose(callback) {
837
+ return this.#events.on('WINDOW_REF_CLOSED', callback);
838
+ }
839
+
840
+ /**
841
+ * Unregisters a previously registered close callback.
842
+ *
843
+ * @param {handler} callback Callback to remove
844
+ * @returns {void}
845
+ */
846
+ offClose(callback) {
847
+ return this.#events.off('WINDOW_REF_CLOSED', callback);
848
+ }
849
+
850
+ /**
851
+ * Checks if the communication instance has been destroyed.
852
+ *
853
+ * @returns {boolean}
854
+ */
855
+ isDestroyed() {
856
+ return !this.#windowRef;
857
+ }
858
+
859
+ /**
860
+ * Destroys the communication instance, cleaning up all resources and listeners.
861
+ *
862
+ * @returns {void}
863
+ */
864
+ destroy() {
865
+ if (!this.#windowRef) return;
866
+ if (this.#pollClosedInterval) {
867
+ clearInterval(this.#pollClosedInterval);
868
+ this.#pollClosedInterval = null;
869
+ pollClosedInterval.delete(this.#windowRef);
870
+ }
871
+ window.removeEventListener('message', this._handleMessage);
872
+ this.#pendingQueue = [];
873
+ this.#ready = false;
874
+ this.#windowRef = null;
875
+ this.#events.offAllTypes();
876
+ }
877
+ }
878
+
879
+ /* harmony default export */ const libs_TinyNewWinEvents = (TinyNewWinEvents);
880
+
881
+ ;// ./src/v1/build/TinyNewWinEvents.mjs
882
+
883
+
884
+
885
+
886
+ window.TinyNewWinEvents = __webpack_exports__.TinyNewWinEvents;
887
+ /******/ })()
888
+ ;