Skip to content

Instantly share code, notes, and snippets.

@RolandPheasant
Created December 7, 2021 19:24
Show Gist options
  • Save RolandPheasant/87bc28067acf12d0ec63c249014596cf to your computer and use it in GitHub Desktop.
Save RolandPheasant/87bc28067acf12d0ec63c249014596cf to your computer and use it in GitHub Desktop.
Auto Refresh With Change Set Buffer
public class MyObject : AbstractNotifyPropertyChanged
{
private bool _isSelected;
public int Id { get; set ; }
public bool IsSelected
{
get => _isSelected;
set => SetAndRaise(ref _isSelected, value);
}
}
[Fact]
public void LetsSeeWhatItDoes()
{
var testScheduler = new TestScheduler();
var myItems = Enumerable.Range(1, 1000).Select(i => new MyObject { Id = i }).ToList();
var myCache = new SourceCache<MyObject, int>(x => x.Id);
//load the cache
myCache.AddOrUpdate(myItems);
int count = 0;
var myOperation = myCache.Connect()
.AutoRefresh(x => x.IsSelected)
.Filter(x => x.IsSelected)
.Subscribe(_=> count++);
int bufferCount = 0;
var myBufferedOperation = myCache.Connect()
.AutoRefresh(x => x.IsSelected, changeSetBuffer:TimeSpan.FromMilliseconds(10), scheduler: testScheduler)
.Filter(x => x.IsSelected)
.Subscribe(_ => bufferCount++);
myItems.ForEach(x=> x.IsSelected = true);
testScheduler.AdvanceBy(TimeSpan.FromMilliseconds(11).Ticks);
//This is well cool. 1 change set produced
bufferCount.Should().Be(1);
//This sucks. 1000 change sets produced !!
count.Should().Be(1000);
myOperation.Dispose();
myBufferedOperation.Dispose();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment