Repository navigation
Expand file tree
/
Copy pathSourceList.cs
More file actions
205 lines (167 loc) · 5.43 KB
/
Copy pathSourceList.cs
File metadata and controls
205 lines (167 loc) · 5.43 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// Copyright (c) 2011-2023 Roland Pheasant. All rights reserved.
// 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.Diagnostics;
using System.Reactive.Disposables;
using System.Reactive.Linq;
using System.Reactive.Subjects;
using DynamicData.List.Internal;
// ReSharper disable once CheckNamespace
namespace DynamicData;
/// <summary>
/// An editable observable list.
/// </summary>
/// <typeparam name="T">The type of the object.</typeparam>
[DebuggerDisplay("SourceList<{typeof(T).Name}> ({Count} Items)")]
public sealed class SourceList<T> : ISourceList<T>
where T : notnull
{
private readonly ISubject<IChangeSet<T>> _changes = new Subject<IChangeSet<T>>();
private readonly Subject<IChangeSet<T>> _changesPreview = new();
private readonly IDisposable _cleanUp;
private readonly Lazy<ISubject<int>> _countChanged = new(() => new Subject<int>());
private readonly object _locker = new();
private readonly ReaderWriter<T> _readerWriter = new();
private int _editLevel;
/// <summary>
/// Initializes a new instance of the <see cref="SourceList{T}"/> class.
/// </summary>
/// <param name="source">The source.</param>
public SourceList(IObservable<IChangeSet<T>>? source = null)
{
var loader = source is null ? Disposable.Empty : LoadFromSource(source);
_cleanUp = Disposable.Create(
() =>
{
loader.Dispose();
OnCompleted();
if (_countChanged.IsValueCreated)
{
_countChanged.Value.OnCompleted();
}
});
}
/// <inheritdoc />
public int Count => _readerWriter.Count;
/// <inheritdoc />
public IObservable<int> CountChanged =>
Observable.Create<int>(
observer =>
{
lock (_locker)
{
var source = _countChanged.Value.StartWith(_readerWriter.Count).DistinctUntilChanged();
return source.SubscribeSafe(observer);
}
});
/// <inheritdoc />
public IEnumerable<T> Items => _readerWriter.Items;
/// <inheritdoc />
public IObservable<IChangeSet<T>> Connect(Func<T, bool>? predicate = null)
{
var observable = Observable.Create<IChangeSet<T>>(
observer =>
{
lock (_locker)
{
if (_readerWriter.Items.Length > 0)
{
observer.OnNext(
new ChangeSet<T>
{
new(ListChangeReason.AddRange, _readerWriter.Items, 0)
});
}
var source = _changes.Finally(observer.OnCompleted);
return source.SubscribeSafe(observer);
}
});
if (predicate is not null)
{
observable = new FilterStatic<T>(observable, predicate).Run();
}
return observable;
}
/// <inheritdoc />
public void Dispose()
{
_cleanUp.Dispose();
_changesPreview.Dispose();
}
/// <inheritdoc />
public void Edit(Action<IExtendedList<T>> updateAction)
{
updateAction.ThrowArgumentNullExceptionIfNull(nameof(updateAction));
lock (_locker)
{
IChangeSet<T>? changes = null;
_editLevel++;
if (_editLevel == 1)
{
changes = _changesPreview.HasObservers ? _readerWriter.WriteWithPreview(updateAction, InvokeNextPreview) : _readerWriter.Write(updateAction);
}
else
{
_readerWriter.WriteNested(updateAction);
}
_editLevel--;
if (changes is not null && _editLevel == 0)
{
InvokeNext(changes);
}
}
}
/// <inheritdoc />
public IObservable<IChangeSet<T>> Preview(Func<T, bool>? predicate = null)
{
IObservable<IChangeSet<T>> observable = _changesPreview;
if (predicate is not null)
{
observable = new FilterStatic<T>(observable, predicate).Run();
}
return observable;
}
private void InvokeNext(IChangeSet<T> changes)
{
if (changes.Count == 0)
{
return;
}
lock (_locker)
{
_changes.OnNext(changes);
if (_countChanged.IsValueCreated)
{
_countChanged.Value.OnNext(_readerWriter.Count);
}
}
}
private void InvokeNextPreview(IChangeSet<T> changes)
{
if (changes.Count == 0)
{
return;
}
lock (_locker)
{
_changesPreview.OnNext(changes);
}
}
private IDisposable LoadFromSource(IObservable<IChangeSet<T>> source) => source.Synchronize(_locker).Finally(OnCompleted).Select(_readerWriter.Write).Subscribe(InvokeNext, OnError, OnCompleted);
private void OnCompleted()
{
lock (_locker)
{
_changesPreview.OnCompleted();
_changes.OnCompleted();
}
}
private void OnError(Exception exception)
{
lock (_locker)
{
_changesPreview.OnError(exception);
_changes.OnError(exception);
}
}
}