Repository navigation
Expand file tree
/
Copy pathPFilter.cs
More file actions
49 lines (40 loc) · 2.31 KB
/
Copy pathPFilter.cs
File metadata and controls
49 lines (40 loc) · 2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// Copyright (c) 2011-2023 Roland Pheasant. All rights reserved.
// Roland Pheasant licenses this file to you under the MIT license.
// See the LICENSE file in the project root for full license information.
#if P_LINQ
using System.Reactive.Linq;
using DynamicData.Cache.Internal;
using DynamicData.Kernel;
// ReSharper disable once CheckNamespace
namespace DynamicData.PLinq
{
internal sealed class PFilter<TObject, TKey>(IObservable<IChangeSet<TObject, TKey>> source, Func<TObject, bool> filter, ParallelisationOptions parallelisationOptions)
where TObject : notnull
where TKey : notnull
{
public IObservable<IChangeSet<TObject, TKey>> Run() => Observable.Create<IChangeSet<TObject, TKey>>(
observer =>
{
var filterer = new PLinqFilteredUpdater(filter, parallelisationOptions);
return source.Select(filterer.Update).NotEmpty().SubscribeSafe(observer);
});
private sealed class PLinqFilteredUpdater(Func<TObject, bool> filter, ParallelisationOptions parallelisationOptions) : AbstractFilter<TObject, TKey>(new ChangeAwareCache<TObject, TKey>(), filter)
{
private readonly ParallelisationOptions _parallelisationOptions = parallelisationOptions;
protected override IEnumerable<UpdateWithFilter> GetChangesWithFilter(ChangeSet<TObject, TKey> updates)
{
if (updates.ShouldParallelise(_parallelisationOptions))
{
return updates.Parallelise(_parallelisationOptions).Select(u => new UpdateWithFilter(Filter(u.Current), u)).ToArray();
}
return updates.Select(u => new UpdateWithFilter(Filter(u.Current), u)).ToArray();
}
protected override IEnumerable<Change<TObject, TKey>> Refresh(IEnumerable<KeyValuePair<TKey, TObject>> items, Func<KeyValuePair<TKey, TObject>, Optional<Change<TObject, TKey>>> factory)
{
var keyValuePairs = items as KeyValuePair<TKey, TObject>[] ?? items.ToArray();
return keyValuePairs.ShouldParallelise(_parallelisationOptions) ? keyValuePairs.Parallelise(_parallelisationOptions).Select(factory).SelectValues() : keyValuePairs.Select(factory).SelectValues();
}
}
}
}
#endif