tiny-essentials 1.19.3 → 1.20.1
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/TinyBasicsEs.min.js +1 -1
- package/dist/v1/TinyDragger.min.js +1 -1
- package/dist/v1/TinyEssentials.min.js +1 -1
- package/dist/v1/TinyHtml.min.js +1 -1
- package/dist/v1/TinyIframeEvents.min.js +1 -0
- package/dist/v1/TinyNewWinEvents.min.js +1 -0
- package/dist/v1/TinySmartScroller.min.js +1 -1
- package/dist/v1/TinyUploadClicker.min.js +1 -1
- package/dist/v1/build/TinyIframeEvents.cjs +7 -0
- package/dist/v1/build/TinyIframeEvents.d.mts +3 -0
- package/dist/v1/build/TinyIframeEvents.mjs +2 -0
- package/dist/v1/build/TinyNewWinEvents.cjs +7 -0
- package/dist/v1/build/TinyNewWinEvents.d.mts +3 -0
- package/dist/v1/build/TinyNewWinEvents.mjs +2 -0
- package/dist/v1/index.cjs +4 -0
- package/dist/v1/index.d.mts +3 -1
- package/dist/v1/index.mjs +3 -1
- package/dist/v1/libs/TinyDragger.cjs +1 -1
- package/dist/v1/libs/TinyDragger.mjs +1 -1
- package/dist/v1/libs/TinyHtml.cjs +292 -103
- package/dist/v1/libs/TinyHtml.d.mts +142 -39
- package/dist/v1/libs/TinyHtml.mjs +266 -94
- package/dist/v1/libs/TinyIframeEvents.cjs +410 -0
- package/dist/v1/libs/TinyIframeEvents.d.mts +193 -0
- package/dist/v1/libs/TinyIframeEvents.mjs +348 -0
- package/dist/v1/libs/TinyNewWinEvents.cjs +488 -0
- package/dist/v1/libs/TinyNewWinEvents.d.mts +241 -0
- package/dist/v1/libs/TinyNewWinEvents.mjs +437 -0
- package/docs/v1/README.md +2 -0
- package/docs/v1/libs/TinyHtml.md +128 -6
- package/docs/v1/libs/TinyIframeEvents.md +149 -0
- package/docs/v1/libs/TinyNewWinEvents.md +186 -0
- package/docs/v1/libs/TinyNotifyCenter.md +1 -1
- package/package.json +1 -1
- package/dist/v1/ColorSafeStringify.js +0 -235
- package/dist/v1/TinyAfterScrollWatcher.js +0 -219
- package/dist/v1/TinyBasicsEs.js +0 -9334
- package/dist/v1/TinyClipboard.js +0 -459
- package/dist/v1/TinyColorConverter.js +0 -617
- package/dist/v1/TinyDomReadyManager.js +0 -213
- package/dist/v1/TinyDragDropDetector.js +0 -307
- package/dist/v1/TinyDragger.js +0 -6569
- package/dist/v1/TinyEssentials.js +0 -19893
- package/dist/v1/TinyEvents.js +0 -402
- package/dist/v1/TinyHtml.js +0 -5545
- package/dist/v1/TinyLevelUp.js +0 -291
- package/dist/v1/TinyLocalStorage.js +0 -1440
- package/dist/v1/TinyNotifications.js +0 -408
- package/dist/v1/TinyNotifyCenter.js +0 -493
- package/dist/v1/TinyPromiseQueue.js +0 -299
- package/dist/v1/TinyRateLimiter.js +0 -611
- package/dist/v1/TinySmartScroller.js +0 -7039
- package/dist/v1/TinyTextRangeEditor.js +0 -497
- package/dist/v1/TinyTimeout.js +0 -233
- package/dist/v1/TinyToastNotify.js +0 -441
- package/dist/v1/TinyUploadClicker.js +0 -13456
- package/dist/v1/UltraRandomMsgGen.js +0 -995
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
export default TinyNewWinEvents;
|
|
2
|
+
/**
|
|
3
|
+
* A function to handle incoming event payloads.
|
|
4
|
+
*/
|
|
5
|
+
export type handler = (payload: any, event: MessageEvent<any>) => any;
|
|
6
|
+
/**
|
|
7
|
+
* @callback handler
|
|
8
|
+
* A function to handle incoming event payloads.
|
|
9
|
+
* @param {any} payload - The data sent by the emitter.
|
|
10
|
+
* @param {MessageEvent<any>} event - Metadata about the message.
|
|
11
|
+
*/
|
|
12
|
+
/**
|
|
13
|
+
* TinyNewWinEvents provides structured communication between a main window
|
|
14
|
+
* and a child window (created using window.open) using postMessage.
|
|
15
|
+
*
|
|
16
|
+
* It supports routing, queuing messages until handshake is ready,
|
|
17
|
+
* connection status checks, and close detection.
|
|
18
|
+
*
|
|
19
|
+
* @class
|
|
20
|
+
*/
|
|
21
|
+
declare class TinyNewWinEvents {
|
|
22
|
+
/**
|
|
23
|
+
* Initializes a TinyNewWinEvents instance for communication.
|
|
24
|
+
*
|
|
25
|
+
* @param {Object} [settings={}] Configuration object.
|
|
26
|
+
* @param {string} [settings.targetOrigin] Origin to enforce in postMessage.
|
|
27
|
+
* @param {string} [settings.url] URL string to open.
|
|
28
|
+
* @param {string} [settings.name] Window name (required if opening a new window).
|
|
29
|
+
* @param {string} [settings.features] Features string for `window.open`.
|
|
30
|
+
*
|
|
31
|
+
* @throws {Error} If `name` is "_blank", which is not allowed.
|
|
32
|
+
* @throws {TypeError} If `targetOrigin`, `url`, or `features` are not strings (when provided).
|
|
33
|
+
* @throws {Error} If the window reference is invalid or already being tracked.
|
|
34
|
+
*/
|
|
35
|
+
constructor({ targetOrigin, url, name, features }?: {
|
|
36
|
+
targetOrigin?: string | undefined;
|
|
37
|
+
url?: string | undefined;
|
|
38
|
+
name?: string | undefined;
|
|
39
|
+
features?: string | undefined;
|
|
40
|
+
});
|
|
41
|
+
/**
|
|
42
|
+
* Enables or disables throwing an error when the maximum number of listeners is exceeded.
|
|
43
|
+
*
|
|
44
|
+
* @param {boolean} shouldThrow - If true, an error will be thrown when the max is exceeded.
|
|
45
|
+
*/
|
|
46
|
+
setThrowOnMaxListeners(shouldThrow: boolean): void;
|
|
47
|
+
/**
|
|
48
|
+
* Checks whether an error will be thrown when the max listener limit is exceeded.
|
|
49
|
+
*
|
|
50
|
+
* @returns {boolean} True if an error will be thrown, false if only a warning is shown.
|
|
51
|
+
*/
|
|
52
|
+
getThrowOnMaxListeners(): boolean;
|
|
53
|
+
/**
|
|
54
|
+
* Adds a listener to the beginning of the listeners array for the specified event.
|
|
55
|
+
*
|
|
56
|
+
* @param {string} event - Event name.
|
|
57
|
+
* @param {handler} handler - The callback function.
|
|
58
|
+
*/
|
|
59
|
+
prependListener(event: string, handler: handler): void;
|
|
60
|
+
/**
|
|
61
|
+
* Adds a one-time listener to the beginning of the listeners array for the specified event.
|
|
62
|
+
*
|
|
63
|
+
* @param {string} event - Event name.
|
|
64
|
+
* @param {handler} handler - The callback function.
|
|
65
|
+
* @returns {handler} - The wrapped handler used internally.
|
|
66
|
+
*/
|
|
67
|
+
prependListenerOnce(event: string, handler: handler): handler;
|
|
68
|
+
/**
|
|
69
|
+
* Adds a event listener.
|
|
70
|
+
*
|
|
71
|
+
* @param {string} event - Event name, such as 'onScrollBoundary' or 'onAutoScroll'.
|
|
72
|
+
* @param {handler} handler - Callback function to be called when event fires.
|
|
73
|
+
*/
|
|
74
|
+
appendListener(event: string, handler: handler): void;
|
|
75
|
+
/**
|
|
76
|
+
* Registers an event listener that runs only once, then is removed.
|
|
77
|
+
*
|
|
78
|
+
* @param {string} event - Event name, such as 'onScrollBoundary' or 'onAutoScroll'.
|
|
79
|
+
* @param {handler} handler - The callback function to run on event.
|
|
80
|
+
* @returns {handler} - The wrapped version of the handler.
|
|
81
|
+
*/
|
|
82
|
+
appendListenerOnce(event: string, handler: handler): handler;
|
|
83
|
+
/**
|
|
84
|
+
* Adds a event listener.
|
|
85
|
+
*
|
|
86
|
+
* @param {string} event - Event name, such as 'onScrollBoundary' or 'onAutoScroll'.
|
|
87
|
+
* @param {handler} handler - Callback function to be called when event fires.
|
|
88
|
+
*/
|
|
89
|
+
on(event: string, handler: handler): void;
|
|
90
|
+
/**
|
|
91
|
+
* Registers an event listener that runs only once, then is removed.
|
|
92
|
+
*
|
|
93
|
+
* @param {string} event - Event name, such as 'onScrollBoundary' or 'onAutoScroll'.
|
|
94
|
+
* @param {handler} handler - The callback function to run on event.
|
|
95
|
+
* @returns {handler} - The wrapped version of the handler.
|
|
96
|
+
*/
|
|
97
|
+
once(event: string, handler: handler): handler;
|
|
98
|
+
/**
|
|
99
|
+
* Removes a previously registered event listener.
|
|
100
|
+
*
|
|
101
|
+
* @param {string} event - The name of the event to remove the handler from.
|
|
102
|
+
* @param {handler} handler - The specific callback function to remove.
|
|
103
|
+
*/
|
|
104
|
+
off(event: string, handler: handler): void;
|
|
105
|
+
/**
|
|
106
|
+
* Removes all event listeners of a specific type from the element.
|
|
107
|
+
*
|
|
108
|
+
* @param {string} event - The event type to remove (e.g. 'onScrollBoundary').
|
|
109
|
+
*/
|
|
110
|
+
offAll(event: string): void;
|
|
111
|
+
/**
|
|
112
|
+
* Removes all event listeners of all types from the element.
|
|
113
|
+
*/
|
|
114
|
+
offAllTypes(): void;
|
|
115
|
+
/**
|
|
116
|
+
* Returns the number of listeners for a given event.
|
|
117
|
+
*
|
|
118
|
+
* @param {string} event - The name of the event.
|
|
119
|
+
* @returns {number} Number of listeners for the event.
|
|
120
|
+
*/
|
|
121
|
+
listenerCount(event: string): number;
|
|
122
|
+
/**
|
|
123
|
+
* Returns a copy of the array of listeners for the specified event.
|
|
124
|
+
*
|
|
125
|
+
* @param {string} event - The name of the event.
|
|
126
|
+
* @returns {handler[]} Array of listener functions.
|
|
127
|
+
*/
|
|
128
|
+
listeners(event: string): handler[];
|
|
129
|
+
/**
|
|
130
|
+
* Returns a copy of the array of listeners for the specified event.
|
|
131
|
+
*
|
|
132
|
+
* @param {string} event - The name of the event.
|
|
133
|
+
* @returns {handler[]} Array of listener functions.
|
|
134
|
+
*/
|
|
135
|
+
onceListeners(event: string): handler[];
|
|
136
|
+
/**
|
|
137
|
+
* Returns a copy of the internal listeners array for the specified event,
|
|
138
|
+
* including wrapper functions like those used by `.once()`.
|
|
139
|
+
* @param {string | symbol} event - The event name.
|
|
140
|
+
* @returns {handler[]} An array of raw listener functions.
|
|
141
|
+
*/
|
|
142
|
+
allListeners(event: string | symbol): handler[];
|
|
143
|
+
/**
|
|
144
|
+
* Returns an array of event names for which there are registered listeners.
|
|
145
|
+
*
|
|
146
|
+
* @returns {string[]} Array of registered event names.
|
|
147
|
+
*/
|
|
148
|
+
eventNames(): string[];
|
|
149
|
+
/**
|
|
150
|
+
* Sets the maximum number of listeners per event before a warning is shown.
|
|
151
|
+
*
|
|
152
|
+
* @param {number} n - The maximum number of listeners.
|
|
153
|
+
*/
|
|
154
|
+
setMaxListeners(n: number): void;
|
|
155
|
+
/**
|
|
156
|
+
* Gets the maximum number of listeners allowed per event.
|
|
157
|
+
*
|
|
158
|
+
* @returns {number} The maximum number of listeners.
|
|
159
|
+
*/
|
|
160
|
+
getMaxListeners(): number;
|
|
161
|
+
/**
|
|
162
|
+
* Sets the internal handshake event name.
|
|
163
|
+
* @param {string} name
|
|
164
|
+
* @throws {TypeError} If the value is not a string.
|
|
165
|
+
*/
|
|
166
|
+
set readyEventName(name: string);
|
|
167
|
+
/**
|
|
168
|
+
* Gets the internal handshake event name.
|
|
169
|
+
* @returns {string}
|
|
170
|
+
*/
|
|
171
|
+
get readyEventName(): string;
|
|
172
|
+
/**
|
|
173
|
+
* Sets the internal route event name.
|
|
174
|
+
* @param {string} name
|
|
175
|
+
* @throws {TypeError} If the value is not a string.
|
|
176
|
+
*/
|
|
177
|
+
set routeEventName(name: string);
|
|
178
|
+
/**
|
|
179
|
+
* Gets the internal route event name.
|
|
180
|
+
* @returns {string}
|
|
181
|
+
*/
|
|
182
|
+
get routeEventName(): string;
|
|
183
|
+
_handleMessage: (event: MessageEvent) => void;
|
|
184
|
+
/**
|
|
185
|
+
* Returns the internal window reference.
|
|
186
|
+
*
|
|
187
|
+
* @returns {Window|null}
|
|
188
|
+
*/
|
|
189
|
+
getWin(): Window | null;
|
|
190
|
+
/**
|
|
191
|
+
* Closes the child window (only allowed from the host).
|
|
192
|
+
*
|
|
193
|
+
* @returns {void}
|
|
194
|
+
*/
|
|
195
|
+
close(): void;
|
|
196
|
+
/**
|
|
197
|
+
* Emits a message to the other window on a specific route.
|
|
198
|
+
* If the handshake is not yet complete, the message is queued.
|
|
199
|
+
* Throws an error if the instance has already been destroyed.
|
|
200
|
+
*
|
|
201
|
+
* @param {string} route - Route name used to identify the message handler.
|
|
202
|
+
* @param {any} payload - Data to send along with the message.
|
|
203
|
+
* @throws {Error} If the instance is already destroyed.
|
|
204
|
+
* @returns {void}
|
|
205
|
+
*/
|
|
206
|
+
emit(route: string, payload: any): void;
|
|
207
|
+
/**
|
|
208
|
+
* Checks if the connection is active and the window is still open.
|
|
209
|
+
*
|
|
210
|
+
* @returns {boolean}
|
|
211
|
+
*/
|
|
212
|
+
isConnected(): boolean;
|
|
213
|
+
/**
|
|
214
|
+
* Registers a callback for when the window is closed.
|
|
215
|
+
*
|
|
216
|
+
* @param {handler} callback Callback to run on close
|
|
217
|
+
* @returns {void}
|
|
218
|
+
*/
|
|
219
|
+
onClose(callback: handler): void;
|
|
220
|
+
/**
|
|
221
|
+
* Unregisters a previously registered close callback.
|
|
222
|
+
*
|
|
223
|
+
* @param {handler} callback Callback to remove
|
|
224
|
+
* @returns {void}
|
|
225
|
+
*/
|
|
226
|
+
offClose(callback: handler): void;
|
|
227
|
+
/**
|
|
228
|
+
* Checks if the communication instance has been destroyed.
|
|
229
|
+
*
|
|
230
|
+
* @returns {boolean}
|
|
231
|
+
*/
|
|
232
|
+
isDestroyed(): boolean;
|
|
233
|
+
/**
|
|
234
|
+
* Destroys the communication instance, cleaning up all resources and listeners.
|
|
235
|
+
*
|
|
236
|
+
* @returns {void}
|
|
237
|
+
*/
|
|
238
|
+
destroy(): void;
|
|
239
|
+
#private;
|
|
240
|
+
}
|
|
241
|
+
//# sourceMappingURL=TinyNewWinEvents.d.mts.map
|
|
@@ -0,0 +1,437 @@
|
|
|
1
|
+
import TinyEvents from './TinyEvents.mjs';
|
|
2
|
+
/**
|
|
3
|
+
* Stores polling intervals associated with window references.
|
|
4
|
+
* Used to detect when the window is closed.
|
|
5
|
+
*
|
|
6
|
+
* @type {WeakMap<Window, NodeJS.Timeout>}
|
|
7
|
+
*/
|
|
8
|
+
const pollClosedInterval = new WeakMap();
|
|
9
|
+
/**
|
|
10
|
+
* @callback handler
|
|
11
|
+
* A function to handle incoming event payloads.
|
|
12
|
+
* @param {any} payload - The data sent by the emitter.
|
|
13
|
+
* @param {MessageEvent<any>} event - Metadata about the message.
|
|
14
|
+
*/
|
|
15
|
+
/**
|
|
16
|
+
* TinyNewWinEvents provides structured communication between a main window
|
|
17
|
+
* and a child window (created using window.open) using postMessage.
|
|
18
|
+
*
|
|
19
|
+
* It supports routing, queuing messages until handshake is ready,
|
|
20
|
+
* connection status checks, and close detection.
|
|
21
|
+
*
|
|
22
|
+
* @class
|
|
23
|
+
*/
|
|
24
|
+
class TinyNewWinEvents {
|
|
25
|
+
#events = new TinyEvents();
|
|
26
|
+
/**
|
|
27
|
+
* Enables or disables throwing an error when the maximum number of listeners is exceeded.
|
|
28
|
+
*
|
|
29
|
+
* @param {boolean} shouldThrow - If true, an error will be thrown when the max is exceeded.
|
|
30
|
+
*/
|
|
31
|
+
setThrowOnMaxListeners(shouldThrow) {
|
|
32
|
+
return this.#events.setThrowOnMaxListeners(shouldThrow);
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Checks whether an error will be thrown when the max listener limit is exceeded.
|
|
36
|
+
*
|
|
37
|
+
* @returns {boolean} True if an error will be thrown, false if only a warning is shown.
|
|
38
|
+
*/
|
|
39
|
+
getThrowOnMaxListeners() {
|
|
40
|
+
return this.#events.getThrowOnMaxListeners();
|
|
41
|
+
}
|
|
42
|
+
/////////////////////////////////////////////////////////////
|
|
43
|
+
/**
|
|
44
|
+
* Adds a listener to the beginning of the listeners array for the specified event.
|
|
45
|
+
*
|
|
46
|
+
* @param {string} event - Event name.
|
|
47
|
+
* @param {handler} handler - The callback function.
|
|
48
|
+
*/
|
|
49
|
+
prependListener(event, handler) {
|
|
50
|
+
return this.#events.prependListener(event, handler);
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Adds a one-time listener to the beginning of the listeners array for the specified event.
|
|
54
|
+
*
|
|
55
|
+
* @param {string} event - Event name.
|
|
56
|
+
* @param {handler} handler - The callback function.
|
|
57
|
+
* @returns {handler} - The wrapped handler used internally.
|
|
58
|
+
*/
|
|
59
|
+
prependListenerOnce(event, handler) {
|
|
60
|
+
return this.#events.prependListenerOnce(event, handler);
|
|
61
|
+
}
|
|
62
|
+
//////////////////////////////////////////////////////////////////////
|
|
63
|
+
/**
|
|
64
|
+
* Adds a event listener.
|
|
65
|
+
*
|
|
66
|
+
* @param {string} event - Event name, such as 'onScrollBoundary' or 'onAutoScroll'.
|
|
67
|
+
* @param {handler} handler - Callback function to be called when event fires.
|
|
68
|
+
*/
|
|
69
|
+
appendListener(event, handler) {
|
|
70
|
+
return this.#events.appendListener(event, handler);
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Registers an event listener that runs only once, then is removed.
|
|
74
|
+
*
|
|
75
|
+
* @param {string} event - Event name, such as 'onScrollBoundary' or 'onAutoScroll'.
|
|
76
|
+
* @param {handler} handler - The callback function to run on event.
|
|
77
|
+
* @returns {handler} - The wrapped version of the handler.
|
|
78
|
+
*/
|
|
79
|
+
appendListenerOnce(event, handler) {
|
|
80
|
+
return this.#events.appendListenerOnce(event, handler);
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Adds a event listener.
|
|
84
|
+
*
|
|
85
|
+
* @param {string} event - Event name, such as 'onScrollBoundary' or 'onAutoScroll'.
|
|
86
|
+
* @param {handler} handler - Callback function to be called when event fires.
|
|
87
|
+
*/
|
|
88
|
+
on(event, handler) {
|
|
89
|
+
return this.#events.on(event, handler);
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Registers an event listener that runs only once, then is removed.
|
|
93
|
+
*
|
|
94
|
+
* @param {string} event - Event name, such as 'onScrollBoundary' or 'onAutoScroll'.
|
|
95
|
+
* @param {handler} handler - The callback function to run on event.
|
|
96
|
+
* @returns {handler} - The wrapped version of the handler.
|
|
97
|
+
*/
|
|
98
|
+
once(event, handler) {
|
|
99
|
+
return this.#events.once(event, handler);
|
|
100
|
+
}
|
|
101
|
+
////////////////////////////////////////////////////////////////////
|
|
102
|
+
/**
|
|
103
|
+
* Removes a previously registered event listener.
|
|
104
|
+
*
|
|
105
|
+
* @param {string} event - The name of the event to remove the handler from.
|
|
106
|
+
* @param {handler} handler - The specific callback function to remove.
|
|
107
|
+
*/
|
|
108
|
+
off(event, handler) {
|
|
109
|
+
return this.#events.off(event, handler);
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Removes all event listeners of a specific type from the element.
|
|
113
|
+
*
|
|
114
|
+
* @param {string} event - The event type to remove (e.g. 'onScrollBoundary').
|
|
115
|
+
*/
|
|
116
|
+
offAll(event) {
|
|
117
|
+
return this.#events.offAll(event);
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Removes all event listeners of all types from the element.
|
|
121
|
+
*/
|
|
122
|
+
offAllTypes() {
|
|
123
|
+
return this.#events.offAllTypes();
|
|
124
|
+
}
|
|
125
|
+
////////////////////////////////////////////////////////////
|
|
126
|
+
/**
|
|
127
|
+
* Returns the number of listeners for a given event.
|
|
128
|
+
*
|
|
129
|
+
* @param {string} event - The name of the event.
|
|
130
|
+
* @returns {number} Number of listeners for the event.
|
|
131
|
+
*/
|
|
132
|
+
listenerCount(event) {
|
|
133
|
+
return this.#events.listenerCount(event);
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Returns a copy of the array of listeners for the specified event.
|
|
137
|
+
*
|
|
138
|
+
* @param {string} event - The name of the event.
|
|
139
|
+
* @returns {handler[]} Array of listener functions.
|
|
140
|
+
*/
|
|
141
|
+
listeners(event) {
|
|
142
|
+
return this.#events.listeners(event);
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Returns a copy of the array of listeners for the specified event.
|
|
146
|
+
*
|
|
147
|
+
* @param {string} event - The name of the event.
|
|
148
|
+
* @returns {handler[]} Array of listener functions.
|
|
149
|
+
*/
|
|
150
|
+
onceListeners(event) {
|
|
151
|
+
return this.#events.onceListeners(event);
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* Returns a copy of the internal listeners array for the specified event,
|
|
155
|
+
* including wrapper functions like those used by `.once()`.
|
|
156
|
+
* @param {string | symbol} event - The event name.
|
|
157
|
+
* @returns {handler[]} An array of raw listener functions.
|
|
158
|
+
*/
|
|
159
|
+
allListeners(event) {
|
|
160
|
+
return this.#events.allListeners(event);
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* Returns an array of event names for which there are registered listeners.
|
|
164
|
+
*
|
|
165
|
+
* @returns {string[]} Array of registered event names.
|
|
166
|
+
*/
|
|
167
|
+
eventNames() {
|
|
168
|
+
return this.#events.eventNames();
|
|
169
|
+
}
|
|
170
|
+
//////////////////////////////////////////////////////
|
|
171
|
+
/**
|
|
172
|
+
* Sets the maximum number of listeners per event before a warning is shown.
|
|
173
|
+
*
|
|
174
|
+
* @param {number} n - The maximum number of listeners.
|
|
175
|
+
*/
|
|
176
|
+
setMaxListeners(n) {
|
|
177
|
+
return this.#events.setMaxListeners(n);
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* Gets the maximum number of listeners allowed per event.
|
|
181
|
+
*
|
|
182
|
+
* @returns {number} The maximum number of listeners.
|
|
183
|
+
*/
|
|
184
|
+
getMaxListeners() {
|
|
185
|
+
return this.#events.getMaxListeners();
|
|
186
|
+
}
|
|
187
|
+
///////////////////////////////////////////////////
|
|
188
|
+
/** @type {Window|null} Reference to the opened or parent window */
|
|
189
|
+
#windowRef;
|
|
190
|
+
/** @type {string} Expected origin for postMessage communication */
|
|
191
|
+
#targetOrigin;
|
|
192
|
+
/** @type {{ route: string, payload: any }[]} Queue of messages emitted before connection is ready */
|
|
193
|
+
#pendingQueue = [];
|
|
194
|
+
/** @type {boolean} True if handshake between windows is complete */
|
|
195
|
+
#ready = false;
|
|
196
|
+
/** @type {boolean} True if this instance is the main window (host) */
|
|
197
|
+
#isHost = false;
|
|
198
|
+
/** @type {NodeJS.Timeout|null} Interval for polling child window closure */
|
|
199
|
+
#pollClosedInterval = null;
|
|
200
|
+
/** @type {string} Internal message type for handshake */
|
|
201
|
+
#readyEventName = '__TNE_READY__';
|
|
202
|
+
/** @type {string} Internal message type for routed communication */
|
|
203
|
+
#routeEventName = '__TNE_ROUTE__';
|
|
204
|
+
/**
|
|
205
|
+
* Gets the internal handshake event name.
|
|
206
|
+
* @returns {string}
|
|
207
|
+
*/
|
|
208
|
+
get readyEventName() {
|
|
209
|
+
return this.#readyEventName;
|
|
210
|
+
}
|
|
211
|
+
/**
|
|
212
|
+
* Sets the internal handshake event name.
|
|
213
|
+
* @param {string} name
|
|
214
|
+
* @throws {TypeError} If the value is not a string.
|
|
215
|
+
*/
|
|
216
|
+
set readyEventName(name) {
|
|
217
|
+
if (typeof name !== 'string')
|
|
218
|
+
throw new TypeError('TinyNewWinEvents: readyEventName must be a string.');
|
|
219
|
+
this.#readyEventName = name;
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* Gets the internal route event name.
|
|
223
|
+
* @returns {string}
|
|
224
|
+
*/
|
|
225
|
+
get routeEventName() {
|
|
226
|
+
return this.#routeEventName;
|
|
227
|
+
}
|
|
228
|
+
/**
|
|
229
|
+
* Sets the internal route event name.
|
|
230
|
+
* @param {string} name
|
|
231
|
+
* @throws {TypeError} If the value is not a string.
|
|
232
|
+
*/
|
|
233
|
+
set routeEventName(name) {
|
|
234
|
+
if (typeof name !== 'string')
|
|
235
|
+
throw new TypeError('TinyNewWinEvents: routeEventName must be a string.');
|
|
236
|
+
this.#routeEventName = name;
|
|
237
|
+
}
|
|
238
|
+
/**
|
|
239
|
+
* Initializes a TinyNewWinEvents instance for communication.
|
|
240
|
+
*
|
|
241
|
+
* @param {Object} [settings={}] Configuration object.
|
|
242
|
+
* @param {string} [settings.targetOrigin] Origin to enforce in postMessage.
|
|
243
|
+
* @param {string} [settings.url] URL string to open.
|
|
244
|
+
* @param {string} [settings.name] Window name (required if opening a new window).
|
|
245
|
+
* @param {string} [settings.features] Features string for `window.open`.
|
|
246
|
+
*
|
|
247
|
+
* @throws {Error} If `name` is "_blank", which is not allowed.
|
|
248
|
+
* @throws {TypeError} If `targetOrigin`, `url`, or `features` are not strings (when provided).
|
|
249
|
+
* @throws {Error} If the window reference is invalid or already being tracked.
|
|
250
|
+
*/
|
|
251
|
+
constructor({ targetOrigin, url, name, features } = {}) {
|
|
252
|
+
if (typeof name === 'string' && name === '_blank')
|
|
253
|
+
throw new Error('TinyNewWinEvents: The window name "_blank" is not supported. Please use a custom name to allow tracking.');
|
|
254
|
+
if (typeof targetOrigin !== 'undefined' && typeof targetOrigin !== 'string')
|
|
255
|
+
throw new TypeError('TinyNewWinEvents: The "targetOrigin" option must be a string.');
|
|
256
|
+
if (typeof url !== 'undefined' && typeof url !== 'string')
|
|
257
|
+
throw new TypeError('TinyNewWinEvents: The "url" option must be a string.');
|
|
258
|
+
if (typeof features !== 'undefined' && typeof features !== 'string')
|
|
259
|
+
throw new TypeError('TinyNewWinEvents: The "features" option must be a string if provided.');
|
|
260
|
+
// Open Page
|
|
261
|
+
if (typeof url === 'undefined')
|
|
262
|
+
this.#windowRef = window.opener;
|
|
263
|
+
// Main Page
|
|
264
|
+
else {
|
|
265
|
+
this.#windowRef = typeof url === 'string' ? window.open(url, name, features) : url;
|
|
266
|
+
this.#isHost = true;
|
|
267
|
+
}
|
|
268
|
+
if (!this.#windowRef || pollClosedInterval.has(this.#windowRef))
|
|
269
|
+
throw new Error('Invalid or duplicate window reference.');
|
|
270
|
+
this.#targetOrigin = targetOrigin ?? window.location.origin;
|
|
271
|
+
this._handleMessage = this.#handleMessage.bind(this);
|
|
272
|
+
window.addEventListener('message', this._handleMessage, false);
|
|
273
|
+
// Sends handshake if it is host (main window)
|
|
274
|
+
if (!this.#isHost) {
|
|
275
|
+
this.#postRaw(this.#readyEventName, null);
|
|
276
|
+
this.#startCloseWatcher();
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
/**
|
|
280
|
+
* Returns the internal window reference.
|
|
281
|
+
*
|
|
282
|
+
* @returns {Window|null}
|
|
283
|
+
*/
|
|
284
|
+
getWin() {
|
|
285
|
+
return this.#windowRef;
|
|
286
|
+
}
|
|
287
|
+
/**
|
|
288
|
+
* Internal message handler.
|
|
289
|
+
*
|
|
290
|
+
* @param {MessageEvent} event
|
|
291
|
+
* @returns {void}
|
|
292
|
+
*/
|
|
293
|
+
#handleMessage(event) {
|
|
294
|
+
if (!event.source || (this.#windowRef && event.source !== this.#windowRef))
|
|
295
|
+
return;
|
|
296
|
+
const { type, route, payload } = event.data || {};
|
|
297
|
+
if (type === this.#readyEventName) {
|
|
298
|
+
this.#ready = true;
|
|
299
|
+
this.#flushQueue();
|
|
300
|
+
this.#startCloseWatcher(); // start watcher after handshake (for child window)
|
|
301
|
+
if (this.#isHost)
|
|
302
|
+
this.#postRaw(this.#readyEventName, null);
|
|
303
|
+
return;
|
|
304
|
+
}
|
|
305
|
+
if (type === this.#routeEventName)
|
|
306
|
+
this.#events.emit(route, payload, event);
|
|
307
|
+
}
|
|
308
|
+
/**
|
|
309
|
+
* Sends all pending messages queued before handshake completion.
|
|
310
|
+
*
|
|
311
|
+
* @returns {void}
|
|
312
|
+
*/
|
|
313
|
+
#flushQueue() {
|
|
314
|
+
while (this.#pendingQueue.length) {
|
|
315
|
+
const data = this.#pendingQueue.shift();
|
|
316
|
+
if (data) {
|
|
317
|
+
const { route, payload } = data;
|
|
318
|
+
this.emit(route, payload);
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
/**
|
|
323
|
+
* Sends a raw postMessage with given type and payload.
|
|
324
|
+
*
|
|
325
|
+
* @param {string} type Internal message type
|
|
326
|
+
* @param {any} payload Data to send
|
|
327
|
+
* @param {string} [route=''] Optional route name
|
|
328
|
+
* @returns {void}
|
|
329
|
+
*/
|
|
330
|
+
#postRaw(type, payload, route = '') {
|
|
331
|
+
if (this.#windowRef && this.#windowRef.closed)
|
|
332
|
+
return;
|
|
333
|
+
this.#windowRef?.postMessage({ type, route, payload }, this.#targetOrigin);
|
|
334
|
+
}
|
|
335
|
+
/**
|
|
336
|
+
* Closes the child window (only allowed from the host).
|
|
337
|
+
*
|
|
338
|
+
* @returns {void}
|
|
339
|
+
*/
|
|
340
|
+
close() {
|
|
341
|
+
if (!this.#isHost)
|
|
342
|
+
throw new Error('Only host can close the window.');
|
|
343
|
+
if (this.#windowRef && !this.#windowRef.closed)
|
|
344
|
+
this.#windowRef.close();
|
|
345
|
+
}
|
|
346
|
+
/**
|
|
347
|
+
* Emits a message to the other window on a specific route.
|
|
348
|
+
* If the handshake is not yet complete, the message is queued.
|
|
349
|
+
* Throws an error if the instance has already been destroyed.
|
|
350
|
+
*
|
|
351
|
+
* @param {string} route - Route name used to identify the message handler.
|
|
352
|
+
* @param {any} payload - Data to send along with the message.
|
|
353
|
+
* @throws {Error} If the instance is already destroyed.
|
|
354
|
+
* @returns {void}
|
|
355
|
+
*/
|
|
356
|
+
emit(route, payload) {
|
|
357
|
+
if (typeof route !== 'string')
|
|
358
|
+
throw new TypeError('Event name must be a string.');
|
|
359
|
+
if (this.isDestroyed())
|
|
360
|
+
throw new Error('Cannot emit: instance has been destroyed.');
|
|
361
|
+
if (!this.#ready) {
|
|
362
|
+
this.#pendingQueue.push({ route, payload });
|
|
363
|
+
return;
|
|
364
|
+
}
|
|
365
|
+
this.#postRaw(this.#routeEventName, payload, route);
|
|
366
|
+
}
|
|
367
|
+
/**
|
|
368
|
+
* Checks if the connection is active and the window is still open.
|
|
369
|
+
*
|
|
370
|
+
* @returns {boolean}
|
|
371
|
+
*/
|
|
372
|
+
isConnected() {
|
|
373
|
+
return this.#ready && this.#windowRef && !this.#windowRef.closed ? true : false;
|
|
374
|
+
}
|
|
375
|
+
/**
|
|
376
|
+
* Starts polling to detect when the window is closed.
|
|
377
|
+
*
|
|
378
|
+
* @returns {void}
|
|
379
|
+
*/
|
|
380
|
+
#startCloseWatcher() {
|
|
381
|
+
if (!this.#windowRef || this.#pollClosedInterval)
|
|
382
|
+
return;
|
|
383
|
+
this.#pollClosedInterval = setInterval(() => {
|
|
384
|
+
if (this.#windowRef?.closed) {
|
|
385
|
+
this.#events.emit('WINDOW_REF_CLOSED');
|
|
386
|
+
this.destroy();
|
|
387
|
+
}
|
|
388
|
+
}, 500);
|
|
389
|
+
pollClosedInterval.set(this.#windowRef, this.#pollClosedInterval);
|
|
390
|
+
}
|
|
391
|
+
/**
|
|
392
|
+
* Registers a callback for when the window is closed.
|
|
393
|
+
*
|
|
394
|
+
* @param {handler} callback Callback to run on close
|
|
395
|
+
* @returns {void}
|
|
396
|
+
*/
|
|
397
|
+
onClose(callback) {
|
|
398
|
+
return this.#events.on('WINDOW_REF_CLOSED', callback);
|
|
399
|
+
}
|
|
400
|
+
/**
|
|
401
|
+
* Unregisters a previously registered close callback.
|
|
402
|
+
*
|
|
403
|
+
* @param {handler} callback Callback to remove
|
|
404
|
+
* @returns {void}
|
|
405
|
+
*/
|
|
406
|
+
offClose(callback) {
|
|
407
|
+
return this.#events.off('WINDOW_REF_CLOSED', callback);
|
|
408
|
+
}
|
|
409
|
+
/**
|
|
410
|
+
* Checks if the communication instance has been destroyed.
|
|
411
|
+
*
|
|
412
|
+
* @returns {boolean}
|
|
413
|
+
*/
|
|
414
|
+
isDestroyed() {
|
|
415
|
+
return !this.#windowRef;
|
|
416
|
+
}
|
|
417
|
+
/**
|
|
418
|
+
* Destroys the communication instance, cleaning up all resources and listeners.
|
|
419
|
+
*
|
|
420
|
+
* @returns {void}
|
|
421
|
+
*/
|
|
422
|
+
destroy() {
|
|
423
|
+
if (!this.#windowRef)
|
|
424
|
+
return;
|
|
425
|
+
if (this.#pollClosedInterval) {
|
|
426
|
+
clearInterval(this.#pollClosedInterval);
|
|
427
|
+
this.#pollClosedInterval = null;
|
|
428
|
+
pollClosedInterval.delete(this.#windowRef);
|
|
429
|
+
}
|
|
430
|
+
window.removeEventListener('message', this._handleMessage);
|
|
431
|
+
this.#pendingQueue = [];
|
|
432
|
+
this.#ready = false;
|
|
433
|
+
this.#windowRef = null;
|
|
434
|
+
this.#events.offAllTypes();
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
export default TinyNewWinEvents;
|
package/docs/v1/README.md
CHANGED
|
@@ -44,6 +44,8 @@ This folder contains the core scripts we have worked on so far. Each file is a m
|
|
|
44
44
|
- 🌈 **[TinyColorConverter](./libs/TinyColorConverter.md)** — A complete color conversion toolkit supporting hex, RGB(A), HSL(A), and integer formats, with smooth gradient generation, color parsing, and multi-format output conversion.
|
|
45
45
|
- 📡 **[TinyEvents](./libs/TinyEvents.md)** — A lightweight and dependency-free event emitter inspired by Node.js, supporting persistent and one-time listeners, listener inspection, and max listener limits.
|
|
46
46
|
- 📦 **[TinyLocalStorage](./libs/TinyLocalStorage.md)** — A tiny wrapper for `localStorage` with full support for objects, arrays, `Map`, `Set`, and typed value helpers like string, number, and boolean.
|
|
47
|
+
- 🖼️ **[TinyIframeEvents](./libs/TinyIframeEvents.md)** — A structured `postMessage`-based event router for secure and reliable communication between a parent window and its embedded iframe. Supports directional filtering, origin enforcement, payload transport, and listener lifecycle.
|
|
48
|
+
- 🪟 **[TinyNewWinEvents](./libs/TinyNewWinEvents.md)** — A smart, route-based `postMessage` system for structured communication between a main window and a popup (`window.open`). Includes queueing, origin enforcement, and lifecycle tracking.
|
|
47
49
|
|
|
48
50
|
### 3. **`fileManager/`**
|
|
49
51
|
* 📁 **[Main](./fileManager/main.md)** — A Node.js file/directory utility module with support for JSON, backups, renaming, size analysis, and more.
|