Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix batch updates to allow cleared values to be re-set #5686

Merged
merged 16 commits into from
Mar 28, 2021
Merged
Show file tree
Hide file tree
Changes from 5 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
16 changes: 13 additions & 3 deletions src/Avalonia.Base/ValueStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public bool TryGetValue<T>(

IDisposable? result = null;

if (_values.TryGetValue(property, out var slot))
if (_values.TryGetValue(property, out var slot) && !IsRemoveSentinel<T>(slot))
grokys marked this conversation as resolved.
Show resolved Hide resolved
{
result = SetExisting(slot, property, value, priority);
}
Expand Down Expand Up @@ -138,7 +138,7 @@ public IDisposable AddBinding<T>(
IObservable<BindingValue<T>> source,
BindingPriority priority)
{
if (_values.TryGetValue(property, out var slot))
if (_values.TryGetValue(property, out var slot) && !IsRemoveSentinel<T>(slot))
{
return BindExisting(slot, property, source, priority);
}
Expand Down Expand Up @@ -338,7 +338,7 @@ private IDisposable BindExisting<T>(
private void AddValue(AvaloniaProperty property, IValue value)
{
_values.AddValue(property, value);
if (_batchUpdate is object && value is IBatchUpdate batch)
if (_batchUpdate?.IsBatchUpdating == true && value is IBatchUpdate batch)
batch.BeginBatchUpdate();
value.Start();
}
Expand All @@ -364,6 +364,14 @@ private void NotifyValueChanged<T>(
}
}

private static bool IsRemoveSentinel<T>(IValue value)
{
// Local value entries are optimized and contain only a single value field to save space,
// so there's no way to mark them for removal at the end of a batch update. Instead a
// ConstantValueEntry with a priority of Unset is used as a sentinel value.
return value is ConstantValueEntry<T> t && t.Priority == BindingPriority.Unset;
}

private class BatchUpdate
{
private ValueStore _owner;
Expand All @@ -373,6 +381,8 @@ private class BatchUpdate

public BatchUpdate(ValueStore owner) => _owner = owner;

public bool IsBatchUpdating => _batchUpdateCount > 0;

public void Begin()
{
if (_batchUpdateCount++ == 0)
Expand Down
54 changes: 54 additions & 0 deletions tests/Avalonia.Base.UnitTests/AvaloniaObjectTests_BatchUpdate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,60 @@ public void Batch_Update_Can_Be_Triggered_By_Ending_Batch_Update()
Assert.Null(raised[1].NewValue);
}

[Fact]
public void Can_Set_Cleared_Value_When_Ending_Batch_Update()
{
var target = new TestClass();
var raised = 0;

target.Foo = "foo";

target.BeginBatchUpdate();
target.ClearValue(TestClass.FooProperty);
target.PropertyChanged += (sender, e) =>
{
if (e.Property == TestClass.FooProperty && e.NewValue is null)
{
target.Foo = "bar";
++raised;
}
};
target.EndBatchUpdate();

Assert.Equal("bar", target.Foo);
Assert.Equal(1, raised);
}

[Fact]
public void Can_Bind_Cleared_Value_When_Ending_Batch_Update()
{
var target = new TestClass();
var raised = 0;
var notifications = new List<AvaloniaPropertyChangedEventArgs>();

target.Foo = "foo";

target.BeginBatchUpdate();
target.ClearValue(TestClass.FooProperty);
target.PropertyChanged += (sender, e) =>
{
if (e.Property == TestClass.FooProperty && e.NewValue is null)
{
target.Bind(TestClass.FooProperty, new TestObservable<string>("bar"));
++raised;
}

notifications.Add(e);
};
target.EndBatchUpdate();

Assert.Equal("bar", target.Foo);
Assert.Equal(1, raised);
Assert.Equal(2, notifications.Count);
Assert.Equal(null, notifications[0].NewValue);
Assert.Equal("bar", notifications[1].NewValue);
}

public class TestClass : AvaloniaObject
{
public static readonly StyledProperty<string> FooProperty =
Expand Down