Skip to content

refactor: apply hinge event payloads instead of re-reading the snapshot - #11

Merged
janicduplessis merged 2 commits into
mainfrom
@janic/observer-event-payload
Sep 21, 2026
Merged

janicduplessis merged 2 commits into
mainfrom
@janic/observer-event-payload

Conversation

@janicduplessis

@janicduplessis janicduplessis commented Sep 21, 2026 •

Copy link
Copy Markdown
Contributor

subscribe ignored the onHingesChange payload and called getSnapshot(rootTag) on every event, plus once more synchronously right after startObserving to avoid applying an event queued before that read. Both native modules already replay the current snapshot as an onHingesChange event when startObserving runs, so that extra read was redundant.

subscribe now applies event.hinges from the payload directly and notifies listeners when it changed, instead of re-reading getSnapshot. The one synchronous getSnapshot read at createHingeObserver creation stays as the seed for get() before any subscription.

Mock startObserving in src/__tests__/index.test.tsx now replays the current mock snapshot as an event, matching native. Replaced the test that asserted a queued event could not overwrite a newer cached snapshot with one asserting events update the snapshot from their payload without calling getSnapshot again, and reworked the create-then-subscribe test so the state change is delivered by that replay event.

Test plan

  • pnpm test, pnpm run typecheck, pnpm run lint, pnpm run build, pnpm run docs:build all pass.

Risk

Native writes its cache before it posts the matching event, so an observer created while an older event is still queued on the JS thread seeds from the newer cache, applies the older payload when that event arrives, then converges on the next event. The previous re-read of the cache on every event masked that window. It needs observer creation to land between a cache write and the queue draining, it self-corrects on the next event, and the cost is one extra listener notification, so it is accepted here rather than adding sequence numbers to the payload.

subscribe ignored the onHingesChange payload and called getSnapshot on
every event, plus once synchronously right after startObserving to
avoid applying a queued older event. Both native modules already
replay the current snapshot as an event when startObserving runs, so
that synchronous read was redundant. Apply event.hinges directly and
drop the extra read.
@janicduplessis
janicduplessis marked this pull request as ready for review September 21, 2026 13:44
@janicduplessis
janicduplessis merged commit 647087e into main Sep 21, 2026
4 checks passed
@janicduplessis
janicduplessis deleted the @janic/observer-event-payload branch September 21, 2026 13:45
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.

1 participant