Fix state mutation via Stateful.getter return values (fixes #870) - #883
Open
unusdon wants to merge 1 commit into
Open
Fix state mutation via Stateful.getter return values (fixes #870)#883unusdon wants to merge 1 commit into
unusdon wants to merge 1 commit into
Conversation
…Mini-Apps#870) viewport.safeAreaInsets() and viewport.contentSafeAreaInsets() (and every other stateful getter that reads a nested object) hand out a direct reference to the internal state — a caller doing 'safeAreaInsets.top += 100' silently mutates the source and every subsequent read observes the mutation. Fix: Stateful.getter now returns a *frozen* shallow copy of plain objects and arrays. Freeze is used instead of a per-read copy because @tma.js/signals computed() memoises its function output — a copy alone would be produced once and then handed to every subsequent read. With the freeze in place, a consumer attempting to mutate the returned object throws in strict mode rather than silently corrupting internal state. Class instances (Date, Map, Set, custom classes) round-trip by reference — spread would strip their prototypes, which would be a bigger behaviour change than the ref-leak fix warrants. Regression covered by packages/sdk/src/composables/Stateful.test.ts: nested object freeze, array freeze, stale-read isolation, primitives passthrough. All 782 existing @tma.js/sdk tests pass, plus 4 new tests. Typecheck + lint clean on the touched files (a pre-existing max-len warning in src/errors.ts is unrelated).
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.
Fixes #870.
What changed
viewport.safeAreaInsets()andviewport.contentSafeAreaInsets()(and every other stateful getter that reads a nested object) hand out a direct reference to the internal state — a caller doingsafeAreaInsets.top += 100silently mutates the source, and every subsequent read observes the mutation:Fix
Stateful.getternow returns a frozen shallow copy of plain objects and arrays. Primitives and class instances are returned as-is:Why freeze, not just clone
@tma.js/signals'scomputed()memoises its function output — a bare shallow copy would be produced once and then handed to every subsequent read (same ref every time). WithObject.freezeon the cached copy, mutation attempts throw in strict mode rather than silently corrupting state.Why not copy class instances
Spread strips prototypes (
Map→{},Date→{}, custom classes lose their methods). That's a bigger behaviour change than the ref-leak fix warrants. Class instances round-trip by reference; theSafeAreaInsetscase is a plain object, which the fix covers.Tests
Added
packages/sdk/src/composables/Stateful.test.ts— 4 tests covering:TypeErrorpushthrowsTypeErrorAll 4 pass + full
@tma.js/sdksuite (782 tests) still green. Typecheck + lint clean on touched files (there's a pre-existing max-len warning insrc/errors.tsthat's not mine).Changeset
.changeset/fix-stateful-getter-ref-leak.md—@tma.js/sdkpatch.