|
| 1 | +// Copyright (c) 2011-2023 Roland Pheasant. All rights reserved. |
| 2 | +// Roland Pheasant licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for full license information. |
| 4 | + |
| 5 | +using System; |
| 6 | +using System.Reactive; |
| 7 | +using System.Reactive.Disposables; |
| 8 | +using System.Reactive.Linq; |
| 9 | +using System.Transactions; |
| 10 | + |
| 11 | +using DynamicData.Internal; |
| 12 | + |
| 13 | +namespace DynamicData.Cache.Internal; |
| 14 | + |
| 15 | +internal static partial class Filter |
| 16 | +{ |
| 17 | + public static class WithPredicateState<TObject, TKey, TState> |
| 18 | + where TObject : notnull |
| 19 | + where TKey : notnull |
| 20 | + { |
| 21 | + public static IObservable<IChangeSet<TObject, TKey>> Create( |
| 22 | + IObservable<IChangeSet<TObject, TKey>> source, |
| 23 | + IObservable<TState> predicateState, |
| 24 | + Func<TState, TObject, bool> predicate, |
| 25 | + bool suppressEmptyChangeSets) |
| 26 | + { |
| 27 | + source.ThrowArgumentNullExceptionIfNull(nameof(source)); |
| 28 | + predicateState.ThrowArgumentNullExceptionIfNull(nameof(predicateState)); |
| 29 | + predicate.ThrowArgumentNullExceptionIfNull(nameof(predicate)); |
| 30 | + |
| 31 | + return Observable.Create<IChangeSet<TObject, TKey>>(downstreamObserver => new Subscription( |
| 32 | + downstreamObserver: downstreamObserver, |
| 33 | + predicate: predicate, |
| 34 | + predicateState: predicateState, |
| 35 | + source: source, |
| 36 | + suppressEmptyChangeSets: suppressEmptyChangeSets)); |
| 37 | + } |
| 38 | + |
| 39 | + private sealed class Subscription |
| 40 | + : IDisposable |
| 41 | + { |
| 42 | + private readonly List<Change<TObject, TKey>> _downstreamChangesBuffer; |
| 43 | + private readonly IObserver<IChangeSet<TObject, TKey>> _downstreamObserver; |
| 44 | + private readonly Dictionary<TKey, ItemState> _itemStatesByKey; |
| 45 | + private readonly Func<TState, TObject, bool> _predicate; |
| 46 | + private readonly IDisposable? _predicateStateSubscription; |
| 47 | + private readonly IDisposable? _sourceSubscription; |
| 48 | + private readonly bool _suppressEmptyChangeSets; |
| 49 | + |
| 50 | + private bool _hasPredicateStateCompleted; |
| 51 | + private bool _hasSourceCompleted; |
| 52 | + private bool _isLatestPredicateStateValid; |
| 53 | + private TState _latestPredicateState; |
| 54 | + |
| 55 | + public Subscription( |
| 56 | + IObserver<IChangeSet<TObject, TKey>> downstreamObserver, |
| 57 | + Func<TState, TObject, bool> predicate, |
| 58 | + IObservable<TState> predicateState, |
| 59 | + IObservable<IChangeSet<TObject, TKey>> source, |
| 60 | + bool suppressEmptyChangeSets) |
| 61 | + { |
| 62 | + _downstreamObserver = downstreamObserver; |
| 63 | + _predicate = predicate; |
| 64 | + _suppressEmptyChangeSets = suppressEmptyChangeSets; |
| 65 | + |
| 66 | + _downstreamChangesBuffer = new(); |
| 67 | + _itemStatesByKey = new(); |
| 68 | + |
| 69 | + _latestPredicateState = default!; |
| 70 | + |
| 71 | + var onError = new Action<Exception>(OnError); |
| 72 | + |
| 73 | + _predicateStateSubscription = predicateState |
| 74 | + .SubscribeSafe( |
| 75 | + onNext: OnPredicateStateNext, |
| 76 | + onError: onError, |
| 77 | + onCompleted: OnPredicateStateCompleted); |
| 78 | + |
| 79 | + _sourceSubscription = source |
| 80 | + .SubscribeSafe( |
| 81 | + onNext: OnSourceNext, |
| 82 | + onError: onError, |
| 83 | + onCompleted: OnSourceCompleted); |
| 84 | + } |
| 85 | + |
| 86 | + public void Dispose() |
| 87 | + { |
| 88 | + _predicateStateSubscription?.Dispose(); |
| 89 | + _sourceSubscription?.Dispose(); |
| 90 | + } |
| 91 | + |
| 92 | + private object DownstreamSynchronizationGate |
| 93 | + => _downstreamChangesBuffer; |
| 94 | + |
| 95 | + private object UpstreamSynchronizationGate |
| 96 | + => _itemStatesByKey; |
| 97 | + |
| 98 | + private IChangeSet<TObject, TKey> AssembleDownstreamChanges() |
| 99 | + { |
| 100 | + if (_downstreamChangesBuffer.Count is 0) |
| 101 | + return ChangeSet<TObject, TKey>.Empty; |
| 102 | + |
| 103 | + var downstreamChanges = new ChangeSet<TObject, TKey>(_downstreamChangesBuffer); |
| 104 | + _downstreamChangesBuffer.Clear(); |
| 105 | + |
| 106 | + return downstreamChanges; |
| 107 | + } |
| 108 | + |
| 109 | + private void OnError(Exception error) |
| 110 | + { |
| 111 | + using var @lock = SwappableLock.CreateAndEnter(UpstreamSynchronizationGate); |
| 112 | + |
| 113 | + _predicateStateSubscription?.Dispose(); |
| 114 | + _sourceSubscription?.Dispose(); |
| 115 | + |
| 116 | + @lock.SwapTo(DownstreamSynchronizationGate); |
| 117 | + |
| 118 | + _downstreamObserver.OnError(error); |
| 119 | + } |
| 120 | + |
| 121 | + private void OnPredicateStateCompleted() |
| 122 | + { |
| 123 | + using var @lock = SwappableLock.CreateAndEnter(UpstreamSynchronizationGate); |
| 124 | + |
| 125 | + _hasPredicateStateCompleted = true; |
| 126 | + |
| 127 | + // If we didn't get at least one predicateState value, we can't ever emit any (non-empty) downstream changesets, |
| 128 | + // no matter how many items come through from source, so just go ahead and complete now. |
| 129 | + if (_hasSourceCompleted || (!_isLatestPredicateStateValid && _suppressEmptyChangeSets)) |
| 130 | + { |
| 131 | + @lock.SwapTo(DownstreamSynchronizationGate); |
| 132 | + |
| 133 | + _downstreamObserver.OnCompleted(); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + private void OnPredicateStateNext(TState predicateState) |
| 138 | + { |
| 139 | + using var @lock = SwappableLock.CreateAndEnter(UpstreamSynchronizationGate); |
| 140 | + |
| 141 | + _latestPredicateState = predicateState; |
| 142 | + _isLatestPredicateStateValid = true; |
| 143 | + |
| 144 | + foreach (var key in _itemStatesByKey.Keys) |
| 145 | + { |
| 146 | + var itemState = _itemStatesByKey[key]; |
| 147 | + |
| 148 | + var isIncluded = _predicate.Invoke(predicateState, itemState.Item); |
| 149 | + |
| 150 | + if (isIncluded && !itemState.IsIncluded) |
| 151 | + { |
| 152 | + _downstreamChangesBuffer.Add(new( |
| 153 | + reason: ChangeReason.Add, |
| 154 | + key: key, |
| 155 | + current: itemState.Item)); |
| 156 | + } |
| 157 | + else if (!isIncluded && itemState.IsIncluded) |
| 158 | + { |
| 159 | + _downstreamChangesBuffer.Add(new( |
| 160 | + reason: ChangeReason.Remove, |
| 161 | + key: key, |
| 162 | + current: itemState.Item)); |
| 163 | + } |
| 164 | + |
| 165 | + _itemStatesByKey[key] = new() |
| 166 | + { |
| 167 | + IsIncluded = isIncluded, |
| 168 | + Item = itemState.Item |
| 169 | + }; |
| 170 | + } |
| 171 | + |
| 172 | + var downstreamChanges = AssembleDownstreamChanges(); |
| 173 | + if ((downstreamChanges.Count is not 0) || !_suppressEmptyChangeSets) |
| 174 | + { |
| 175 | + @lock.SwapTo(DownstreamSynchronizationGate); |
| 176 | + |
| 177 | + _downstreamObserver.OnNext(downstreamChanges); |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | + private void OnSourceCompleted() |
| 182 | + { |
| 183 | + using var @lock = SwappableLock.CreateAndEnter(UpstreamSynchronizationGate); |
| 184 | + |
| 185 | + _hasSourceCompleted = true; |
| 186 | + |
| 187 | + // We can never emit any (non-empty) downstream changes in the future, if the collection is empty |
| 188 | + // and the source has reported that it'll never change, so go ahead and complete now. |
| 189 | + if (_hasPredicateStateCompleted || ((_itemStatesByKey.Count is 0) && _suppressEmptyChangeSets)) |
| 190 | + { |
| 191 | + @lock.SwapTo(DownstreamSynchronizationGate); |
| 192 | + |
| 193 | + _downstreamObserver.OnCompleted(); |
| 194 | + } |
| 195 | + } |
| 196 | + |
| 197 | + private void OnSourceNext(IChangeSet<TObject, TKey> upstreamChanges) |
| 198 | + { |
| 199 | + using var @lock = SwappableLock.CreateAndEnter(UpstreamSynchronizationGate); |
| 200 | + |
| 201 | + foreach (var change in upstreamChanges.ToConcreteType()) |
| 202 | + { |
| 203 | + switch (change.Reason) |
| 204 | + { |
| 205 | + case ChangeReason.Add: |
| 206 | + { |
| 207 | + var isIncluded = _isLatestPredicateStateValid && _predicate.Invoke(_latestPredicateState, change.Current); |
| 208 | + |
| 209 | + _itemStatesByKey.Add( |
| 210 | + key: change.Key, |
| 211 | + value: new() |
| 212 | + { |
| 213 | + IsIncluded = isIncluded, |
| 214 | + Item = change.Current |
| 215 | + }); |
| 216 | + |
| 217 | + if (isIncluded) |
| 218 | + { |
| 219 | + _downstreamChangesBuffer.Add(new( |
| 220 | + reason: ChangeReason.Add, |
| 221 | + key: change.Key, |
| 222 | + current: change.Current)); |
| 223 | + } |
| 224 | + } |
| 225 | + break; |
| 226 | + |
| 227 | + // Intentionally not supporting Moved changes, too much work to try and track indexes. |
| 228 | + |
| 229 | + case ChangeReason.Refresh: |
| 230 | + { |
| 231 | + var itemState = _itemStatesByKey[change.Key]; |
| 232 | + |
| 233 | + var isIncluded = _isLatestPredicateStateValid && _predicate.Invoke(_latestPredicateState, itemState.Item); |
| 234 | + |
| 235 | + if (isIncluded && itemState.IsIncluded) |
| 236 | + { |
| 237 | + _downstreamChangesBuffer.Add(new( |
| 238 | + reason: ChangeReason.Refresh, |
| 239 | + key: change.Key, |
| 240 | + current: itemState.Item)); |
| 241 | + } |
| 242 | + else if (isIncluded && !itemState.IsIncluded) |
| 243 | + { |
| 244 | + _downstreamChangesBuffer.Add(new( |
| 245 | + reason: ChangeReason.Add, |
| 246 | + key: change.Key, |
| 247 | + current: itemState.Item)); |
| 248 | + } |
| 249 | + else if (!isIncluded && itemState.IsIncluded) |
| 250 | + { |
| 251 | + _downstreamChangesBuffer.Add(new( |
| 252 | + reason: ChangeReason.Remove, |
| 253 | + key: change.Key, |
| 254 | + current: itemState.Item)); |
| 255 | + } |
| 256 | + |
| 257 | + _itemStatesByKey[change.Key] = new() |
| 258 | + { |
| 259 | + IsIncluded = isIncluded, |
| 260 | + Item = itemState.Item |
| 261 | + }; |
| 262 | + } |
| 263 | + break; |
| 264 | + |
| 265 | + case ChangeReason.Remove: |
| 266 | + { |
| 267 | + var itemState = _itemStatesByKey[change.Key]; |
| 268 | + |
| 269 | + _itemStatesByKey.Remove(change.Key); |
| 270 | + |
| 271 | + if (itemState.IsIncluded) |
| 272 | + { |
| 273 | + _downstreamChangesBuffer.Add(new( |
| 274 | + reason: ChangeReason.Remove, |
| 275 | + key: change.Key, |
| 276 | + current: itemState.Item)); |
| 277 | + } |
| 278 | + } |
| 279 | + break; |
| 280 | + |
| 281 | + case ChangeReason.Update: |
| 282 | + { |
| 283 | + var itemState = _itemStatesByKey[change.Key]; |
| 284 | + |
| 285 | + var isIncluded = _isLatestPredicateStateValid && _predicate.Invoke(_latestPredicateState, change.Current); |
| 286 | + |
| 287 | + if (isIncluded && itemState.IsIncluded) |
| 288 | + { |
| 289 | + _downstreamChangesBuffer.Add(new( |
| 290 | + reason: ChangeReason.Update, |
| 291 | + key: change.Key, |
| 292 | + current: change.Current, |
| 293 | + previous: itemState.Item)); |
| 294 | + } |
| 295 | + else if (isIncluded && !itemState.IsIncluded) |
| 296 | + { |
| 297 | + _downstreamChangesBuffer.Add(new( |
| 298 | + reason: ChangeReason.Add, |
| 299 | + key: change.Key, |
| 300 | + current: change.Current)); |
| 301 | + } |
| 302 | + else if (!isIncluded && itemState.IsIncluded) |
| 303 | + { |
| 304 | + _downstreamChangesBuffer.Add(new( |
| 305 | + reason: ChangeReason.Remove, |
| 306 | + key: change.Key, |
| 307 | + current: itemState.Item)); |
| 308 | + } |
| 309 | + |
| 310 | + _itemStatesByKey[change.Key] = new() |
| 311 | + { |
| 312 | + IsIncluded = isIncluded, |
| 313 | + Item = change.Current |
| 314 | + }; |
| 315 | + } |
| 316 | + break; |
| 317 | + } |
| 318 | + } |
| 319 | + |
| 320 | + var downstreamChanges = AssembleDownstreamChanges(); |
| 321 | + if ((downstreamChanges.Count is not 0) || !_suppressEmptyChangeSets) |
| 322 | + { |
| 323 | + @lock.SwapTo(DownstreamSynchronizationGate); |
| 324 | + |
| 325 | + _downstreamObserver.OnNext(downstreamChanges); |
| 326 | + } |
| 327 | + } |
| 328 | + } |
| 329 | + |
| 330 | + private readonly struct ItemState |
| 331 | + { |
| 332 | + public required bool IsIncluded { get; init; } |
| 333 | + |
| 334 | + public required TObject Item { get; init; } |
| 335 | + } |
| 336 | + } |
| 337 | +} |
0 commit comments