Skip to content

Add opt-in throttleMs option and flushEntityStates helper - #824

Draft
MindFreeze wants to merge 2 commits into
masterfrom
throttle-entity-state-updates
Draft

MindFreeze wants to merge 2 commits into
masterfrom
throttle-entity-state-updates

Conversation

@MindFreeze

Copy link
Copy Markdown
Member

Adds a minimal opt-in throttling primitive to entityStatesColl / subscribeEntityStates (Home Assistant 2022.4+), leaving the default per-message behavior unchanged:

  • throttleMs?: number | (() => number) — folds updates arriving within the interval into one { ...store.state } copy and publishes on the leading and trailing edge (coll.state still updates on every message).
  • flushEntityStates(conn) — flushes any pending updates immediately (e.g., on visibilitychange or after a user action).

Browser/UI policy (slow-device detection, document.hidden, user-action fast-paths) stays out of this library so consumers can plug in whatever strategy they need.

Benchmarks (Chromium 158, 5,000 entities, 6× CPU throttling)

Paced stream — 10 WS frames/sec over 30 s (300 frames, 806 events, library + subscribers):

Setting Map copies / publishes Library CPU Total Main-Thread CPU (% core) Speedup vs throttleMs: 0
throttleMs: 0 (per-frame) 300 1,380 ms 3,265 ms (10.9%) 1.0×
throttleMs: 100 165 761 ms 1,841 ms (6.1%) 1.8×
throttleMs: 250 100 459 ms 1,125 ms (3.8%) 2.9×
throttleMs: 1000 28 137 ms 351 ms (1.2%) 9.3×

Busy Lovelace dashboard stress test — 30 WS frames/sec over 15 s (6× CPU):

Setting Publishes Total CPU % rAF FPS Jank frames (>50 ms)
throttleMs: 0 (per-frame) 458 99.5% 18.6 148
throttleMs: 250 59 45.0% 56.4 0
throttleMs: 1000 16 34.1% 59.0 0
throttleMs: () => Infinity + flushEntityStates (hidden tab) 1 1.2% N/A 0

@bramkragten

Copy link
Copy Markdown
Member

@copilot resolve the merge conflicts in this pull request

…te-updates

# Conflicts:
#	README.md
#	lib/states.ts
#	test/states.spec.ts

Copilot AI commented Dec 4, 2026

Copy link
Copy Markdown
Contributor

@copilot resolve the merge conflicts in this pull request

Resolved and merged master in 3710276. The test suite and build pass.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants