tiny-essentials
Version:
Collection of small, essential scripts designed to be used across various projects. These simple utilities are crafted for speed, ease of use, and versatility.
187 lines (117 loc) โข 4.15 kB
Markdown
# ๐ช TinyNewWinEvents
Structured and reliable `postMessage`-based communication between a **main window** and a **popup window** (created using `window.open`).
Ideal for secure, bidirectional messaging with route-based logic.
## โจ Features
* ๐ Bidirectional communication (`main window` โ `popup`)
* ๐ฌ Named message routes with flexible payloads
* ๐ Queues messages before handshake is ready
* ๐ง Connection and lifecycle status tracking
* ๐ Origin and window reference validation
* ๐งน Full cleanup support with `.destroy()`
## ๐ Usage
### In the main window:
```js
import TinyNewWinEvents from './TinyNewWinEvents.mjs';
const events = new TinyNewWinEvents({
url: '/popup.html',
name: 'MyPopupWindow',
features: 'width=400,height=400',
targetOrigin: window.location.origin
});
events.on('user:reply', (payload) => {
console.log('๐ฉ From popup:', payload);
});
events.emit('init:data', { id: 123 });
```
### In the popup window:
```js
import TinyNewWinEvents from './TinyNewWinEvents.mjs';
const events = new TinyNewWinEvents();
events.on('init:data', (payload) => {
console.log('๐ฆ Init payload from main window:', payload);
});
events.emit('user:reply', { msg: '๐ Hello from popup!' });
```
## ๐ง API Reference
### `new TinyNewWinEvents(config?)`
Creates a new communication instance.
#### Parameters:
| Name | Type | Description |
| -------------- | ------------------------ | -------------------------------------------------- |
| `url` | `string?` | URL to open in popup. |
| `name` | `string?` | Name of the popup window . |
| `features` | `string?` | `window.open` features string. |
| `targetOrigin` | `string?` | Expected origin (defaults to current origin) |
โ `name: '_blank'` is **not allowed** for popup tracking.
### `emit(route, payload)`
Sends a message on a specific route.
| Param | Type | Description |
| --------- | -------- | ------------------------------- |
| `route` | `string` | Event route name |
| `payload` | `any` | Data to send (any serializable) |
๐ If the handshake hasn't completed yet, the message is **queued**.
### `on(route, handler)`
Registers a callback for a specific route.
```js
events.on('data:sync', (payload, event) => {
console.log(payload, event.origin);
});
```
### `off(route, handler)`
Removes a previously registered route listener.
### `onClose(callback)`
Runs when the **popup window** is closed.
### `offClose(callback)`
Removes a previously registered close callback.
### `close()`
Closes the popup window (can only be called from the host).
### `getWin()`
Returns the current window reference (host or popup).
### `isConnected()`
Returns `true` if both:
* Handshake is complete
* Window is still open
### `isDestroyed()`
Returns `true` if the instance has been destroyed.
### `destroy()`
Destroys the instance, cleans up all resources:
* Stops polling
* Removes all listeners
* Clears queue
## ๐ฆ Internals (for advanced use)
| Concept | Purpose |
| --------------- | ------------------------------------------- |
| `postMessage` | Core transport for messaging |
| `TinyEvents` | Internal event emitter and listener manager |
| `__TNE_READY__` | Handshake trigger message type |
| `__TNE_ROUTE__` | Route-based message delivery type |
## ๐ Type Definitions
### `@callback handler`
```ts
type handler = (
payload: any,
event: MessageEvent<any>
) => void;
```
## ๐ก๏ธ Safety and Validation
* ๐ฅ Throws if `emit()` is called after `.destroy()`
* ๐ฃ Throws if the popup was opened with `_blank` name
* โ
Only communicates with expected `targetOrigin`
* ๐งผ Automatically detects closed windows and emits `WINDOW_REF_CLOSED`