DeclarativeProperty async optimizations

This commit is contained in:
2023-10-31 14:58:22 +01:00
parent 63a450fc5f
commit 17df1b45c2
11 changed files with 34 additions and 43 deletions

View File

@@ -27,12 +27,9 @@ public class CollectionRepeaterProperty<TCollection, TItem> : DeclarativePropert
}
private void HandleCollectionChanged(object? sender, NotifyCollectionChangedEventArgs e)
{
var t = Task.Run(async () => await NotifySubscribersAsync(Value));
t.Wait();
}
=> 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<TCollection, TItem> : DeclarativePropert
_currentCollection = collection;
await SetNewValueAsync(collection, cancellationToken);
return SetNewValueAsync(collection, cancellationToken);
}
}

View File

@@ -14,9 +14,11 @@ public class CombineAllProperty<T, TResult> : DeclarativePropertyBase<TResult>
_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)
{

View File

@@ -27,8 +27,8 @@ public sealed class DeclarativeProperty<T> : DeclarativePropertyBase<T>
{
}
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)
{

View File

@@ -140,8 +140,8 @@ public abstract class DeclarativePropertyBase<T> : IDeclarativeProperty<T>
}
}
public async Task ReFireAsync()
=> await SetNewValueAsync(Value);
public Task ReFireAsync()
=> SetNewValueAsync(Value);
public void Dispose()
{

View File

@@ -11,13 +11,13 @@ public sealed class DistinctUntilChangedProperty<T> : DeclarativePropertyBase<T>
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<T> : DeclarativePropertyBase<T>
|| (Value?.Equals(next) ?? false)
)
{
return;
return Task.CompletedTask;
}
_firstFire = false;
await SetNewValueAsync(next, cancellationToken);
return SetNewValueAsync(next, cancellationToken);
}
}

View File

@@ -148,9 +148,9 @@ public sealed class ExtractorProperty<T, TResult> : DeclarativePropertyBase<TRes
}
private void CollectionUpdated(object? sender, NotifyCollectionChangedEventArgs e)
=> Task.Run(async () => await Fire(_collectionWrapper?.Collection)).Wait();
=> Task.Run(async () => await FireAsync(_collectionWrapper?.Collection)).Wait();
private async Task SetValue<TWrapper, TCollection>(TCollection? next, CancellationToken cancellationToken = default)
private Task SetValue<TWrapper, TCollection>(TCollection? next, CancellationToken cancellationToken = default)
where TCollection : IList<T>, INotifyCollectionChanged
where TWrapper : ICollectionWrapper<TCollection>
{
@@ -160,13 +160,13 @@ public sealed class ExtractorProperty<T, TResult> : DeclarativePropertyBase<TRes
? null
: TWrapper.Create(next, CollectionUpdated);
await Fire(next, cancellationToken);
return FireAsync(next, cancellationToken);
}
private async Task Fire(IList<T>? items, CancellationToken cancellationToken = default)
private Task FireAsync(IList<T>? items, CancellationToken cancellationToken = default)
{
var newValue = _extractor(items);
await SetNewValueAsync(newValue, cancellationToken);
return SetNewValueAsync(newValue, cancellationToken);
}
}

View File

@@ -11,9 +11,8 @@ public sealed class FilterProperty<T> : DeclarativePropertyBase<T>
{
_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);
}

View File

@@ -12,6 +12,6 @@ public class MergeProperty<T> : DeclarativePropertyBase<T>
}
}
private async Task UpdateAsync(T newValue, CancellationToken token)
=> await SetNewValueAsync(newValue, token);
private Task UpdateAsync(T newValue, CancellationToken token)
=> SetNewValueAsync(newValue, token);
}

View File

@@ -18,6 +18,6 @@ public sealed class SwitchProperty<TItem> : DeclarativePropertyBase<TItem>
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);
}

View File

@@ -15,19 +15,19 @@ public class ThrottleProperty<T> : DeclarativePropertyBase<T>
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
);

View File

@@ -1,14 +1,7 @@
namespace DeclarativeProperty;
internal sealed class Unsubscriber<T> : IDisposable
internal sealed class Unsubscriber<T>(IDeclarativeProperty<T> owner, Func<T, CancellationToken, Task> onChange)
: IDisposable
{
private readonly IDeclarativeProperty<T> _owner;
private readonly Func<T, CancellationToken, Task> _onChange;
public Unsubscriber(IDeclarativeProperty<T> owner, Func<T, CancellationToken, Task> onChange)
{
_owner = owner;
_onChange = onChange;
}
public void Dispose() => _owner.Unsubscribe(_onChange);
public void Dispose() => owner.Unsubscribe(onChange);
}