Browse Source

Use weak events in AvaloniaProperty plugin.

Fixes #8178.
pull/8189/head
Steven Kirk 4 years ago
parent
commit
69f5f7a48e
  1. 40
      src/Avalonia.Base/Data/Core/Plugins/AvaloniaPropertyAccessorPlugin.cs
  2. 15
      src/Avalonia.Base/Utilities/WeakEvents.cs

40
src/Avalonia.Base/Data/Core/Plugins/AvaloniaPropertyAccessorPlugin.cs

@ -1,5 +1,6 @@
using System;
using System.Runtime.ExceptionServices;
using Avalonia.Utilities;
namespace Avalonia.Data.Core.Plugins
{
@ -60,11 +61,10 @@ namespace Avalonia.Data.Core.Plugins
return AvaloniaPropertyRegistry.Instance.FindRegistered(o, propertyName);
}
private class Accessor : PropertyAccessorBase, IObserver<object?>
private class Accessor : PropertyAccessorBase, IWeakEventSubscriber<AvaloniaPropertyChangedEventArgs>
{
private readonly WeakReference<AvaloniaObject> _reference;
private readonly AvaloniaProperty _property;
private IDisposable? _subscription;
public Accessor(WeakReference<AvaloniaObject> reference, AvaloniaProperty property)
{
@ -95,29 +95,45 @@ namespace Avalonia.Data.Core.Plugins
return false;
}
protected override void SubscribeCore()
void IWeakEventSubscriber<AvaloniaPropertyChangedEventArgs>.
OnEvent(object? notifyPropertyChanged, WeakEvent ev, AvaloniaPropertyChangedEventArgs e)
{
_subscription = Instance?.GetObservable(_property).Subscribe(this);
if (e.Property == _property)
{
SendCurrentValue();
}
}
protected override void UnsubscribeCore()
protected override void SubscribeCore()
{
_subscription?.Dispose();
_subscription = null;
SubscribeToChanges();
SendCurrentValue();
}
void IObserver<object?>.OnCompleted()
protected override void UnsubscribeCore()
{
var instance = Instance;
if (instance != null)
WeakEvents.AvaloniaPropertyChanged.Unsubscribe(instance, this);
}
void IObserver<object?>.OnError(Exception error)
private void SendCurrentValue()
{
ExceptionDispatchInfo.Capture(error).Throw();
try
{
var value = Value;
PublishValue(value);
}
catch { }
}
void IObserver<object?>.OnNext(object? value)
private void SubscribeToChanges()
{
PublishValue(value);
var instance = Instance;
if (instance != null)
WeakEvents.AvaloniaPropertyChanged.Subscribe(instance, this);
}
}
}

15
src/Avalonia.Base/Utilities/WeakEvents.cs

@ -31,10 +31,23 @@ public class WeakEvents
return () => s.PropertyChanged -= handler;
});
/// <summary>
/// Represents PropertyChanged event from <see cref="AvaloniaObject"/>
/// </summary>
public static readonly WeakEvent<AvaloniaObject, AvaloniaPropertyChangedEventArgs>
AvaloniaPropertyChanged = WeakEvent.Register<AvaloniaObject, AvaloniaPropertyChangedEventArgs>(
(s, h) =>
{
EventHandler<AvaloniaPropertyChangedEventArgs> handler = (_, e) => h(s, e);
s.PropertyChanged += handler;
return () => s.PropertyChanged -= handler;
});
/// <summary>
/// Represents CanExecuteChanged event from <see cref="ICommand"/>
/// </summary>
public static readonly WeakEvent<ICommand, EventArgs> CommandCanExecuteChanged =
WeakEvent.Register<ICommand>((s, h) => s.CanExecuteChanged += h,
(s, h) => s.CanExecuteChanged -= h);
}
}

Loading…
Cancel
Save