Skip to content

feat: add demand-driven field observation - #23

Draft
sonmbol wants to merge 3 commits into
mainfrom
agent/demand-driven-observation
Draft

feat: add demand-driven field observation#23
sonmbol wants to merge 3 commits into
mainfrom
agent/demand-driven-observation

Conversation

@sonmbol

@sonmbol sonmbol commented Aug 5, 2026

Copy link
Copy Markdown
Owner

Summary

Introduces an argument-free, demand-driven observation mode:

@KMPObservable
extension ArticleViewModel: @retroactive KMPStaticallyObservable {}

A typed StateFlow collector is created only when its projected property is first read, for example $viewModel.articleState. Swift provides the field key path through compile-time dynamic-member lookup, so this requires no runtime reflection, generated source, or imported-member enumeration.

Architecture

  • preserves the existing explicit fields: macro and eager observation behavior
  • shares one collector per model identity and demanded key path
  • lazily allocates field observation slots
  • retains field-level Swift Observation invalidation
  • cancels field leases on rebinding and teardown
  • keeps Kotlin as the authoritative value store
  • adds no task or allocation to the established explicit SKIE emission path

Current API tradeoff

The demand-driven path intentionally uses the projected store:

Text($viewModel.articleState.title)

Plain viewModel.articleState still returns the imported Kotlin model directly and cannot intercept its getter without exporter-generated Observation instrumentation. This draft is intended for reviewing that API and the lifetime/performance design before it is considered stable.

Validation

  • 48 tests pass (1 platform-specific test skipped on macOS)
  • strict concurrency enabled
  • warnings treated as errors
  • macro expansion coverage
  • lazy-start and shared-collector tests
  • package-manifest consistency
  • public API compatibility check against the baseline
  • git diff --check

Closes #14

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.

Investigate compile-time discovery of all imported KMP observable properties

1 participant