Documentation Index
Fetch the complete documentation index at: https://mintlify.com/smogon/pokemon-showdown-client/llms.txt
Use this file to discover all available pages before exploring further.
PSStreamModel<T> solves a specific problem that plain PSModel cannot: what happens to events that fire before any listener is attached? In the PS client, many events — server messages, preference changes, background loads — can arrive during the JavaScript boot sequence, before Preact panels have mounted and called subscribe. PSStreamModel holds those events in a backlog array and replays them the moment the first subscriber registers, so nothing is silently dropped. Once at least one subscriber exists the backlog is cleared and events flow directly to listeners as they would in a normal observable.
Nullish values are never added to the backlog. Passing
null or undefined to update() while there are no subscribers is a deliberate signal (e.g. “bulk reload”) — it is forwarded to live subscribers but is intentionally not buffered.Class Signature
string, reflecting its primary use case: streaming string keys or identifiers (like preference keys) to late-binding subscribers.
Properties
The live list of all active subscriptions. Managed automatically by
subscribe() and PSSubscription.unsubscribe(). Before the first subscriber registers, this array is empty and events are buffered in backlog instead.Stores events that were fired before any subscriber registered.
- Starts as an empty array
[]. - Appended to (with non-nullish values) whenever
update(value)is called whilesubscriptionsis empty. - Set to
nullpermanently after the first subscriber registers and the backlog is replayed —nullis the sentinel meaning “backlog phase is over”.
backlog !== null→ still in pre-subscriber phase; backlog may contain queued events.backlog === null→ at least one subscriber has existed; replay is complete.
Methods
Registers a listener. If events are sitting in the backlog they are replayed synchronously in order before this call returns, and then the backlog is set to
null. Future events flow directly to all registered listeners.Like
subscribe, but additionally fires the listener once synchronously with value after the backlog has been replayed. Defaults to calling the listener with null! if value is omitted.Fires an event with the given value.
- If
subscriptionsis non-empty: calls every listener withvalue. - If
subscriptionsis empty andvalueis non-nullish: appendsvaluetobacklogfor later replay. - If
subscriptionsis empty andvalueisnullorundefined: the event is discarded (not added to backlog).
PSPrefs — The canonical PSStreamModel
PSPrefs (accessed as PS.prefs) is the built-in PSStreamModel<string | null> that tracks every user preference. It extends PSStreamModel rather than PSModel to ensure preference updates that arrive during boot (before panels mount) are not lost.
| Value | Meaning |
|---|---|
"theme" | Only the theme preference changed. |
"volume" | Only the volume preference changed. |
null | All preferences were reloaded (bulk change). |
Usage Examples
Backlog Lifecycle
Phase 1 — Pre-subscriber (backlog active)
Phase 1 — Pre-subscriber (backlog active)
When a
PSStreamModel is first created, backlog is an empty array and subscriptions is empty. Every call to update(nonNullValue) appends to backlog. Nullish values are dropped silently in this phase.Phase 2 — First subscriber (backlog replay)
Phase 2 — First subscriber (backlog replay)
The moment
subscribe(listener) is called, all buffered events are replayed to the new listener in order, then backlog is set to null to signal that the pre-subscriber phase is over.Phase 3 — Normal streaming (backlog null)
Phase 3 — Normal streaming (backlog null)
Once
backlog is null, every update(value) call goes directly to all live subscribers. New subscribers joining at this stage receive only future events.PSStreamModel vs PSModel
PSStreamModel
Use when events can arrive before listeners are ready. Keeps a backlog of non-nullish events and replays them to the first subscriber. Best for boot-time data streams: preferences, backgrounds, early server messages.
PSModel
Use when late subscribers simply don’t need history. Notifies all current listeners immediately and discards the value afterwards. Best for UI-driven state like connection status, panel focus, and render triggers.
