Browse Source

Reduce memory pressure when using bindings.

pull/5528/head
Dariusz Komosinski 6 years ago
parent
commit
11e2d9250c
  1. 28
      src/Avalonia.Base/Data/BindingOperations.cs
  2. 5
      src/Avalonia.Base/Reactive/AvaloniaPropertyObservable.cs

28
src/Avalonia.Base/Data/BindingOperations.cs

@ -45,7 +45,7 @@ namespace Avalonia.Data
case BindingMode.OneWay: case BindingMode.OneWay:
return target.Bind(property, binding.Observable ?? binding.Subject, binding.Priority); return target.Bind(property, binding.Observable ?? binding.Subject, binding.Priority);
case BindingMode.TwoWay: case BindingMode.TwoWay:
return new CompositeDisposable( return new TwoWayBindingDisposable(
target.Bind(property, binding.Subject, binding.Priority), target.Bind(property, binding.Subject, binding.Priority),
target.GetObservable(property).Subscribe(binding.Subject)); target.GetObservable(property).Subscribe(binding.Subject));
case BindingMode.OneTime: case BindingMode.OneTime:
@ -88,6 +88,32 @@ namespace Avalonia.Data
throw new ArgumentException("Invalid binding mode."); throw new ArgumentException("Invalid binding mode.");
} }
} }
private sealed class TwoWayBindingDisposable : IDisposable
{
private readonly IDisposable _first;
private readonly IDisposable _second;
private bool _isDisposed;
public TwoWayBindingDisposable(IDisposable first, IDisposable second)
{
_first = first;
_second = second;
}
public void Dispose()
{
if (_isDisposed)
{
return;
}
_first.Dispose();
_second.Dispose();
_isDisposed = true;
}
}
} }
public sealed class DoNothingType public sealed class DoNothingType

5
src/Avalonia.Base/Reactive/AvaloniaPropertyObservable.cs

@ -1,4 +1,5 @@
using System; using System;
using System.Collections.Generic;
namespace Avalonia.Reactive namespace Avalonia.Reactive
{ {
@ -55,9 +56,9 @@ namespace Avalonia.Reactive
newValue = (T)e.Sender.GetValue(e.Property); newValue = (T)e.Sender.GetValue(e.Property);
} }
if (!Equals(newValue, _value)) if (!EqualityComparer<T>.Default.Equals(newValue, _value))
{ {
_value = (T)newValue; _value = newValue;
PublishNext(_value); PublishNext(_value);
} }
} }

Loading…
Cancel
Save