-
-
Notifications
You must be signed in to change notification settings - Fork 679
Expand file tree
/
Copy pathSearchCollection.cs
More file actions
307 lines (258 loc) · 9.62 KB
/
Copy pathSearchCollection.cs
File metadata and controls
307 lines (258 loc) · 9.62 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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
//
// Copyright (c) Fela Ameghino 2015-2026
//
// Distributed under the GNU General Public License v3.0. (See accompanying
// file LICENSE or copy at https://www.gnu.org/licenses/gpl-3.0.txt)
//
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Threading;
using System.Threading.Tasks;
using Telegram.Common;
using Windows.Foundation;
using Windows.UI.Xaml.Data;
namespace Telegram.Collections
{
public partial class SearchCollection<T, TSource> : DiffObservableCollection<T>, ISupportIncrementalLoading where TSource : IList<T>, ISupportIncrementalLoading, INotifyCollectionChanged
{
private readonly Func<object, string, TSource> _factory;
private object _sender;
// Guards the query pipeline alone: a value waiting out the debounce, and the
// UpdateQuery it eventually fires. Loading must not touch it, or scrolling the
// list would drop a search the user has already typed.
private CancellationTokenSource _cancellation;
private TSource _source;
// Bumped whenever the source is replaced or the collection is cancelled. Work in
// flight compares it after every await: a call that lost the race must not touch
// the collection, nor clear state the newer call now owns.
private int _version;
// The load in flight, if any. A second caller gets this task, never an empty
// result: Count = 0 while HasMoreItems is true makes the list ask again at once,
// and that loop never yields the thread these awaits resume on.
private Task<LoadMoreItemsResult> _loading;
// The source swap in flight, if any. Loads queue behind it, so nothing mutates
// the source while DiffUtil reads it off-thread.
private Task _replace;
private bool _initialized;
public SearchCollection(Func<object, string, TSource> factory, IDiffHandler<T> handler)
: this(factory, null, handler)
{
}
public SearchCollection(Func<object, string, TSource> factory, object sender, IDiffHandler<T> handler)
: base(handler)
{
_factory = factory;
_sender = sender;
_query = new DebouncedPropertyWithToken<string>(Constants.TypingTimeout, UpdateQuery);
}
private readonly DebouncedPropertyWithToken<string> _query;
public string Query
{
get => _query;
set
{
_cancellation?.Cancel();
_cancellation = new();
_query.Set(value, _cancellation.Token);
}
}
public TSource Source => _source;
public void Reload()
{
Update(_factory(_sender ?? this, _query.Value));
}
public void UpdateSender(object sender)
{
Update(_factory((_sender = sender) ?? this, _query.Value));
}
public void UpdateQuery(string value, CancellationToken token = default)
{
if (token.IsCancellationRequested)
{
return;
}
Update(_factory(_sender ?? this, _query.Value = value));
}
public void Cancel()
{
_cancellation?.Cancel();
_cancellation = null;
_query.Cancel();
// Abandons the load and the source swap in flight, if any.
_version++;
}
public void Update(TSource source)
{
var replace = UpdateImpl(source, false);
// A swap that ran to completion synchronously has already cleared the field,
// so storing it now would keep every later load waiting on a finished task.
_replace = replace.IsCompleted ? null : replace;
}
private async Task UpdateImpl(TSource source, bool reentrancy)
{
var version = reentrancy ? _version : ++_version;
try
{
if (_source != null)
{
_source.CollectionChanged -= OnCollectionChanged;
}
if (source == null || !source.HasMoreItems)
{
_source = default;
Clear();
UpdateEmpty();
return;
}
_source = source;
if (!_initialized)
{
source.CollectionChanged += OnCollectionChanged;
return;
}
await source.LoadMoreItemsAsync(0);
if (version != _version)
{
return;
}
// On the UI thread on purpose. The walk snapshots both sides and then
// works on the copies, but the indices it reports are positions in this
// collection, so nothing else may touch it while they are applied.
// Measured at well under a millisecond up to a thousand items, and a list
// long enough to cost more than that costs far more than that applying it.
ReplaceDiff(source);
UpdateEmpty();
// Subscribed last on purpose: UpdateItem writes the old items back into
// the source while the diff is applied, and the handler would echo that
// straight back into this collection.
source.CollectionChanged += OnCollectionChanged;
// I'm not sure in what conditions this can happen, but it happens
if (Count < 1 && source.HasMoreItems && !reentrancy)
{
await UpdateImpl(source, true);
}
}
catch (Exception ex)
{
// A swap that throws must not leave loading wedged: the list would then
// ask for more items forever and never get any.
Logger.Error(ex);
}
finally
{
if (version == _version)
{
_replace = null;
}
}
}
protected override void UpdateItem(T oldValue, T newValue, int newSeqIndex, IDiffHandler<T> diffHandler)
{
// Swap new item with old one to have the same reference in both lists
_source[newSeqIndex] = oldValue;
}
public IAsyncOperation<LoadMoreItemsResult> LoadMoreItemsAsync(uint count)
{
return IncrementalLoading.Run(_ => LoadMoreItems(count));
}
private Task<LoadMoreItemsResult> LoadMoreItems(uint count)
{
if (_loading != null)
{
return _loading;
}
var loading = LoadMoreItemsImpl(count);
// As above: a task that completed synchronously has already run its finally.
if (!loading.IsCompleted)
{
_loading = loading;
}
return loading;
}
private async Task<LoadMoreItemsResult> LoadMoreItemsImpl(uint count)
{
try
{
var replace = _replace;
if (replace != null)
{
await replace;
}
var source = _source;
if (source == null)
{
return default;
}
var version = _version;
var result = await source.LoadMoreItemsAsync(count);
if (result.Count > 0 && version == _version)
{
UpdateEmpty();
}
return result;
}
catch (Exception ex)
{
Logger.Error(ex);
return default;
}
finally
{
_initialized = true;
_loading = null;
}
}
private void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
switch (e.Action)
{
case NotifyCollectionChangedAction.Add:
InsertRangeT(e.NewStartingIndex, e.NewItems);
break;
case NotifyCollectionChangedAction.Remove:
RemoveRange(e.OldStartingIndex, e.OldItems.Count);
break;
case NotifyCollectionChangedAction.Move:
Move(e.OldStartingIndex, e.NewStartingIndex);
break;
case NotifyCollectionChangedAction.Reset:
ReplaceWith(_source);
break;
}
}
public bool HasMoreItems
{
get
{
if (_source != null)
{
return _source.HasMoreItems;
}
// The list has asked for items, so whatever arrives next replaces
// something already on screen rather than being the initial fill.
_initialized = true;
return false;
}
}
private bool _isEmpty = true;
public bool IsEmpty
{
get => _isEmpty;
private set
{
if (_isEmpty != value)
{
_isEmpty = value;
OnPropertyChanged(new PropertyChangedEventArgs(nameof(IsEmpty)));
}
}
}
private void UpdateEmpty()
{
IsEmpty = Count == 0;
}
}
}