Repository navigation
Expand file tree
/
Copy pathSwitch.cs
More file actions
35 lines (28 loc) · 1.36 KB
/
Copy pathSwitch.cs
File metadata and controls
35 lines (28 loc) · 1.36 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
// 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.
using System.Reactive.Disposables;
using System.Reactive.Linq;
namespace DynamicData.Cache.Internal;
internal sealed class Switch<TObject, TKey>(IObservable<IObservable<IChangeSet<TObject, TKey>>> sources)
where TObject : notnull
where TKey : notnull
{
private readonly IObservable<IObservable<IChangeSet<TObject, TKey>>> _sources = sources ?? throw new ArgumentNullException(nameof(sources));
public IObservable<IChangeSet<TObject, TKey>> Run() => Observable.Create<IChangeSet<TObject, TKey>>(
observer =>
{
var locker = new object();
var destination = new LockFreeObservableCache<TObject, TKey>();
var populator = Observable.Switch(
_sources.Do(
_ =>
{
lock (locker)
{
destination.Clear();
}
})).Synchronize(locker).PopulateInto(destination);
return new CompositeDisposable(destination, populator, destination.Connect().SubscribeSafe(observer));
});
}