Repository navigation
Expand file tree
/
Copy pathVirtualiser.cs
More file actions
96 lines (80 loc) · 3.93 KB
/
Copy pathVirtualiser.cs
File metadata and controls
96 lines (80 loc) · 3.93 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
using System;
using System.Linq;
using System.Reactive.Linq;
using DynamicData.Cache.Internal;
using DynamicData.Internal;
namespace DynamicData.Cache
{
internal sealed class Virtualise<TObject, TKey>
{
private readonly IObservable<ISortedChangeSet<TObject, TKey>> _source;
private readonly IObservable<IVirtualRequest> _virtualRequests;
public Virtualise(IObservable<ISortedChangeSet<TObject, TKey>> source,
IObservable<IVirtualRequest> virtualRequests)
{
if (source == null) throw new ArgumentNullException(nameof(source));
if (virtualRequests == null) throw new ArgumentNullException(nameof(virtualRequests));
_source = source;
_virtualRequests = virtualRequests;
}
public IObservable<IVirtualChangeSet<TObject, TKey>>Run( )
{
return Observable.Create<IVirtualChangeSet<TObject, TKey>>(observer =>
{
var virtualiser = new Virtualiser();
var locker = new object();
var request = _virtualRequests.Synchronize(locker).Select(virtualiser.Virtualise);
var datachange = _source.Synchronize(locker).Select(virtualiser.Update);
return request.Merge(datachange)
.Where(updates => updates != null)
.SubscribeSafe(observer);
});
}
internal sealed class Virtualiser
{
private readonly FilteredIndexCalculator<TObject, TKey> _changedCalculator = new FilteredIndexCalculator<TObject, TKey>();
private IKeyValueCollection<TObject, TKey> _all = new KeyValueCollection<TObject, TKey>();
private IKeyValueCollection<TObject, TKey> _current = new KeyValueCollection<TObject, TKey>();
private IVirtualRequest _parameters;
private bool _isLoaded;
public Virtualiser(VirtualRequest request = null)
{
_parameters = request ?? new VirtualRequest();
}
public IVirtualChangeSet<TObject, TKey> Virtualise(IVirtualRequest parameters)
{
if (parameters == null || parameters.StartIndex < 0 || parameters.Size < 1)
{
return null;
}
if (parameters.Size == _parameters.Size && parameters.StartIndex == _parameters.StartIndex)
return null;
_parameters = parameters;
return Virtualise();
}
public IVirtualChangeSet<TObject, TKey> Update(ISortedChangeSet<TObject, TKey> updates)
{
_isLoaded = true;
_all = updates.SortedItems;
return Virtualise(updates);
}
private IVirtualChangeSet<TObject, TKey> Virtualise(ISortedChangeSet<TObject, TKey> updates = null)
{
if (_isLoaded == false) return null;
var previous = _current;
var virualised = _all.Skip(_parameters.StartIndex)
.Take(_parameters.Size)
.ToList();
_current = new KeyValueCollection<TObject, TKey>(virualised, _all.Comparer, updates?.SortedItems.SortReason ?? SortReason.DataChanged, _all.Optimisations);
////check for changes within the current virtualised page. Notify if there have been changes or if the overall count has changed
var notifications = _changedCalculator.Calculate(_current, previous, updates);
if (notifications.Count == 0 && (previous.Count != _current.Count))
{
return null;
}
var response = new VirtualResponse(_parameters.Size, _parameters.StartIndex, _all.Count);
return new VirtualChangeSet<TObject, TKey>(notifications, _current, response);
}
}
}
}