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

fix #320 LimitSizeTo notifies subscriber of removed items #334

Merged
merged 1 commit into from
Apr 28, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/DynamicData.Tests/Cache/SizeLimitFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,5 +104,34 @@ public void OnCompleteIsInvokedWhenSourceIsDisposed()

completed.Should().BeTrue();
}

[Fact]
public void InvokeLimitSizeToWhenOverLimit()
{
bool removesTriggered = false;
var subscriber = _source.LimitSizeTo(10, _scheduler)
.Subscribe(removes =>
{
removesTriggered = true;
});

_source.AddOrUpdate(_generator.Take(10).ToArray());
_scheduler.AdvanceBy(TimeSpan.FromMilliseconds(150).Ticks);

removesTriggered.Should().BeFalse();

_source.AddOrUpdate(_generator.Take(10).ToArray());

_scheduler.AdvanceBy(TimeSpan.FromMilliseconds(150).Ticks);

removesTriggered.Should().BeTrue();

_results.Messages.Count.Should().Be(3, "Should be 3 updates");
_results.Messages[0].Adds.Should().Be(10, "Should be 10 adds in the first update");
_results.Messages[1].Adds.Should().Be(10, "Should be 10 adds in the second update");
_results.Messages[2].Removes.Should().Be(10, "Should be 10 removes in the third update");

subscriber.Dispose();
}
}
}
5 changes: 3 additions & 2 deletions src/DynamicData/Cache/Internal/SizeLimiter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// 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.Collections.Generic;
using System.Linq;
using DynamicData.Kernel;

Expand Down Expand Up @@ -45,14 +46,14 @@ public SizeLimiter(int size)
return new ChangeSet<TObject, TKey>(changed);
}

public TKey[] CloneAndReturnExpiredOnly(IChangeSet<ExpirableItem<TObject, TKey>, TKey> updates)
public KeyValuePair<TKey, TObject>[] CloneAndReturnExpiredOnly(IChangeSet<ExpirableItem<TObject, TKey>, TKey> updates)
{
_cache.Clone(updates);
_cache.CaptureChanges(); //Clear any changes

return _cache.KeyValues.OrderByDescending(exp => exp.Value.Index)
.Skip(_sizeLimit)
.Select(kvp => kvp.Key)
.Select(kvp => new KeyValuePair<TKey, TObject>(kvp.Key, kvp.Value.Value))
.ToArray();
}
}
Expand Down
13 changes: 12 additions & 1 deletion src/DynamicData/Cache/ObservableCacheEx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5182,7 +5182,18 @@ void UpdateAction(IChangeSet<TObject, TKey> updates)
.Transform((t, v) => new ExpirableItem<TObject, TKey>(t, v, DateTime.Now, Interlocked.Increment(ref orderItemWasAdded)))
.Select(sizeLimiter.CloneAndReturnExpiredOnly)
.Where(expired => expired.Length != 0)
.Subscribe(source.Remove);
.Subscribe(toRemove =>
{
try
{
source.Remove(toRemove.Select(kv => kv.Key));
observer.OnNext(toRemove);
}
catch (Exception ex)
{
observer.OnError(ex);
}
});
});
}

Expand Down