From 58c479ae1071a77e0b31671dfcc08b7a15b56dfb Mon Sep 17 00:00:00 2001 From: "d.dovgopolyi" Date: Fri, 14 Jun 2019 10:28:51 +0300 Subject: [PATCH] Fix And operator --- DynamicData.Tests/Cache/AndFixture.cs | 13 +++++++++++++ DynamicData.Tests/List/AndFixture.cs | 11 +++++++++++ DynamicData/Cache/Internal/Combiner.cs | 13 +++++++------ DynamicData/List/Internal/Combiner.cs | 16 +++++++++------- 4 files changed, 40 insertions(+), 13 deletions(-) diff --git a/DynamicData.Tests/Cache/AndFixture.cs b/DynamicData.Tests/Cache/AndFixture.cs index 3eec16182..d9f2b9100 100644 --- a/DynamicData.Tests/Cache/AndFixture.cs +++ b/DynamicData.Tests/Cache/AndFixture.cs @@ -95,5 +95,18 @@ public void UpdatingOneProducesOnlyOneUpdate() _results.Data.Count.Should().Be(1, "Cache should have no items"); _results.Data.Items.First().Should().Be(personUpdated, "Should be updated person"); } + + [Fact] + public void StartingWithNonEmptySourceProducesNoResult() + { + var person = new Person("Adult", 50); + _source1.AddOrUpdate(person); + + using (var result = CreateObservable().AsAggregator()) + { + _results.Messages.Count.Should().Be(0, "Should have no updates"); + result.Data.Count.Should().Be(0, "Cache should have no items"); + } + } } } diff --git a/DynamicData.Tests/List/AndFixture.cs b/DynamicData.Tests/List/AndFixture.cs index d21e935ed..c5b309c37 100644 --- a/DynamicData.Tests/List/AndFixture.cs +++ b/DynamicData.Tests/List/AndFixture.cs @@ -90,5 +90,16 @@ public void ClearOneClearsResult() _source1.Clear(); _results.Data.Count.Should().Be(0); } + + [Fact] + public void StartingWithNonEmptySourceProducesNoResult() + { + _source1.Add(1); + + using (var result = CreateObservable().AsAggregator()) + { + result.Data.Count.Should().Be(0); + } + } } } diff --git a/DynamicData/Cache/Internal/Combiner.cs b/DynamicData/Cache/Internal/Combiner.cs index e5d85e6da..cce1e871e 100644 --- a/DynamicData/Cache/Internal/Combiner.cs +++ b/DynamicData/Cache/Internal/Combiner.cs @@ -30,15 +30,16 @@ public IDisposable Subscribe(IObservable>[] source) var disposable = new CompositeDisposable(); lock (_locker) { - foreach (var item in source) - { - var cache = new Cache(); - _sourceCaches.Add(cache); + var caches = Enumerable.Range(0, source.Length).Select(_ => new Cache()); + _sourceCaches.AddRange(caches); - var subsription = item.Subscribe(updates => Update(cache, updates)); - disposable.Add(subsription); + foreach (var pair in source.Zip(_sourceCaches, (item, cache) => new { Item = item, Cache = cache })) + { + var subscription = pair.Item.Subscribe(updates => Update(pair.Cache, updates)); + disposable.Add(subscription); } } + return disposable; } diff --git a/DynamicData/List/Internal/Combiner.cs b/DynamicData/List/Internal/Combiner.cs index 69bca0cf8..0cab4175a 100644 --- a/DynamicData/List/Internal/Combiner.cs +++ b/DynamicData/List/Internal/Combiner.cs @@ -25,19 +25,20 @@ public IObservable> Run() return Observable.Create>(observer => { var disposable = new CompositeDisposable(); - var sourceLists = new List>(); + var resultList = new ChangeAwareListWithRefCounts(); lock (_locker) { - foreach (var item in _source) - { - var list = new ReferenceCountTracker(); - sourceLists.Add(list); + var sourceLists = Enumerable.Range(0, _source.Count) + .Select(_ => new ReferenceCountTracker()) + .ToList(); - disposable.Add(item.Synchronize(_locker).Subscribe(changes => + foreach (var pair in _source.Zip(sourceLists, (item, list) => new { Item = item, List = list })) + { + disposable.Add(pair.Item.Synchronize(_locker).Subscribe(changes => { - CloneSourceList(list, changes); + CloneSourceList(pair.List, changes); var notifications = UpdateResultList(changes, sourceLists, resultList); if (notifications.Count != 0) @@ -45,6 +46,7 @@ public IObservable> Run() })); } } + return disposable; }); }