Replace runtime discovery with static observation - #12
Merged
Conversation
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.
What changed
StateObjectfactory laziness so repeated SwiftUI view-value construction does not allocate throwaway Kotlin ViewModels.Why
Runtime selector discovery was expensive, fragile across generated ABI changes, and unable to provide compile-time guarantees. Eager factory evaluation also violated
StateObjectlifetime semantics by constructing a new Kotlin model whenever a parent recreated the SwiftUI view value.The new architecture makes observation routes explicit and type-checked while preserving SwiftUI identity through stable, lazily realized
StateObjectstorage. Shared hubs use allocation-light integer listener IDs, support reentrant teardown, and deregister from the weak registry in constant time.Impact
Applications declare observable KMP ViewModels once with
@KMPObservable. SwiftUI owners and observers retain native APIs while gaining deterministic teardown, shared collection, fewer redundant invalidations, and exporter-specific integration modules.This intentionally changes the automatic-observation integration surface for the next major version; explicit adapters remain available as an exporter-neutral fallback.
Validation
swift test -Xswiftc -strict-concurrency=complete -Xswiftc -warnings-as-errors— 36 tests passedxcodebuild docbuild -scheme KMPObservableBridge -destination 'generic/platform=iOS Simulator' CODE_SIGNING_ALLOWED=NO— passedswift package dump-package— passed./Scripts/check-api.sh— passed (API baseline is not configured)git diff --check— passed