| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="DynamicDataExtensions.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.Collections; |
| | | 9 | | using System.Collections.Generic; |
| | | 10 | | using System.ComponentModel; |
| | | 11 | | using System.Linq; |
| | | 12 | | using System.Reactive.Concurrency; |
| | | 13 | | using System.Reactive.Linq; |
| | | 14 | | using DynamicData; |
| | | 15 | | using DynamicData.Binding; |
| | | 16 | | using DynamicData.Kernel; |
| | | 17 | | |
| | | 18 | | #pragma warning disable IDE0130 // Namespace does not match folder structure |
| | | 19 | | namespace MyNet.Observable; |
| | | 20 | | #pragma warning restore IDE0130 // Namespace does not match folder structure |
| | | 21 | | |
| | | 22 | | public static class DynamicDataExtensions |
| | | 23 | | { |
| | | 24 | | /// <summary> |
| | | 25 | | /// Like DynamicData <c>MergeMany</c>, but forwards a remove change set for inner items when an outer item is remove |
| | | 26 | | /// </summary> |
| | | 27 | | /// <remarks> |
| | | 28 | | /// <para> |
| | | 29 | | /// <paramref name="observableSelector"/> is invoked again when an outer item is removed to build the forwarded |
| | | 30 | | /// remove change set. For the same outer item it must return an observable that reflects the same inner items |
| | | 31 | | /// (no side effects, no new disposable resources that leak). |
| | | 32 | | /// </para> |
| | | 33 | | /// </remarks> |
| | | 34 | | [System.Diagnostics.CodeAnalysis.SuppressMessage("Naming", "CA1711:Identifiers should not have incorrect suffix", Ju |
| | | 35 | | public static IObservable<IChangeSet<TDestination>> MergeManyEx<T, TDestination>( |
| | | 36 | | this IObservable<IChangeSet<T>> source, |
| | | 37 | | Func<T, IObservable<IChangeSet<TDestination>>> observableSelector) |
| | | 38 | | where T : notnull |
| | | 39 | | where TDestination : notnull |
| | 9 | 40 | | => source == null |
| | 9 | 41 | | ? throw new ArgumentNullException(nameof(source)) |
| | 9 | 42 | | : observableSelector == null |
| | 9 | 43 | | ? throw new ArgumentNullException(nameof(observableSelector)) |
| | 9 | 44 | | : new MergeManyEx<T, TDestination>(source, observableSelector).Run(); |
| | | 45 | | |
| | | 46 | | /// <summary> |
| | | 47 | | /// Keyed variant of <see cref="MergeManyEx{T, TDestination}"/>. |
| | | 48 | | /// </summary> |
| | | 49 | | /// <remarks> |
| | | 50 | | /// <inheritdoc cref="MergeManyEx{T, TDestination}(IObservable{IChangeSet{T}}, Func{T, IObservable{IChangeSet{TDesti |
| | | 51 | | /// </remarks> |
| | | 52 | | [System.Diagnostics.CodeAnalysis.SuppressMessage("Naming", "CA1711:Identifiers should not have incorrect suffix", Ju |
| | | 53 | | public static IObservable<IChangeSet<TDestination, TDestinationKey>> MergeManyEx<T, TKey, TDestination, TDestination |
| | | 54 | | this IObservable<IChangeSet<T, TKey>> source, |
| | | 55 | | Func<T, IObservable<IChangeSet<TDestination, TDestinationKey>>> observableSelector, |
| | | 56 | | Func<TDestination, TDestinationKey> observableKeySelector) |
| | | 57 | | where T : notnull |
| | | 58 | | where TDestination : notnull |
| | | 59 | | where TKey : notnull |
| | | 60 | | where TDestinationKey : notnull |
| | 6 | 61 | | => source == null |
| | 6 | 62 | | ? throw new ArgumentNullException(nameof(source)) |
| | 6 | 63 | | : observableSelector == null |
| | 6 | 64 | | ? throw new ArgumentNullException(nameof(observableSelector)) |
| | 6 | 65 | | : new MergeManyEx<T, TKey, TDestination, TDestinationKey>(source, observableSelector, observableKeySelec |
| | | 66 | | |
| | | 67 | | public static void RemoveMany<T>(this ICollection<T> source, IEnumerable<T> itemsToRemove) |
| | | 68 | | { |
| | 24 | 69 | | ArgumentNullException.ThrowIfNull(source); |
| | | 70 | | |
| | 21 | 71 | | ArgumentNullException.ThrowIfNull(itemsToRemove); |
| | | 72 | | |
| | 15 | 73 | | var toRemoveArray = itemsToRemove.ToList(); |
| | | 74 | | |
| | | 75 | | // match all indices and remove in reverse as it is more efficient |
| | 15 | 76 | | var toRemove = source.IndexOfMany(toRemoveArray) |
| | 15 | 77 | | .OrderByDescending(x => x.Index) |
| | 15 | 78 | | .ToList(); |
| | | 79 | | |
| | | 80 | | // if there are duplicates, it could be that an item exists in the |
| | | 81 | | // source collection more than once - in that case the fast remove |
| | | 82 | | // would remove each instance |
| | 15 | 83 | | var hasDuplicates = toRemove.Duplicates(t => t.Item).Any(); |
| | | 84 | | |
| | 15 | 85 | | if (!hasDuplicates && source is IList list) |
| | | 86 | | { |
| | | 87 | | // Fast remove because we know the index of all, and we remove in order |
| | 6 | 88 | | toRemove.ForEach(t => list.RemoveAt(t.Index)); |
| | | 89 | | } |
| | | 90 | | else |
| | | 91 | | { |
| | | 92 | | // Slow remove but safe |
| | 9 | 93 | | toRemoveArray.ForEach(t => source.Remove(t)); |
| | | 94 | | } |
| | 9 | 95 | | } |
| | | 96 | | |
| | | 97 | | extension<T>(IChangeSet<T> changes) |
| | | 98 | | where T : notnull |
| | | 99 | | { |
| | | 100 | | /// <summary>Gets items added in this change set (single add and add range).</summary> |
| | 0 | 101 | | public IEnumerable<T> GetAddedItems() => changes.Where(y => y.Reason == ListChangeReason.Add).Select(z => z.Item |
| | | 102 | | |
| | | 103 | | /// <summary>Gets items removed in this change set (single remove and remove range).</summary> |
| | 15 | 104 | | public IEnumerable<T> GetRemovedItems() => changes.Where(y => y.Reason == ListChangeReason.Remove).Select(z => z |
| | | 105 | | } |
| | | 106 | | |
| | | 107 | | extension<T, TKey>(IChangeSet<T, TKey> changes) |
| | | 108 | | where T : notnull |
| | | 109 | | where TKey : notnull |
| | | 110 | | { |
| | | 111 | | /// <summary>Gets outer items removed in this keyed change set (each <see cref="ChangeReason.Remove"/> entry).</ |
| | 21 | 112 | | public IEnumerable<T> GetRemovedItems() => changes.Where(c => c.Reason == ChangeReason.Remove).Select(c => c.Cur |
| | | 113 | | } |
| | | 114 | | |
| | | 115 | | /// <summary> |
| | | 116 | | /// Invokes <paramref name="action"/> when the change set updates and when any item raises |
| | | 117 | | /// <see cref="INotifyPropertyChanged.PropertyChanged"/>. |
| | | 118 | | /// </summary> |
| | | 119 | | /// <remarks> |
| | | 120 | | /// <paramref name="action"/> runs once immediately when the outer subscription receives a change set, and again |
| | | 121 | | /// for each item property change. After the initial bind, expect at least one call per batch of changes. |
| | | 122 | | /// </remarks> |
| | | 123 | | public static IDisposable SubscribeAll<T>(this IObservable<IChangeSet<T>> source, Action action) |
| | | 124 | | where T : INotifyPropertyChanged |
| | 3 | 125 | | => source.SubscribeMany(x => x.WhenAnyPropertyChanged().Subscribe(_ => action())) |
| | 3 | 126 | | .Subscribe(_ => action()); |
| | | 127 | | |
| | | 128 | | /// <inheritdoc cref="SubscribeAll{T}(IObservable{IChangeSet{T}}, Action)"/> |
| | | 129 | | public static IDisposable SubscribeAll<T, TKey>(this IObservable<IChangeSet<T, TKey>> source, Action action) |
| | | 130 | | where T : INotifyPropertyChanged |
| | | 131 | | where TKey : notnull |
| | 0 | 132 | | => source.SubscribeMany(x => x.WhenAnyPropertyChanged().Subscribe(_ => action())) |
| | 0 | 133 | | .Subscribe(_ => action()); |
| | | 134 | | |
| | | 135 | | /// <summary> |
| | | 136 | | /// Like ObserveOn(IScheduler), but if <paramref name="scheduler"/> is null, returns the source observable without o |
| | | 137 | | /// </summary> |
| | | 138 | | /// <param name="source">The source observable sequence.</param> |
| | | 139 | | /// <param name="scheduler">The scheduler to observe on, or null to observe on the current thread.</param> |
| | | 140 | | /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam> |
| | | 141 | | /// <returns>An observable sequence that observes on the specified scheduler, or the source sequence if the schedule |
| | | 142 | | /// <exception cref="ArgumentNullException">Thrown if the source is null.</exception> |
| | 564 | 143 | | public static IObservable<TSource> ObserveOnOptional<TSource>(this IObservable<TSource> source, IScheduler? schedule |
| | | 144 | | } |
| | | 145 | | |