DeclarativeProperty async optimizations
This commit is contained in:
@@ -27,12 +27,9 @@ public class CollectionRepeaterProperty<TCollection, TItem> : DeclarativePropert
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void HandleCollectionChanged(object? sender, NotifyCollectionChangedEventArgs e)
|
private void HandleCollectionChanged(object? sender, NotifyCollectionChangedEventArgs e)
|
||||||
{
|
=> Task.Run(async () => await NotifySubscribersAsync(Value)).Wait();
|
||||||
var t = Task.Run(async () => await NotifySubscribersAsync(Value));
|
|
||||||
t.Wait();
|
|
||||||
}
|
|
||||||
|
|
||||||
private async Task Handle(TCollection collection, CancellationToken cancellationToken = default)
|
private Task Handle(TCollection collection, CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
if (_currentCollection is { } currentCollection)
|
if (_currentCollection is { } currentCollection)
|
||||||
{
|
{
|
||||||
@@ -47,6 +44,6 @@ public class CollectionRepeaterProperty<TCollection, TItem> : DeclarativePropert
|
|||||||
|
|
||||||
_currentCollection = collection;
|
_currentCollection = collection;
|
||||||
|
|
||||||
await SetNewValueAsync(collection, cancellationToken);
|
return SetNewValueAsync(collection, cancellationToken);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -14,9 +14,11 @@ public class CombineAllProperty<T, TResult> : DeclarativePropertyBase<TResult>
|
|||||||
_sources = sourcesList;
|
_sources = sourcesList;
|
||||||
_combiner = combiner;
|
_combiner = combiner;
|
||||||
|
|
||||||
var initialValueTask = Task.Run(async () => await _combiner(sourcesList.Select(p => p.Value)));
|
|
||||||
initialValueTask.Wait();
|
var result = Task.Run(async () => await _combiner(sourcesList.Select(p => p.Value)))
|
||||||
SetNewValueSync(initialValueTask.Result);
|
.GetAwaiter()
|
||||||
|
.GetResult();
|
||||||
|
SetNewValueSync(result);
|
||||||
|
|
||||||
foreach (var declarativeProperty in sourcesList)
|
foreach (var declarativeProperty in sourcesList)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -27,8 +27,8 @@ public sealed class DeclarativeProperty<T> : DeclarativePropertyBase<T>
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task SetValue(T newValue, CancellationToken cancellationToken = default)
|
public Task SetValue(T newValue, CancellationToken cancellationToken = default)
|
||||||
=> await SetNewValueAsync(newValue, cancellationToken);
|
=> SetNewValueAsync(newValue, cancellationToken);
|
||||||
|
|
||||||
public void SetValueSafe(T newValue, CancellationToken cancellationToken = default)
|
public void SetValueSafe(T newValue, CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -140,8 +140,8 @@ public abstract class DeclarativePropertyBase<T> : IDeclarativeProperty<T>
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task ReFireAsync()
|
public Task ReFireAsync()
|
||||||
=> await SetNewValueAsync(Value);
|
=> SetNewValueAsync(Value);
|
||||||
|
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -11,13 +11,13 @@ public sealed class DistinctUntilChangedProperty<T> : DeclarativePropertyBase<T>
|
|||||||
AddDisposable(from.Subscribe(Handle));
|
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 is { } comparer)
|
||||||
{
|
{
|
||||||
if (comparer(Value, next))
|
if (comparer(Value, next))
|
||||||
{
|
{
|
||||||
return;
|
return Task.CompletedTask;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (
|
else if (
|
||||||
@@ -25,10 +25,10 @@ public sealed class DistinctUntilChangedProperty<T> : DeclarativePropertyBase<T>
|
|||||||
|| (Value?.Equals(next) ?? false)
|
|| (Value?.Equals(next) ?? false)
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
return;
|
return Task.CompletedTask;
|
||||||
}
|
}
|
||||||
|
|
||||||
_firstFire = false;
|
_firstFire = false;
|
||||||
await SetNewValueAsync(next, cancellationToken);
|
return SetNewValueAsync(next, cancellationToken);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -148,9 +148,9 @@ public sealed class ExtractorProperty<T, TResult> : DeclarativePropertyBase<TRes
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void CollectionUpdated(object? sender, NotifyCollectionChangedEventArgs e)
|
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 TCollection : IList<T>, INotifyCollectionChanged
|
||||||
where TWrapper : ICollectionWrapper<TCollection>
|
where TWrapper : ICollectionWrapper<TCollection>
|
||||||
{
|
{
|
||||||
@@ -160,13 +160,13 @@ public sealed class ExtractorProperty<T, TResult> : DeclarativePropertyBase<TRes
|
|||||||
? null
|
? null
|
||||||
: TWrapper.Create(next, CollectionUpdated);
|
: 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);
|
var newValue = _extractor(items);
|
||||||
|
|
||||||
await SetNewValueAsync(newValue, cancellationToken);
|
return SetNewValueAsync(newValue, cancellationToken);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -11,9 +11,8 @@ public sealed class FilterProperty<T> : DeclarativePropertyBase<T>
|
|||||||
{
|
{
|
||||||
_filter = filter;
|
_filter = filter;
|
||||||
|
|
||||||
var initialValueTask = Task.Run(async () => await _filter(from.Value));
|
var result = Task.Run(async () => await _filter(from.Value)).GetAwaiter().GetResult();
|
||||||
initialValueTask.Wait();
|
if (result)
|
||||||
if (initialValueTask.Result)
|
|
||||||
{
|
{
|
||||||
SetNewValueSync(from.Value);
|
SetNewValueSync(from.Value);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,6 +12,6 @@ public class MergeProperty<T> : DeclarativePropertyBase<T>
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task UpdateAsync(T newValue, CancellationToken token)
|
private Task UpdateAsync(T newValue, CancellationToken token)
|
||||||
=> await SetNewValueAsync(newValue, token);
|
=> SetNewValueAsync(newValue, token);
|
||||||
}
|
}
|
||||||
@@ -18,6 +18,6 @@ public sealed class SwitchProperty<TItem> : DeclarativePropertyBase<TItem>
|
|||||||
await SetNewValueAsync(next is null ? default! : next.Value, token);
|
await SetNewValueAsync(next is null ? default! : next.Value, token);
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task HandleInnerValueChange(TItem next, CancellationToken token)
|
private Task HandleInnerValueChange(TItem next, CancellationToken token)
|
||||||
=> await SetNewValueAsync(next, token);
|
=> SetNewValueAsync(next, token);
|
||||||
}
|
}
|
||||||
@@ -15,19 +15,19 @@ public class ThrottleProperty<T> : DeclarativePropertyBase<T>
|
|||||||
AddDisposable(from.Subscribe(SetValue));
|
AddDisposable(from.Subscribe(SetValue));
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task SetValue(T next, CancellationToken cancellationToken = default)
|
private Task SetValue(T next, CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
lock (_lock)
|
lock (_lock)
|
||||||
{
|
{
|
||||||
if (DateTime.Now - _lastFired < _interval())
|
if (DateTime.Now - _lastFired < _interval())
|
||||||
{
|
{
|
||||||
return;
|
return Task.CompletedTask;
|
||||||
}
|
}
|
||||||
|
|
||||||
_lastFired = DateTime.Now;
|
_lastFired = DateTime.Now;
|
||||||
}
|
}
|
||||||
|
|
||||||
await SetNewValueAsync(
|
return SetNewValueAsync(
|
||||||
next,
|
next,
|
||||||
cancellationToken
|
cancellationToken
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -1,14 +1,7 @@
|
|||||||
namespace DeclarativeProperty;
|
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;
|
public void Dispose() => owner.Unsubscribe(onChange);
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user