From 17df1b45c2b739eb199019ad4a2ee1cd2ae40e9d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81d=C3=A1m=20Kov=C3=A1cs?= Date: Tue, 31 Oct 2023 14:58:22 +0100 Subject: [PATCH] DeclarativeProperty async optimizations --- .../CollectionRepeaterProperty.cs | 11 ++++------- .../DeclarativeProperty/CombineAllProperty.cs | 8 +++++--- .../DeclarativeProperty/DeclarativeProperty.cs | 4 ++-- .../DeclarativeProperty/DeclarativePropertyBase.cs | 4 ++-- .../DistinctUntilChangedProperty.cs | 8 ++++---- .../DeclarativeProperty/ExtractorProperty.cs | 10 +++++----- src/Library/DeclarativeProperty/FilterProperty.cs | 5 ++--- src/Library/DeclarativeProperty/MergeProperty.cs | 4 ++-- src/Library/DeclarativeProperty/SwitchProperty.cs | 4 ++-- src/Library/DeclarativeProperty/ThrottleProperty.cs | 6 +++--- src/Library/DeclarativeProperty/Unsubscriber.cs | 13 +++---------- 11 files changed, 34 insertions(+), 43 deletions(-) diff --git a/src/Library/DeclarativeProperty/CollectionRepeaterProperty.cs b/src/Library/DeclarativeProperty/CollectionRepeaterProperty.cs index 9aaccf2..d536438 100644 --- a/src/Library/DeclarativeProperty/CollectionRepeaterProperty.cs +++ b/src/Library/DeclarativeProperty/CollectionRepeaterProperty.cs @@ -26,13 +26,10 @@ public class CollectionRepeaterProperty : DeclarativePropert collection.CollectionChanged += HandleCollectionChanged; } - private void HandleCollectionChanged(object? sender, NotifyCollectionChangedEventArgs e) - { - var t = Task.Run(async () => await NotifySubscribersAsync(Value)); - t.Wait(); - } + private void HandleCollectionChanged(object? sender, NotifyCollectionChangedEventArgs e) + => Task.Run(async () => await NotifySubscribersAsync(Value)).Wait(); - private async Task Handle(TCollection collection, CancellationToken cancellationToken = default) + private Task Handle(TCollection collection, CancellationToken cancellationToken = default) { if (_currentCollection is { } currentCollection) { @@ -47,6 +44,6 @@ public class CollectionRepeaterProperty : DeclarativePropert _currentCollection = collection; - await SetNewValueAsync(collection, cancellationToken); + return SetNewValueAsync(collection, cancellationToken); } } \ No newline at end of file diff --git a/src/Library/DeclarativeProperty/CombineAllProperty.cs b/src/Library/DeclarativeProperty/CombineAllProperty.cs index ae0184d..8cab144 100644 --- a/src/Library/DeclarativeProperty/CombineAllProperty.cs +++ b/src/Library/DeclarativeProperty/CombineAllProperty.cs @@ -14,9 +14,11 @@ public class CombineAllProperty : DeclarativePropertyBase _sources = sourcesList; _combiner = combiner; - var initialValueTask = Task.Run(async () => await _combiner(sourcesList.Select(p => p.Value))); - initialValueTask.Wait(); - SetNewValueSync(initialValueTask.Result); + + var result = Task.Run(async () => await _combiner(sourcesList.Select(p => p.Value))) + .GetAwaiter() + .GetResult(); + SetNewValueSync(result); foreach (var declarativeProperty in sourcesList) { diff --git a/src/Library/DeclarativeProperty/DeclarativeProperty.cs b/src/Library/DeclarativeProperty/DeclarativeProperty.cs index 7e6bf39..3c373fe 100644 --- a/src/Library/DeclarativeProperty/DeclarativeProperty.cs +++ b/src/Library/DeclarativeProperty/DeclarativeProperty.cs @@ -27,8 +27,8 @@ public sealed class DeclarativeProperty : DeclarativePropertyBase { } - public async Task SetValue(T newValue, CancellationToken cancellationToken = default) - => await SetNewValueAsync(newValue, cancellationToken); + public Task SetValue(T newValue, CancellationToken cancellationToken = default) + => SetNewValueAsync(newValue, cancellationToken); public void SetValueSafe(T newValue, CancellationToken cancellationToken = default) { diff --git a/src/Library/DeclarativeProperty/DeclarativePropertyBase.cs b/src/Library/DeclarativeProperty/DeclarativePropertyBase.cs index 9d2a703..7cd4bd3 100644 --- a/src/Library/DeclarativeProperty/DeclarativePropertyBase.cs +++ b/src/Library/DeclarativeProperty/DeclarativePropertyBase.cs @@ -140,8 +140,8 @@ public abstract class DeclarativePropertyBase : IDeclarativeProperty } } - public async Task ReFireAsync() - => await SetNewValueAsync(Value); + public Task ReFireAsync() + => SetNewValueAsync(Value); public void Dispose() { diff --git a/src/Library/DeclarativeProperty/DistinctUntilChangedProperty.cs b/src/Library/DeclarativeProperty/DistinctUntilChangedProperty.cs index 5d556b7..019e544 100644 --- a/src/Library/DeclarativeProperty/DistinctUntilChangedProperty.cs +++ b/src/Library/DeclarativeProperty/DistinctUntilChangedProperty.cs @@ -11,13 +11,13 @@ public sealed class DistinctUntilChangedProperty : DeclarativePropertyBase AddDisposable(from.Subscribe(Handle)); } - async Task Handle(T next, CancellationToken cancellationToken = default) + private Task Handle(T next, CancellationToken cancellationToken = default) { if (_comparer is { } comparer) { if (comparer(Value, next)) { - return; + return Task.CompletedTask; } } else if ( @@ -25,10 +25,10 @@ public sealed class DistinctUntilChangedProperty : DeclarativePropertyBase || (Value?.Equals(next) ?? false) ) { - return; + return Task.CompletedTask; } _firstFire = false; - await SetNewValueAsync(next, cancellationToken); + return SetNewValueAsync(next, cancellationToken); } } \ No newline at end of file diff --git a/src/Library/DeclarativeProperty/ExtractorProperty.cs b/src/Library/DeclarativeProperty/ExtractorProperty.cs index 2cb61b9..8e3dc96 100644 --- a/src/Library/DeclarativeProperty/ExtractorProperty.cs +++ b/src/Library/DeclarativeProperty/ExtractorProperty.cs @@ -148,9 +148,9 @@ public sealed class ExtractorProperty : DeclarativePropertyBase Task.Run(async () => await Fire(_collectionWrapper?.Collection)).Wait(); + => Task.Run(async () => await FireAsync(_collectionWrapper?.Collection)).Wait(); - private async Task SetValue(TCollection? next, CancellationToken cancellationToken = default) + private Task SetValue(TCollection? next, CancellationToken cancellationToken = default) where TCollection : IList, INotifyCollectionChanged where TWrapper : ICollectionWrapper { @@ -160,13 +160,13 @@ public sealed class ExtractorProperty : DeclarativePropertyBase? items, CancellationToken cancellationToken = default) + private Task FireAsync(IList? items, CancellationToken cancellationToken = default) { var newValue = _extractor(items); - await SetNewValueAsync(newValue, cancellationToken); + return SetNewValueAsync(newValue, cancellationToken); } } \ No newline at end of file diff --git a/src/Library/DeclarativeProperty/FilterProperty.cs b/src/Library/DeclarativeProperty/FilterProperty.cs index 3f0dd1e..6577585 100644 --- a/src/Library/DeclarativeProperty/FilterProperty.cs +++ b/src/Library/DeclarativeProperty/FilterProperty.cs @@ -11,9 +11,8 @@ public sealed class FilterProperty : DeclarativePropertyBase { _filter = filter; - var initialValueTask = Task.Run(async () => await _filter(from.Value)); - initialValueTask.Wait(); - if (initialValueTask.Result) + var result = Task.Run(async () => await _filter(from.Value)).GetAwaiter().GetResult(); + if (result) { SetNewValueSync(from.Value); } diff --git a/src/Library/DeclarativeProperty/MergeProperty.cs b/src/Library/DeclarativeProperty/MergeProperty.cs index 49a990d..0caeceb 100644 --- a/src/Library/DeclarativeProperty/MergeProperty.cs +++ b/src/Library/DeclarativeProperty/MergeProperty.cs @@ -12,6 +12,6 @@ public class MergeProperty : DeclarativePropertyBase } } - private async Task UpdateAsync(T newValue, CancellationToken token) - => await SetNewValueAsync(newValue, token); + private Task UpdateAsync(T newValue, CancellationToken token) + => SetNewValueAsync(newValue, token); } \ No newline at end of file diff --git a/src/Library/DeclarativeProperty/SwitchProperty.cs b/src/Library/DeclarativeProperty/SwitchProperty.cs index bcf8599..d4bb785 100644 --- a/src/Library/DeclarativeProperty/SwitchProperty.cs +++ b/src/Library/DeclarativeProperty/SwitchProperty.cs @@ -18,6 +18,6 @@ public sealed class SwitchProperty : DeclarativePropertyBase await SetNewValueAsync(next is null ? default! : next.Value, token); } - private async Task HandleInnerValueChange(TItem next, CancellationToken token) - => await SetNewValueAsync(next, token); + private Task HandleInnerValueChange(TItem next, CancellationToken token) + => SetNewValueAsync(next, token); } \ No newline at end of file diff --git a/src/Library/DeclarativeProperty/ThrottleProperty.cs b/src/Library/DeclarativeProperty/ThrottleProperty.cs index 9e935a1..849b872 100644 --- a/src/Library/DeclarativeProperty/ThrottleProperty.cs +++ b/src/Library/DeclarativeProperty/ThrottleProperty.cs @@ -15,19 +15,19 @@ public class ThrottleProperty : DeclarativePropertyBase AddDisposable(from.Subscribe(SetValue)); } - private async Task SetValue(T next, CancellationToken cancellationToken = default) + private Task SetValue(T next, CancellationToken cancellationToken = default) { lock (_lock) { if (DateTime.Now - _lastFired < _interval()) { - return; + return Task.CompletedTask; } _lastFired = DateTime.Now; } - await SetNewValueAsync( + return SetNewValueAsync( next, cancellationToken ); diff --git a/src/Library/DeclarativeProperty/Unsubscriber.cs b/src/Library/DeclarativeProperty/Unsubscriber.cs index 7958704..0ed9e9f 100644 --- a/src/Library/DeclarativeProperty/Unsubscriber.cs +++ b/src/Library/DeclarativeProperty/Unsubscriber.cs @@ -1,14 +1,7 @@ namespace DeclarativeProperty; -internal sealed class Unsubscriber : IDisposable +internal sealed class Unsubscriber(IDeclarativeProperty owner, Func onChange) + : IDisposable { - private readonly IDeclarativeProperty _owner; - private readonly Func _onChange; - - public Unsubscriber(IDeclarativeProperty owner, Func onChange) - { - _owner = owner; - _onChange = onChange; - } - public void Dispose() => _owner.Unsubscribe(_onChange); + public void Dispose() => owner.Unsubscribe(onChange); } \ No newline at end of file