refactor: apply hinge event payloads instead of re-reading the snapshot - #11
Merged
Merged
Conversation
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.
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.
subscribeignored theonHingesChangepayload and calledgetSnapshot(rootTag)on every event, plus once more synchronously right afterstartObservingto avoid applying an event queued before that read. Both native modules already replay the current snapshot as anonHingesChangeevent whenstartObservingruns, so that extra read was redundant.subscribenow appliesevent.hingesfrom the payload directly and notifies listeners when it changed, instead of re-readinggetSnapshot. The one synchronousgetSnapshotread atcreateHingeObservercreation stays as the seed forget()before any subscription.Mock
startObservinginsrc/__tests__/index.test.tsxnow 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 callinggetSnapshotagain, 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:buildall 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.