tiny-essentials 1.19.3 โ†’ 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.
@@ -0,0 +1,149 @@
1
+ # ๐Ÿงฉ TinyIframeEvents
2
+
3
+ A flexible event routing system for structured communication between a **parent window** and its **iframe** using postMessage.
4
+
5
+ This module abstracts the complexity of origin validation, window detection, and message direction. It allows **modular, bidirectional, and secure communication** across frames.
6
+
7
+ ---
8
+
9
+ ## โœจ Features
10
+
11
+ - ๐Ÿ” Bidirectional communication (`iframe` โ‡„ `parent`)
12
+ - ๐ŸŽฏ Named event routes with custom payloads
13
+ - ๐Ÿ” Target window and origin validation
14
+ - ๐Ÿง  Smart context detection (no config required inside iframe)
15
+ - ๐Ÿงน Clean-up support with `.destroy()`
16
+
17
+ ---
18
+
19
+ ## ๐Ÿš€ Usage
20
+
21
+ ### In the parent:
22
+
23
+ ```js
24
+ import TinyIframeEvents from './TinyIframeEvents.mjs';
25
+
26
+ const iframe = document.querySelector('iframe');
27
+
28
+ const parentEvents = new TinyIframeEvents({
29
+ targetIframe: iframe, // Will use iframe.contentWindow
30
+ targetOrigin: window.location.origin
31
+ });
32
+
33
+ parentEvents.emit('hello:iframe', { text: '๐Ÿ‘‹ From parent!' });
34
+
35
+ parentEvents.on('reply:fromIframe', (data, event) => {
36
+ console.log('๐Ÿ“จ Received from iframe:', data, event);
37
+ });
38
+ ```
39
+
40
+ ---
41
+
42
+ ### In the iframe:
43
+
44
+ ```js
45
+ import TinyIframeEvents from './TinyIframeEvents.mjs';
46
+
47
+ const iframeEvents = new TinyIframeEvents();
48
+
49
+ iframeEvents.on('hello:iframe', (data, event) => {
50
+ console.log('๐Ÿ“ฅ Message from parent:', event);
51
+ });
52
+
53
+ iframeEvents.emit('reply:fromIframe', { text: '๐Ÿ™‹โ€โ™€๏ธ Hi parent!' });
54
+ ```
55
+
56
+ ---
57
+
58
+ ## ๐Ÿง  API Reference
59
+
60
+ ### `new TinyIframeEvents(config?)`
61
+
62
+ Creates a new instance for communication.
63
+
64
+ #### Parameters:
65
+
66
+ | Name | Type | Description |
67
+ | -------------- | -------------------- | --------------------------------------------------------------------- |
68
+ | `targetIframe` | `HTMLIFrameElement?` | The iframe element to communicate with (used in the parent only). |
69
+ | `targetOrigin` | `string?` | Expected origin (for security). Defaults to `window.location.origin`. |
70
+
71
+ ---
72
+
73
+ ### `emit(eventName, payload)`
74
+
75
+ Sends a message to the target frame.
76
+
77
+ | Param | Type | Description |
78
+ | ----------- | -------- | ------------------------------------------ |
79
+ | `eventName` | `string` | Unique identifier of the event. |
80
+ | `payload` | `any` | The data to send (any serializable value). |
81
+
82
+ #### Throws:
83
+
84
+ * `TypeError` if `eventName` is not a string.
85
+
86
+ ---
87
+
88
+ ### `on(eventName, handler)`
89
+
90
+ Registers a listener for a specific event.
91
+
92
+ ```js
93
+ iframeEvents.on('my:event', (payload, event) => {
94
+ // event = { origin, source }
95
+ });
96
+ ```
97
+
98
+ ---
99
+
100
+ ### `destroy()`
101
+
102
+ Removes all listeners and message event binding.
103
+ Call this when the instance is no longer needed.
104
+
105
+ ```js
106
+ iframeEvents.destroy();
107
+ ```
108
+
109
+ ---
110
+
111
+ ### `isDestroyed()`
112
+
113
+ Returns `true` if the instance has been destroyed via `.destroy()`.
114
+
115
+ ```js
116
+ if (events.isDestroyed()) {
117
+ console.warn('Event system is no longer active.');
118
+ }
119
+ ```
120
+
121
+ ---
122
+
123
+ ## ๐Ÿ”’ Security Notes
124
+
125
+ * Only messages with the flag `__tinyEvent: true` are accepted.
126
+ * The source window is checked strictly (`source === targetWindow`).
127
+ * Messages are ignored unless their `direction` matches the current side (`iframe` or `parent`).
128
+
129
+ ---
130
+
131
+ ## ๐Ÿ“š Internals (for advanced users)
132
+
133
+ This class internally wraps:
134
+
135
+ * postMessage
136
+ * A minimal event router: `TinyEvents`
137
+
138
+ ---
139
+
140
+ ## ๐Ÿ“„ Type Definitions
141
+
142
+ ### `@callback handler`
143
+
144
+ ```ts
145
+ type handler = (
146
+ payload: any,
147
+ event: MessageEvent<any>
148
+ ) => void;
149
+ ```
@@ -0,0 +1,186 @@
1
+ # ๐ŸชŸ TinyNewWinEvents
2
+
3
+ Structured and reliable `postMessage`-based communication between a **main window** and a **popup window** (created using `window.open`).
4
+ Ideal for secure, bidirectional messaging with route-based logic.
5
+
6
+ ---
7
+
8
+ ## โœจ Features
9
+
10
+ * ๐Ÿ” Bidirectional communication (`main window` โ‡„ `popup`)
11
+ * ๐Ÿ“ฌ Named message routes with flexible payloads
12
+ * ๐Ÿ•“ Queues messages before handshake is ready
13
+ * ๐Ÿง  Connection and lifecycle status tracking
14
+ * ๐Ÿ”’ Origin and window reference validation
15
+ * ๐Ÿงน Full cleanup support with `.destroy()`
16
+
17
+ ---
18
+
19
+ ## ๐Ÿš€ Usage
20
+
21
+ ### In the main window:
22
+
23
+ ```js
24
+ import TinyNewWinEvents from './TinyNewWinEvents.mjs';
25
+
26
+ const events = new TinyNewWinEvents({
27
+ url: '/popup.html',
28
+ name: 'MyPopupWindow',
29
+ features: 'width=400,height=400',
30
+ targetOrigin: window.location.origin
31
+ });
32
+
33
+ events.on('user:reply', (payload) => {
34
+ console.log('๐Ÿ“ฉ From popup:', payload);
35
+ });
36
+
37
+ events.emit('init:data', { id: 123 });
38
+ ```
39
+
40
+ ---
41
+
42
+ ### In the popup window:
43
+
44
+ ```js
45
+ import TinyNewWinEvents from './TinyNewWinEvents.mjs';
46
+
47
+ const events = new TinyNewWinEvents();
48
+
49
+ events.on('init:data', (payload) => {
50
+ console.log('๐Ÿ“ฆ Init payload from main window:', payload);
51
+ });
52
+
53
+ events.emit('user:reply', { msg: '๐Ÿ‘‹ Hello from popup!' });
54
+ ```
55
+
56
+ ---
57
+
58
+ ## ๐Ÿง  API Reference
59
+
60
+ ### `new TinyNewWinEvents(config?)`
61
+
62
+ Creates a new communication instance.
63
+
64
+ #### Parameters:
65
+
66
+ | Name | Type | Description |
67
+ | -------------- | ------------------------ | -------------------------------------------------- |
68
+ | `url` | `string?` | URL to open in popup. |
69
+ | `name` | `string?` | Name of the popup window . |
70
+ | `features` | `string?` | `window.open` features string. |
71
+ | `targetOrigin` | `string?` | Expected origin (defaults to current origin) |
72
+
73
+ โ— `name: '_blank'` is **not allowed** for popup tracking.
74
+
75
+ ---
76
+
77
+ ### `emit(route, payload)`
78
+
79
+ Sends a message on a specific route.
80
+
81
+ | Param | Type | Description |
82
+ | --------- | -------- | ------------------------------- |
83
+ | `route` | `string` | Event route name |
84
+ | `payload` | `any` | Data to send (any serializable) |
85
+
86
+ ๐Ÿ“ If the handshake hasn't completed yet, the message is **queued**.
87
+
88
+ ---
89
+
90
+ ### `on(route, handler)`
91
+
92
+ Registers a callback for a specific route.
93
+
94
+ ```js
95
+ events.on('data:sync', (payload, event) => {
96
+ console.log(payload, event.origin);
97
+ });
98
+ ```
99
+
100
+ ---
101
+
102
+ ### `off(route, handler)`
103
+
104
+ Removes a previously registered route listener.
105
+
106
+ ---
107
+
108
+ ### `onClose(callback)`
109
+
110
+ Runs when the **popup window** is closed.
111
+
112
+ ---
113
+
114
+ ### `offClose(callback)`
115
+
116
+ Removes a previously registered close callback.
117
+
118
+ ---
119
+
120
+ ### `close()`
121
+
122
+ Closes the popup window (can only be called from the host).
123
+
124
+ ---
125
+
126
+ ### `getWin()`
127
+
128
+ Returns the current window reference (host or popup).
129
+
130
+ ---
131
+
132
+ ### `isConnected()`
133
+
134
+ Returns `true` if both:
135
+
136
+ * Handshake is complete
137
+ * Window is still open
138
+
139
+ ---
140
+
141
+ ### `isDestroyed()`
142
+
143
+ Returns `true` if the instance has been destroyed.
144
+
145
+ ---
146
+
147
+ ### `destroy()`
148
+
149
+ Destroys the instance, cleans up all resources:
150
+
151
+ * Stops polling
152
+ * Removes all listeners
153
+ * Clears queue
154
+
155
+ ---
156
+
157
+ ## ๐Ÿ“ฆ Internals (for advanced use)
158
+
159
+ | Concept | Purpose |
160
+ | --------------- | ------------------------------------------- |
161
+ | `postMessage` | Core transport for messaging |
162
+ | `TinyEvents` | Internal event emitter and listener manager |
163
+ | `__TNE_READY__` | Handshake trigger message type |
164
+ | `__TNE_ROUTE__` | Route-based message delivery type |
165
+
166
+ ---
167
+
168
+ ## ๐Ÿ“„ Type Definitions
169
+
170
+ ### `@callback handler`
171
+
172
+ ```ts
173
+ type handler = (
174
+ payload: any,
175
+ event: MessageEvent<any>
176
+ ) => void;
177
+ ```
178
+
179
+ ---
180
+
181
+ ## ๐Ÿ›ก๏ธ Safety and Validation
182
+
183
+ * ๐Ÿ’ฅ Throws if `emit()` is called after `.destroy()`
184
+ * ๐Ÿ’ฃ Throws if the popup was opened with `_blank` name
185
+ * โœ… Only communicates with expected `targetOrigin`
186
+ * ๐Ÿงผ Automatically detects closed windows and emits `WINDOW_REF_CLOSED`
@@ -80,7 +80,7 @@ TinyNotifyCenter.insertTemplate();
80
80
 
81
81
  // Or insert at the end of <body>
82
82
  TinyNotifyCenter.insertTemplate('beforeend');
83
- ````
83
+ ```
84
84
 
85
85
  ---
86
86
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tiny-essentials",
3
- "version": "1.19.3",
3
+ "version": "1.20.0",
4
4
  "description": "Collection of small, essential scripts designed to be used across various projects. These simple utilities are crafted for speed, ease of use, and versatility.",
5
5
  "scripts": {
6
6
  "test": "npm run test:mjs && npm run test:cjs && npm run test:js",