Repository navigation
Expand file tree
/
Copy pathSourceCache.cs
More file actions
128 lines (112 loc) · 4.51 KB
/
Copy pathSourceCache.cs
File metadata and controls
128 lines (112 loc) · 4.51 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
using System;
using System.Collections.Generic;
using DynamicData.Kernel;
// ReSharper disable once CheckNamespace
namespace DynamicData
{
/// <summary>
/// An observable cache which exposes an update API. Used at the root
/// of all observable chains
/// </summary>
/// <typeparam name="TObject">The type of the object.</typeparam>
/// <typeparam name="TKey">The type of the key.</typeparam>
public class SourceCache<TObject, TKey> : ISourceCache<TObject, TKey>
{
private readonly ObservableCache<TObject, TKey> _innerCache;
/// <summary>
/// Initializes a new instance of the <see cref="SourceCache{TObject, TKey}"/> class.
/// </summary>
/// <param name="keySelector">The key selector.</param>
/// <exception cref="System.ArgumentNullException">keySelector</exception>
public SourceCache(Func<TObject, TKey> keySelector)
{
if (keySelector == null) throw new ArgumentNullException(nameof(keySelector));
_innerCache = new ObservableCache<TObject, TKey>(keySelector);
}
#region Delegated Members
/// <inheritdoc />
/// <summary>
/// Add, update and remove api via an action method. Enables the consumer to perform queries and updates
/// safely within the innner caches lock.
/// The result of the action will produce appropriate notifications.
/// </summary>
/// <param name="updateAction">The update action.</param>
/// <param name="errorHandler">The error handler.</param>
public void Edit(Action<ISourceUpdater<TObject, TKey>> updateAction, Action<Exception> errorHandler = null)
{
_innerCache.UpdateFromSource(updateAction, errorHandler);
}
/// <summary>
/// Notifies the observer that the source list has finished sending notifications.
/// </summary>
public void OnCompleted()
{
(_innerCache as ICollectionSubject)?.OnCompleted();
}
/// <summary>
/// Notifies the observer that the source list has experienced an error condition.
/// </summary>
public void OnError(Exception exception)
{
(_innerCache as ICollectionSubject)?.OnCompleted();
}
/// <inheritdoc />
public IObservable<int> CountChanged => _innerCache.CountChanged;
/// <summary>
/// Returns a filtered stream of cache changes preceeded with the initital filtered state
/// </summary>
/// <param name="predicate">The predicate.</param>
/// <returns></returns>
public IObservable<IChangeSet<TObject, TKey>> Connect(Func<TObject, bool> predicate)
{
return _innerCache.Connect(predicate);
}
/// <returns></returns>
public IObservable<IChangeSet<TObject, TKey>> Connect()
{
return _innerCache.Connect();
}
/// <summary>
/// Returns an observable of any changes which match the specified key, preceeded with the initital cache state
/// </summary>
/// <param name="key">The key.</param>
/// <returns></returns>
public IObservable<Change<TObject, TKey>> Watch(TKey key)
{
return _innerCache.Watch(key);
}
/// <summary>
/// The total count of cached items
/// </summary>
public int Count => _innerCache.Count;
/// <summary>
/// Gets the Items
/// </summary>
public IEnumerable<TObject> Items => _innerCache.Items;
/// <summary>
/// Gets the key value pairs
/// </summary>
public IEnumerable<KeyValuePair<TKey, TObject>> KeyValues => _innerCache.KeyValues;
/// <summary>
/// Gets the keys
/// </summary>
public IEnumerable<TKey> Keys => _innerCache.Keys;
/// <summary>
/// Lookup a single item using the specified key.
/// </summary>
/// <remarks>
/// Fast indexed lookup
/// </remarks>
/// <param name="key">The key.</param>
/// <returns></returns>
public Optional<TObject> Lookup(TKey key) => _innerCache.Lookup(key);
/// <summary>
/// Releases unmanaged and - optionally - managed resources.
/// </summary>
public void Dispose()
{
_innerCache.Dispose();
}
#endregion
}
}