Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,15 @@ public final void invalidate() {
valid = false;
onInvalidating();
ExpressionHelper.fireValueChangedEvent(helper);
value = null; // clear cached value to avoid hard reference to stale data

/*
* Cached value should be cleared to avoid a strong reference to stale data,
* but only if this binding didn't become valid after firing the event:
*/

if (!valid) {
value = null;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,24 @@ void shouldBeInvalidInitially() {
assertFalse(binding.isValid());
}

@Test
void invalidationWhichBecomesValidDuringCallbacksShouldReturnCorrectValue() {
LazyObjectBindingStub<String> binding = new LazyObjectBindingStub<>() {
@Override
protected String computeValue() {
return "A";
}
};

binding.addListener(obs -> {
assertEquals("A", binding.get());
});

binding.invalidate(); // becomes valid again immediately

assertEquals("A", binding.get());
}

@Nested
class WhenObservedWithInvalidationListener {
private InvalidationListener invalidationListener = obs -> {};
Expand Down