# Lifecycle How a widget starts, what `onWidgetLoad` gives it, when it reloads, and how the event queue plays alerts one at a time. ## Load order 1. The page is built: your CSS in ``, your HTML in ``, your JS at the end of ``. `{{field}}` placeholders are already replaced. jQuery (`$`) is loaded before your code. 2. Your JS runs. Add your event listeners here (or in jQuery's ready handler or `window.onload`: they run in time, too). 3. After the window's `load` event (at the latest 3 seconds after the HTML is ready, so a slow external file cannot hold it up), `onWidgetLoad` fires, once. 4. Only after that are events delivered. Events that came in meanwhile are delivered then, in order. ## onWidgetLoad ```js window.addEventListener('onWidgetLoad', (obj) => { const { channel, currency, fieldData, recents, session, overlay } = obj.detail; }); ``` | Key | Value | |---|---| | `fieldData` | The values of your settings. [Fields](https://streamed.hu/docs/fields.md) | | `session.data` | Counters, latest events, top donations, goals. [Session data](https://streamed.hu/docs/session-data.md) | | `recents` | Up to 25 recent events of all kinds (no chat), newest first: `{ type, name, amount, createdAt, tier? }` | | `channel` | The first Twitch account the overlay is connected to, else its first YouTube channel: `{ username, displayName, providerId, id, avatar, apiToken }`. `id`, `avatar` and `apiToken` are always empty. | | `currency` | Always `{ code: "USD", name: "U.S. Dollar", symbol: "$" }`. The currency of a tip is in the tip event. | | `overlay` | `{ isEditorMode, muted }`. `isEditorMode` is `true` in the editor's preview; `muted` is always `false`. | Example: ```json { "channel": { "username": "yourchannel", "displayName": "YourChannel", "providerId": "123456789", "id": "", "avatar": "", "apiToken": "" }, "currency": { "code": "USD", "name": "U.S. Dollar", "symbol": "$" }, "fieldData": { "title": "New follower!", "fontSize": 32 }, "recents": [ { "name": "PixelPanda", "amount": 500, "type": "cheer", "createdAt": "2026-12-01T18:30:00.000Z" }, { "name": "LuckyViewer", "amount": 1, "type": "subscriber", "createdAt": "2026-12-01T18:10:00.000Z", "tier": "1000" } ], "session": { "data": { "follower-session": { "count": 3 }, "…": "…" } }, "overlay": { "isEditorMode": false, "muted": false } } ``` `recents` entries have only these keys: no `platform`, `message`, `gifted`, `bulkGifted` or `isCommunityGift`, so a gift bundle and each of its recipients are separate entries that cannot be told apart. To tell platforms or gifts apart, use live events. `recents` and the session data are built from the events Streamed keeps: the latest 200, and more of the current stream when there are more. When an event kind has real events, its test events are left out. ## When the widget reloads The whole page reloads (and `onWidgetLoad` fires again) when: - the overlay is saved, or its layers or connected accounts change (OBS reloads the whole overlay); - in the editor's preview: shortly after every change of the code or settings; - OBS reloads the browser source. Anything only in memory is lost then. To keep a value across reloads, use the [store](https://streamed.hu/docs/store.md). ## The event queue Without a `widgetDuration` field, every event is delivered the moment it arrives. Two alerts that come together then overlap, unless your code queues them itself. With a number field called `widgetDuration` (in seconds), alert events (the `…-latest` listeners) are delivered one at a time: each one holds the queue until - `widgetDuration` seconds have passed, or - the widget calls `SE_API.resumeQueue()`. Chat messages and their deletions, `bot:counter`, `kvstore:update` and `widget-button` never wait in the queue. ```js window.addEventListener('onEventReceived', (obj) => { const { listener, event } = obj.detail; if (!listener.endsWith('-latest')) return; if (listener !== 'follower-latest') { // Not shown by this widget: do not hold up the queue SE_API.resumeQueue(); return; } showAlert(event).then(() => SE_API.resumeQueue()); // done: next one }); ``` Set `widgetDuration` a little longer than your longest alert, and call `resumeQueue()` when an alert ends and for every queued event you skip. Otherwise a skipped event holds the queue for the full time. `onSessionUpdate` does not wait for the queue: the session data already counts an event that is still waiting. ## Hidden layers While a layer (or its overlay) is switched off, its events are dropped, not saved for later. In the editor's preview a hidden layer still gets them. In OBS, a browser source that is not visible in the current scene keeps running (timers too) and gets events as usual, unless "Shutdown source when not visible" is on in its properties. Then OBS closes the page, and it starts again with `onWidgetLoad` when it is shown.