|
| 1 | +// Copyright (c) 2011-2025 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.Collections.ObjectModel; |
| 6 | +using System.ComponentModel; |
| 7 | +using System.Diagnostics.CodeAnalysis; |
| 8 | +using System.Linq.Expressions; |
| 9 | +using System.Reactive; |
| 10 | +using System.Reactive.Concurrency; |
| 11 | +using System.Reactive.Disposables; |
| 12 | +using System.Reactive.Linq; |
| 13 | +using DynamicData.Binding; |
| 14 | +using DynamicData.Cache.Internal; |
| 15 | +using DynamicData.List.Internal; |
| 16 | +using DynamicData.List.Linq; |
| 17 | + |
| 18 | +// ReSharper disable once CheckNamespace |
| 19 | +namespace DynamicData; |
| 20 | + |
| 21 | +/// <summary> |
| 22 | +/// Extensions for ObservableList. |
| 23 | +/// </summary> |
| 24 | +public static partial class ObservableListEx |
| 25 | +{ |
| 26 | + /// <summary> |
| 27 | + /// Applies a logical AND (intersection) between multiple list changeset streams. |
| 28 | + /// Only items present in ALL sources appear in the result. |
| 29 | + /// </summary> |
| 30 | + /// <typeparam name="T">The type of items in the lists.</typeparam> |
| 31 | + /// <param name="source">The first source <see cref="IObservable{IChangeSet{T}}"/> to intersect.</param> |
| 32 | + /// <param name="others">The additional <see cref="IObservable{IChangeSet{T}}"/> changeset streams to intersect with.</param> |
| 33 | + /// <returns>A list changeset stream containing items that exist in every source.</returns> |
| 34 | + /// <exception cref="ArgumentNullException"><paramref name="others"/> is <see langword="null"/>.</exception> |
| 35 | + /// <remarks> |
| 36 | + /// <para> |
| 37 | + /// Uses reference counting per item across all sources. An item appears downstream only when |
| 38 | + /// its reference count is non-zero in ALL sources. Item identity is determined by the default equality comparer. |
| 39 | + /// </para> |
| 40 | + /// <list type="table"> |
| 41 | + /// <listheader><term>Event</term><description>Behavior</description></listheader> |
| 42 | + /// <item><term>Add/AddRange</term><description>The item's reference count is incremented in its source tracker. If the item is now present in all sources, an <b>Add</b> is emitted.</description></item> |
| 43 | + /// <item><term>Replace</term><description>The old item's reference count is decremented and the new item's is incremented. Depending on whether each is present in ALL sources, this emits an <b>Add</b>, <b>Remove</b>, <b>Replace</b>, or nothing.</description></item> |
| 44 | + /// <item><term>Remove/RemoveRange/Clear</term><description>The item's reference count is decremented. If it was in the result and is no longer in all sources, a <b>Remove</b> is emitted.</description></item> |
| 45 | + /// <item><term>Refresh</term><description>Forwarded as <b>Refresh</b> if the item is currently in the result.</description></item> |
| 46 | + /// <item><term>Moved</term><description>Ignored (set operations are position-independent).</description></item> |
| 47 | + /// </list> |
| 48 | + /// <para><b>Worth noting:</b> Item identity uses object equality, not position. Duplicate items in a single source are reference-counted independently.</para> |
| 49 | + /// </remarks> |
| 50 | + /// <seealso cref="Or{T}(IObservable{IChangeSet{T}}, IObservable{IChangeSet{T}}[])"/> |
| 51 | + /// <seealso cref="Except{T}(IObservable{IChangeSet{T}}, IObservable{IChangeSet{T}}[])"/> |
| 52 | + /// <seealso cref="Xor{T}(IObservable{IChangeSet{T}}, IObservable{IChangeSet{T}}[])"/> |
| 53 | + /// <seealso cref="ObservableCacheEx.And{TObject, TKey}(IObservable{IChangeSet{TObject, TKey}}, IObservable{IChangeSet{TObject, TKey}}[])"/> |
| 54 | + public static IObservable<IChangeSet<T>> And<T>(this IObservable<IChangeSet<T>> source, params IObservable<IChangeSet<T>>[] others) |
| 55 | + where T : notnull |
| 56 | + { |
| 57 | + others.ThrowArgumentNullExceptionIfNull(nameof(others)); |
| 58 | + |
| 59 | + return source.Combine(CombineOperator.And, others); |
| 60 | + } |
| 61 | + |
| 62 | + /// <inheritdoc cref="And{T}(IObservable{IChangeSet{T}}, IObservable{IChangeSet{T}}[])"/> |
| 63 | + /// <param name="sources">A <see cref="ICollection{T}"/> of changeset streams to intersect.</param> |
| 64 | + /// <remarks> |
| 65 | + /// <inheritdoc cref="And{T}(IObservable{IChangeSet{T}}, IObservable{IChangeSet{T}}[])"/> |
| 66 | + /// <para>This overload accepts a pre-built collection of sources instead of a params array.</para> |
| 67 | + /// </remarks> |
| 68 | + public static IObservable<IChangeSet<T>> And<T>(this ICollection<IObservable<IChangeSet<T>>> sources) |
| 69 | + where T : notnull => sources.Combine(CombineOperator.And); |
| 70 | + |
| 71 | + /// <inheritdoc cref="And{T}(IObservable{IChangeSet{T}}, IObservable{IChangeSet{T}}[])"/> |
| 72 | + /// <param name="sources">An <see cref="IObservableList{T}"/> of changeset streams. Sources can be added or removed dynamically.</param> |
| 73 | + /// <remarks> |
| 74 | + /// <inheritdoc cref="And{T}(IObservable{IChangeSet{T}}, IObservable{IChangeSet{T}}[])"/> |
| 75 | + /// <para>This overload supports dynamic source management: adding or removing changeset streams from the observable list triggers re-evaluation.</para> |
| 76 | + /// </remarks> |
| 77 | + public static IObservable<IChangeSet<T>> And<T>(this IObservableList<IObservable<IChangeSet<T>>> sources) |
| 78 | + where T : notnull => sources.Combine(CombineOperator.And); |
| 79 | + |
| 80 | + /// <inheritdoc cref="And{T}(IObservable{IChangeSet{T}}, IObservable{IChangeSet{T}}[])"/> |
| 81 | + /// <param name="sources">An <see cref="IObservableList{IObservableList{T}}"/> of <see cref="IObservableList{IObservableList{T}}"/>. Each inner list's changes are connected automatically.</param> |
| 82 | + /// <remarks> |
| 83 | + /// <inheritdoc cref="And{T}(IObservable{IChangeSet{T}}, IObservable{IChangeSet{T}}[])"/> |
| 84 | + /// <para>This overload accepts <see cref="IObservableList{T}"/> instances directly, calling <c>Connect()</c> internally.</para> |
| 85 | + /// </remarks> |
| 86 | + public static IObservable<IChangeSet<T>> And<T>(this IObservableList<IObservableList<T>> sources) |
| 87 | + where T : notnull => sources.Combine(CombineOperator.And); |
| 88 | + |
| 89 | + /// <inheritdoc cref="And{T}(IObservable{IChangeSet{T}}, IObservable{IChangeSet{T}}[])"/> |
| 90 | + /// <param name="sources">An <see cref="IObservableList{ISourceList{T}}"/> of <see cref="ISourceList{T}"/>. Each inner list's changes are connected automatically.</param> |
| 91 | + /// <remarks> |
| 92 | + /// <inheritdoc cref="And{T}(IObservable{IChangeSet{T}}, IObservable{IChangeSet{T}}[])"/> |
| 93 | + /// <para>This overload accepts <see cref="ISourceList{T}"/> instances directly, calling <c>Connect()</c> internally.</para> |
| 94 | + /// </remarks> |
| 95 | + public static IObservable<IChangeSet<T>> And<T>(this IObservableList<ISourceList<T>> sources) |
| 96 | + where T : notnull => sources.Combine(CombineOperator.And); |
| 97 | + |
| 98 | + private static IObservable<IChangeSet<T>> Combine<T>(this ICollection<IObservable<IChangeSet<T>>> sources, CombineOperator type) |
| 99 | + where T : notnull |
| 100 | + { |
| 101 | + sources.ThrowArgumentNullExceptionIfNull(nameof(sources)); |
| 102 | + |
| 103 | + return new Combiner<T>(sources, type).Run(); |
| 104 | + } |
| 105 | + |
| 106 | + private static IObservable<IChangeSet<T>> Combine<T>(this IObservable<IChangeSet<T>> source, CombineOperator type, params IObservable<IChangeSet<T>>[] others) |
| 107 | + where T : notnull |
| 108 | + { |
| 109 | + source.ThrowArgumentNullExceptionIfNull(nameof(source)); |
| 110 | + others.ThrowArgumentNullExceptionIfNull(nameof(others)); |
| 111 | + |
| 112 | + if (others.Length == 0) |
| 113 | + { |
| 114 | + throw new ArgumentException("Must be at least one item to combine with", nameof(others)); |
| 115 | + } |
| 116 | + |
| 117 | + var items = source.EnumerateOne().Union(others).ToList(); |
| 118 | + return new Combiner<T>(items, type).Run(); |
| 119 | + } |
| 120 | + |
| 121 | + private static IObservable<IChangeSet<T>> Combine<T>(this IObservableList<ISourceList<T>> sources, CombineOperator type) |
| 122 | + where T : notnull |
| 123 | + { |
| 124 | + sources.ThrowArgumentNullExceptionIfNull(nameof(sources)); |
| 125 | + |
| 126 | + return Observable.Create<IChangeSet<T>>( |
| 127 | + observer => |
| 128 | + { |
| 129 | + var changesSetList = sources.Connect().Transform(s => s.Connect()).AsObservableList(); |
| 130 | + var subscriber = changesSetList.Combine(type).SubscribeSafe(observer); |
| 131 | + return new CompositeDisposable(changesSetList, subscriber); |
| 132 | + }); |
| 133 | + } |
| 134 | + |
| 135 | + private static IObservable<IChangeSet<T>> Combine<T>(this IObservableList<IObservableList<T>> sources, CombineOperator type) |
| 136 | + where T : notnull |
| 137 | + { |
| 138 | + sources.ThrowArgumentNullExceptionIfNull(nameof(sources)); |
| 139 | + |
| 140 | + return Observable.Create<IChangeSet<T>>( |
| 141 | + observer => |
| 142 | + { |
| 143 | + var changesSetList = sources.Connect().Transform(s => s.Connect()).AsObservableList(); |
| 144 | + var subscriber = changesSetList.Combine(type).SubscribeSafe(observer); |
| 145 | + return new CompositeDisposable(changesSetList, subscriber); |
| 146 | + }); |
| 147 | + } |
| 148 | + |
| 149 | + private static IObservable<IChangeSet<T>> Combine<T>(this IObservableList<IObservable<IChangeSet<T>>> sources, CombineOperator type) |
| 150 | + where T : notnull |
| 151 | + { |
| 152 | + sources.ThrowArgumentNullExceptionIfNull(nameof(sources)); |
| 153 | + |
| 154 | + return new DynamicCombiner<T>(sources, type).Run(); |
| 155 | + } |
| 156 | +} |
0 commit comments