| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="DispatchedObservableCollection.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.Generic; |
| | | 9 | | using System.Collections.Specialized; |
| | | 10 | | using System.ComponentModel; |
| | | 11 | | |
| | | 12 | | namespace MyNet.Collections; |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// An observable collection that schedules notifications on a specific scheduler (typically UI thread). |
| | | 16 | | /// It only owns notification dispatching responsibility. |
| | | 17 | | /// </summary> |
| | | 18 | | /// <typeparam name="T">The type of items in the collection.</typeparam> |
| | | 19 | | /// <remarks> |
| | | 20 | | /// Initializes a new instance of the <see cref="DispatchedObservableCollection{T}"/> class with an existing inner colle |
| | | 21 | | /// </remarks> |
| | | 22 | | /// <param name="inner">The wrapped collection.</param> |
| | | 23 | | /// <param name="dispatcher">The dispatcher used for notifications.</param> |
| | 9 | 24 | | public sealed class DispatchedObservableCollection<T>(ObservableRangeCollection<T> inner, ICollectionEventDispatcher dis |
| | | 25 | | { |
| | 9 | 26 | | private readonly ICollectionEventDispatcher _dispatcher = dispatcher ?? throw new ArgumentNullException(nameof(dispa |
| | | 27 | | |
| | | 28 | | /// <summary> |
| | | 29 | | /// Initializes a new instance of the <see cref="DispatchedObservableCollection{T}"/> class with a scheduler. |
| | | 30 | | /// </summary> |
| | | 31 | | /// <param name="dispatcher">The dispatcher used for notifications.</param> |
| | | 32 | | public DispatchedObservableCollection(ICollectionEventDispatcher dispatcher) |
| | 6 | 33 | | : this([], dispatcher) |
| | | 34 | | { |
| | 6 | 35 | | } |
| | | 36 | | |
| | | 37 | | /// <summary> |
| | | 38 | | /// Initializes a new instance of the <see cref="DispatchedObservableCollection{T}"/> class with initial capacity an |
| | | 39 | | /// Optimized for known collection sizes to avoid resizing. |
| | | 40 | | /// </summary> |
| | | 41 | | /// <param name="capacity">The initial capacity to pre-allocate.</param> |
| | | 42 | | /// <param name="dispatcher">The dispatcher used for notifications.</param> |
| | | 43 | | public DispatchedObservableCollection(int capacity, ICollectionEventDispatcher dispatcher) |
| | 0 | 44 | | : this(new ObservableRangeCollection<T>(capacity), dispatcher) |
| | | 45 | | { |
| | 0 | 46 | | } |
| | | 47 | | |
| | | 48 | | /// <summary> |
| | | 49 | | /// Initializes a new instance of the <see cref="DispatchedObservableCollection{T}"/> class with elements from a lis |
| | | 50 | | /// </summary> |
| | | 51 | | /// <param name="list">The list whose elements are copied to the new collection.</param> |
| | | 52 | | /// <param name="dispatcher">The dispatcher used for notifications.</param> |
| | | 53 | | public DispatchedObservableCollection(IList<T> list, ICollectionEventDispatcher dispatcher) |
| | 0 | 54 | | : this(new ObservableRangeCollection<T>(list), dispatcher) |
| | | 55 | | { |
| | 0 | 56 | | } |
| | | 57 | | |
| | | 58 | | /// <summary> |
| | | 59 | | /// Initializes a new instance of the <see cref="DispatchedObservableCollection{T}"/> class with elements from a col |
| | | 60 | | /// </summary> |
| | | 61 | | /// <param name="collection">The collection whose elements are copied to the new collection.</param> |
| | | 62 | | /// <param name="dispatcher">The dispatcher used for notifications.</param> |
| | | 63 | | public DispatchedObservableCollection(IEnumerable<T> collection, ICollectionEventDispatcher dispatcher) |
| | 0 | 64 | | : this(new ObservableRangeCollection<T>(collection), dispatcher) |
| | | 65 | | { |
| | 0 | 66 | | } |
| | | 67 | | |
| | | 68 | | /// <summary> |
| | | 69 | | /// Handles collection changed events from the inner collection and dispatches them on the specified scheduler. |
| | | 70 | | /// </summary> |
| | | 71 | | /// <param name="sender">The source of the event.</param> |
| | | 72 | | /// <param name="e">The event data.</param> |
| | | 73 | | protected override void OnCollectionChanged(object? sender, NotifyCollectionChangedEventArgs e) |
| | | 74 | | { |
| | 9 | 75 | | if (_dispatcher.CheckAccess()) |
| | | 76 | | { |
| | 6 | 77 | | base.OnCollectionChanged(this, e); |
| | 6 | 78 | | return; |
| | | 79 | | } |
| | | 80 | | |
| | 3 | 81 | | _dispatcher.Dispatch(() => base.OnCollectionChanged(this, e)); |
| | 3 | 82 | | } |
| | | 83 | | |
| | | 84 | | /// <summary> |
| | | 85 | | /// Handles property changed events from the inner collection and dispatches them on the specified scheduler. |
| | | 86 | | /// </summary> |
| | | 87 | | /// <param name="sender">The source of the event.</param> |
| | | 88 | | /// <param name="e">The event data.</param> |
| | | 89 | | protected override void OnPropertyChanged(object? sender, PropertyChangedEventArgs e) |
| | | 90 | | { |
| | 18 | 91 | | if (_dispatcher.CheckAccess()) |
| | | 92 | | { |
| | 12 | 93 | | base.OnPropertyChanged(this, e); |
| | 12 | 94 | | return; |
| | | 95 | | } |
| | | 96 | | |
| | 6 | 97 | | _dispatcher.Dispatch(() => base.OnPropertyChanged(this, e)); |
| | 6 | 98 | | } |
| | | 99 | | } |
| | | 100 | | |