Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -1885,6 +1885,9 @@ namespace DynamicData
public static System.IObservable<DynamicData.IChangeSet<TObject, TKey>> Switch<TObject, TKey>(this System.IObservable<System.IObservable<DynamicData.IChangeSet<TObject, TKey>>> sources)
where TObject : notnull
where TKey : notnull { }
public static System.IObservable<DynamicData.IChangeSet<TObject, TKey>> Switch<TObject, TKey>(this System.IObservable<System.IObservable<DynamicData.IChangeSet<TObject, TKey, DynamicData.VirtualContext<TObject>>>> sources)
where TObject : notnull
where TKey : notnull { }
public static System.IObservable<System.Collections.Generic.IReadOnlyCollection<TObject>> ToCollection<TObject, TKey>(this System.IObservable<DynamicData.IChangeSet<TObject, TKey>> source)
where TObject : notnull
where TKey : notnull { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1876,6 +1876,9 @@ namespace DynamicData
public static System.IObservable<DynamicData.IChangeSet<TObject, TKey>> Switch<TObject, TKey>(this System.IObservable<System.IObservable<DynamicData.IChangeSet<TObject, TKey>>> sources)
where TObject : notnull
where TKey : notnull { }
public static System.IObservable<DynamicData.IChangeSet<TObject, TKey>> Switch<TObject, TKey>(this System.IObservable<System.IObservable<DynamicData.IChangeSet<TObject, TKey, DynamicData.VirtualContext<TObject>>>> sources)
where TObject : notnull
where TKey : notnull { }
public static System.IObservable<System.Collections.Generic.IReadOnlyCollection<TObject>> ToCollection<TObject, TKey>(this System.IObservable<DynamicData.IChangeSet<TObject, TKey>> source)
where TObject : notnull
where TKey : notnull { }
Expand Down
71 changes: 71 additions & 0 deletions src/DynamicData.Tests/Cache/VirtualizedSwitchFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reactive.Linq;
using System.Reactive.Subjects;
using DynamicData;
using DynamicData.Tests.Utilities;
using FluentAssertions;
using Xunit;

namespace ExternalConsumerTests.Cache;

public class VirtualizedSwitchFixture
{
[Fact]
public void SwitchingAwayFromVirtualizedSourceRemovesItsItems()
{
using var enabled = new BehaviorSubject<bool>(true);
using var source = new SourceCache<int, int>(value => value);
source.AddOrUpdate(Enumerable.Range(0, 20));

IObservable<IChangeSet<int, int, VirtualContext<int>>> virtualized = CreateVirtualized(source, 10);
IObservable<IObservable<IChangeSet<int, int, VirtualContext<int>>>> sources = enabled.Select(
isEnabled => isEnabled ? virtualized : Observable.Empty<IChangeSet<int, int, VirtualContext<int>>>());
IObservable<IChangeSet<int, int>> switched = sources.Switch();
using var subscription = switched
.ValidateChangeSets(static value => value)
.RecordCacheItems(out var results);

results.RecordedChangeSets.Should().ContainSingle();
results.RecordedChangeSets[0].Adds.Should().Be(10);
results.RecordedItemsByKey.Values.Should().BeEquivalentTo(Enumerable.Range(0, 10));

Comment thread
Guflly marked this conversation as resolved.
enabled.OnNext(false);

results.RecordedChangeSets.Should().HaveCount(2);
results.RecordedChangeSets[1].Removes.Should().Be(10);
results.RecordedItemsByKey.Should().BeEmpty();
}

[Fact]
public void SwitchingBetweenVirtualizedSourcesReplacesVisibleItems()
{
using var first = new SourceCache<int, int>(value => value);
using var second = new SourceCache<int, int>(value => value);
first.AddOrUpdate(Enumerable.Range(0, 10));
second.AddOrUpdate(Enumerable.Range(100, 10));

IObservable<IChangeSet<int, int, VirtualContext<int>>> firstVirtualized = CreateVirtualized(first, 3);
IObservable<IChangeSet<int, int, VirtualContext<int>>> secondVirtualized = CreateVirtualized(second, 3);
using var sources = new BehaviorSubject<IObservable<IChangeSet<int, int, VirtualContext<int>>>>(firstVirtualized);
IObservable<IChangeSet<int, int>> switched = sources.Switch();
using var subscription = switched
.ValidateChangeSets(static value => value)
.RecordCacheItems(out var results);

results.RecordedItemsByKey.Values.Should().BeEquivalentTo([0, 1, 2]);

sources.OnNext(secondVirtualized);

results.RecordedChangeSets.Should().HaveCount(3);
results.RecordedChangeSets[1].Removes.Should().Be(3);
results.RecordedChangeSets[2].Adds.Should().Be(3);
results.RecordedItemsByKey.Values.Should().BeEquivalentTo([100, 101, 102]);
}

private static IObservable<IChangeSet<int, int, VirtualContext<int>>> CreateVirtualized(SourceCache<int, int> source, int size) =>
source.Connect().SortAndVirtualize(
Comparer<int>.Default,
Observable.Return<IVirtualRequest>(new VirtualRequest(0, size)));
}
9 changes: 9 additions & 0 deletions src/DynamicData/Cache/ObservableCacheEx.Switch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@ public static IObservable<IChangeSet<TObject, TKey>> Switch<TObject, TKey>(this
return sources.Select(cache => cache.Connect()).Switch();
}

/// <inheritdoc cref="Switch{TObject, TKey}(IObservable{IObservable{IChangeSet{TObject, TKey}}})"/>
/// <param name="sources">An observable that emits virtualized changeset streams.</param>
public static IObservable<IChangeSet<TObject, TKey>> Switch<TObject, TKey>(this IObservable<IObservable<IChangeSet<TObject, TKey, VirtualContext<TObject>>>> sources)
where TObject : notnull
where TKey : notnull
{
return ObservableCacheEx.Switch((IObservable<IObservable<IChangeSet<TObject, TKey>>>)sources);
}

/// <summary>
/// Subscribes to the latest inner changeset stream, unsubscribing from the previous one on each switch.
/// When switching, the old source's items are removed and the new source's items are added.
Expand Down
Loading