< Summary

Information
Class: MyNet.Collections.DispatchedObservableCollection<T>
Assembly: MyNet.Collections
File(s): https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Collections/DispatchedObservableCollection.cs
Tag: 323_28699572109
Line coverage
70%
Covered lines: 14
Uncovered lines: 6
Coverable lines: 20
Total lines: 100
Line coverage: 70%
Branch coverage
83%
Covered branches: 5
Total branches: 6
Branch coverage: 83.3%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)50%22100%
.ctor(...)100%11100%
.ctor(...)100%210%
.ctor(...)100%210%
.ctor(...)100%210%
OnCollectionChanged(...)100%22100%
OnPropertyChanged(...)100%22100%

File(s)

https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Collections/DispatchedObservableCollection.cs

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="DispatchedObservableCollection.cs" company="Stéphane ANDRE">
 3// Copyright (c) Stéphane ANDRE. All rights reserved.
 4// </copyright>
 5// -----------------------------------------------------------------------
 6
 7using System;
 8using System.Collections.Generic;
 9using System.Collections.Specialized;
 10using System.ComponentModel;
 11
 12namespace 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>
 924public sealed class DispatchedObservableCollection<T>(ObservableRangeCollection<T> inner, ICollectionEventDispatcher dis
 25{
 926    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)
 633        : this([], dispatcher)
 34    {
 635    }
 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)
 044        : this(new ObservableRangeCollection<T>(capacity), dispatcher)
 45    {
 046    }
 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)
 054        : this(new ObservableRangeCollection<T>(list), dispatcher)
 55    {
 056    }
 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)
 064        : this(new ObservableRangeCollection<T>(collection), dispatcher)
 65    {
 066    }
 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    {
 975        if (_dispatcher.CheckAccess())
 76        {
 677            base.OnCollectionChanged(this, e);
 678            return;
 79        }
 80
 381        _dispatcher.Dispatch(() => base.OnCollectionChanged(this, e));
 382    }
 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    {
 1891        if (_dispatcher.CheckAccess())
 92        {
 1293            base.OnPropertyChanged(this, e);
 1294            return;
 95        }
 96
 697        _dispatcher.Dispatch(() => base.OnPropertyChanged(this, e));
 698    }
 99}
 100