Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vanquish the curse of transform async #565

Merged
merged 1 commit into from
Mar 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 31 additions & 5 deletions src/DynamicData/Cache/Internal/TransformAsync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System;
using System.Linq;
using System.Reactive.Linq;
using System.Threading;
using System.Threading.Tasks;

using DynamicData.Kernel;
Expand Down Expand Up @@ -32,16 +33,41 @@ public TransformAsync(IObservable<IChangeSet<TSource, TKey>> source, Func<TSourc

public IObservable<IChangeSet<TDestination, TKey>> Run()
{
return Observable.Create<IChangeSet<TDestination, TKey>>(
observer =>
return Observable.Create<IChangeSet<TDestination, TKey>>(observer =>
{
var cache = new ChangeAwareCache<TransformedItemContainer, TKey>();
var transformer = _source.SelectMany(changes => DoTransform(cache, changes));
var asyncLock = new SemaphoreSlim(1, 1);

var transformer = _source.SelectMany(async changes =>
{
try
{
await asyncLock.WaitAsync();
return await DoTransform(cache, changes).ConfigureAwait(false);

}
finally
{
asyncLock.Release();
}
});

if (_forceTransform is not null)
{
var locker = new object();
var forced = _forceTransform.Synchronize(locker).SelectMany(shouldTransform => DoTransform(cache, shouldTransform));
var forced = _forceTransform.Synchronize(locker).SelectMany(async shouldTransform =>
{
try
{
await asyncLock.WaitAsync();
return await DoTransform(cache, shouldTransform).ConfigureAwait(false);

}
finally
{
asyncLock.Release();
}
});

transformer = transformer.Synchronize(locker).Merge(forced);
}
Expand Down Expand Up @@ -175,4 +201,4 @@ public TransformResult(Change<TSource, TKey> change, Exception error)
public bool Success { get; }
}
}
}
}
2 changes: 1 addition & 1 deletion version.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "7.5",
"version": "7.5.1",
"publicReleaseRefSpec": [
"^refs/heads/main$", // we release out of master
"^refs/heads/preview/.*", // we release previews
Expand Down