Skip to content

fix: correct binding setup and helper replacement - #547

Merged
glennawatson merged 2 commits into
mainfrom
fix/binding-subscription-lifetimes
Nov 17, 2026
Merged

glennawatson merged 2 commits into
mainfrom
fix/binding-subscription-lifetimes

Conversation

@glennawatson

@glennawatson glennawatson commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Two-way bindings clean up partial setup failures, and generated output properties expose the current value when their helper is replaced.

  • Dispose the forward subscription when reverse setup fails in generated BindTwoWay, source-first BindTwoWayUnsafe and view-first BindUnsafe.
  • Prefer the helper being constructed over a previous helper during synchronous change notifications.
  • Add regression coverage, update generated-code snapshots and demonstrate both cases in runnable examples targeting .NET 10 and .NET 11.

Why

If a target rejects notification subscription, binding creation throws without returning a disposable, but later source edits still update the target.

Reviewing ReactiveUI's activation reports also exposed a related generated-getter defect: replacing a helper raises notifications before the helper field is reassigned, so listeners read the previous helper's value. This can leave a reactivated view displaying a stale value.

The binding generator and runtime are maintained here; ReactiveUI consumes this binding package, and the former standalone binding-generator repository is archived. Refs reactiveui/ReactiveUI#4538.

Breaking changes

None.

How this was verified

Regression tests cover rejected subscriptions, converter overloads, view-first runtime bindings and synchronous helper replacement. Exercised documentation and platform examples on Linux and Windows, plus a .NET 11 Native AOT consumer; Apple examples require an Apple host.

Notes for the reviewer

Start with reverse subscription setup in BindTwoWayCodeGenerator and RuntimeBindingFallback, then the getter emitted by ObservableAsPropertyGenerator. The getter must consult the helper being constructed even when its field contains an older helper. Snapshot changes repeat the emitted cleanup and getter updates.

Cobertura covers every executable line and branch in the changed production files. The downstream ReactiveUI review separately fixes interface-initialized notification state.

Checklist

  • I have read the Contribute guide
  • The PR title follows Conventional Commits
  • Tests cover this change, or the summary says why they do not
  • New or changed public API has XML documentation

- Dispose the forward subscription when reverse setup fails.
- Cover generated and runtime binding failures.
- Demonstrate failed setup without retaining the source.
@codecov

codecov Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.01%. Comparing base (87fcc89) to head (f5db082).

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #547   +/-   ##
=======================================
  Coverage   99.01%   99.01%           
=======================================
  Files         471      471           
  Lines       18848    18864   +16     
  Branches     2793     2793           
=======================================
+ Hits        18663    18679   +16     
  Misses        185      185           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

- Give a helper being initialized priority over the assigned helper.
- Keep listeners current when activation replaces a helper.
- Demonstrate helper replacement with an observed property.
@glennawatson glennawatson changed the title fix: release two-way bindings when setup fails fix: correct binding setup and helper replacement Nov 16, 2026
@sonarqubecloud

Copy link
Copy Markdown

@glennawatson
glennawatson merged commit b8243d6 into main Nov 17, 2026
24 checks passed
@glennawatson
glennawatson deleted the fix/binding-subscription-lifetimes branch November 17, 2026 11:27
glennawatson added a commit to reactiveui/ReactiveUI that referenced this pull request Nov 17, 2026
<!-- Read the [Contribute
guide](https://www.reactiveui.net/contribute/index.html) before opening
a PR. -->
<!-- Give this PR a title that follows [Conventional
Commits](https://www.conventionalcommits.org/), e.g. `fix: dispose the
binding when the view detaches`. -->
<!-- Two people read this: the reviewer, and a user who followed a link
here from the release notes.
Fill in each section and keep it in proportion. A new subsystem earns
more bullets; it never
     earns longer ones. A one-line fix earns one bullet. -->

## Summary
<!-- One or two sentences on what this PR changes, then one bullet per
distinct change.
Write for someone who has not read the diff. Name the behaviour, type or
API, not the files.
One or two sentences per bullet, no sub-bullets. Cover everything the
branch does.
Describe the result, not the route you took to it, and not how it works
inside. -->

Typed property-change observers receive notifications even when bindings
or event handlers initialize shared state through `IReactiveObject`.

- Preserve the concrete sender and property name when notification
arguments need adapting.
- Keep compatible notification arguments unchanged.
- Cover persistence, generated helper initialization and read-only WPF
dependency-property observation through the packaged generators.

## Why
<!-- The problem this solves. For a bug, say what went wrong and under
what conditions.
     Link the issue and write `Closes #123` so it closes on merge.
     If there is no issue, say what prompted the change. -->

`AutoPersist<T>` threw `InvalidCastException` when shared state was
first accessed through an interface. The same cast also broke typed
changing and changed observers. A notification's sender can have the
requested concrete type even when its event-argument type uses
`IReactiveObject`.

Closes #4554. Refs #4538, #4541, #4547, #4549.

## Breaking changes
<!-- Anything a consumer has to react to: removed or renamed public API,
a changed default,
different timing or threading, a new dependency, a dropped target
framework.
Say what breaks and what to write instead. Write "None" if nothing
breaks. -->

None.

## How this was verified
<!-- One or two lines. What the new tests cover, and anything you
exercised by hand.
"Tests cover every class this adds" and "ran the sample app on Android
and iOS" are complete answers.
Never list test counts, pass or fail results, individual test names, or
which frameworks went
     green. The checks tab reports all of that already.
     Say plainly if something could not be tested here. -->

Tests cover mixed sender types, terminal signals, persistence,
activation and dependent helper initialization. Exercised generated and
runtime read-only WPF observation on real Windows, and compiled
consumers of the current generator package with the .NET 8, 9, 10 and 11
compilers.

## Notes for the reviewer
<!-- Where to start reading, and what deserves the most attention.
Say which parts are mechanical and safe to skim, and which parts are
subtle.
Flag any decision you were unsure about, and anything you left out of
scope on purpose.
     Screenshots, traces and links go here. -->

Start with `ChangeArgsCastObservable`: adapt incompatible event
arguments while preserving the sender and nullable property name. Its
internal visibility permits direct protocol tests without reflection.

The open reports were checked against current packages:

| Report | Finding |
| --- | --- |
| #4554 | Fixed by the shared notification adapter. |
| #4538 | Initial activation works; a related helper-replacement defect
is fixed in reactiveui/ReactiveUI.SourceGenerators#547. |
| #4541 | Constructor initialization works; retained as a downstream
integration test. |
| #4547 | Read-only dependency properties work through generated and
runtime observation; retained as a Windows regression test. |
| #4549 | Current package 5.0.0 loads across all three shipped Roslyn
bands. The reported failure cannot be reproduced without its missing
package and IDE version details. |

Cobertura covers every executable line and branch in the changed
production file.

## Checklist
- [x] I have read the [Contribute
guide](https://www.reactiveui.net/contribute/index.html)
- [x] The PR title follows [Conventional
Commits](https://www.conventionalcommits.org/)
- [x] Tests cover this change, or the summary says why they do not
- [x] New or changed public API has XML documentation
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