Repository navigation
Add opt-in adaptive entity state throttling - #822
Closed
MindFreeze wants to merge 1 commit into
Closed
MindFreeze wants to merge 1 commit into
MindFreeze wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
🟡 Changes recommended
Unresolved throttling bugs can publish partial state from rejected events, deliver updates prematurely or twice, and retain entity stores after connection closure.
6 open issues
Clean up throttler when the connection closes · New Support or explicitly reject throttling on legacy versions · New Prevent duplicate delivery to subscribers joining a pending batch · New Invalidate queued flushes after a synchronous flush · New Rollback partial state changes when a diff fails · New Seed cooldown from snapshot duration, not normalized cost · New
What changed in this PR
Adds opt-in adaptive notification throttling to entity-state subscriptions while keeping stored state current and default behavior unchanged.
Changes:
- Adds throttling options and explicit flushing.
- Coalesces notifications with user-action fast paths and hidden-tab deferral.
- Adds tests for adaptive timing, state consistency, and cleanup.
| File | Description |
|---|---|
| test/throttle.spec.ts | Tests adaptive measurements, timing, and cleanup. |
| test/states.spec.ts | Tests subscription integration and default behavior. |
| README.md | Documents throttling options and explicit flushing. |
| lib/util.ts | Adds service-call and store-read hooks. |
| lib/throttle.ts | Implements adaptive notification coalescing. |
| lib/store.ts | Supports silent updates and state-read tracking. |
| lib/states.ts | Connects throttling options to entity collections. |
| lib/connection.ts | Reports service calls for entity-target tracking. |
Review effort: Max (adaptive)
|
|
||
| return () => { | ||
| conn.removeEventListener("ready", markReplace); | ||
| cleanup(); |
Comment on lines
+266
to
+267
| if (!atLeastHaVersion(conn.haVersion, 2022, 4, 0)) { | ||
| return getCollection( |
| const t0 = shouldProbe ? now() : 0; | ||
|
|
||
| try { | ||
| store.setState(state, true); |
Comment on lines
+443
to
+466
| store.setState(state, true); | ||
| } finally { | ||
| lastPublishAt = now(); | ||
| } | ||
|
|
||
| if (shouldProbe && !destroyed && !isDocumentHidden()) { | ||
| startProbe(t0); | ||
| } | ||
| }; | ||
|
|
||
| const scheduleMicrotaskFlush = () => { | ||
| if (timerId !== undefined) { | ||
| clearTimeout(timerId); | ||
| timerId = undefined; | ||
| } | ||
| if (microtaskScheduled) { | ||
| return; | ||
| } | ||
| microtaskScheduled = true; | ||
| Promise.resolve().then(() => { | ||
| microtaskScheduled = false; | ||
| flush(); | ||
| }); | ||
| }; |
|
|
||
| const measureSnapshot = isReplace && !hasProbeSample; | ||
| const t0 = measureSnapshot ? now() : 0; | ||
| applyUpdates(target, ev); |
| if (entityCount > 0) { | ||
| const msPer1k = (snapshotMs * 1000) / entityCount; | ||
| if (msPer1k > options.snapshotThresholdMsPer1k) { | ||
| emaCostMs = Math.min(MAX_SAMPLE_MS, msPer1k * SNAPSHOT_SEED_FACTOR); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Adds opt-in passive adaptive throttling for
entityStatesColl/subscribeEntityStatesviaoptions.throttleUpdates, while keeping default behavior unchanged.Highlights
EntityStatesCollectionOptions(throttleUpdates?: boolean | ThrottleUpdatesOptions),flushEntityStates(conn), andStore.setState(update, overwrite?, notify?)socoll.statestays immediately consistent while subscriber notifications are coalesced.lib/throttle.ts):MessageChannel+rAF → MessageChannel(with synchronous fallback) and winsorized EMA (alpha = 0.25) to scale the cooldown interval between microtask batching (0ms) andmaxIntervalMs.ready) reset.userIdcontext or recentcall_serviceentity targets (2s lazy-pruned TTL).document.hiddenis true and flushes immediately onvisibilitychange(skipping EMA measurement on resume).