< Summary

Information
Class: MyNet.Observable.Collections.Sorting.SortEngine<T>
Assembly: MyNet.Observable
File(s): https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Observable/Collections/Sorting/SortEngine.cs
Tag: 323_28699572109
Line coverage
95%
Covered lines: 20
Uncovered lines: 1
Coverable lines: 21
Total lines: 89
Line coverage: 95.2%
Branch coverage
100%
Covered branches: 2
Total branches: 2
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.cctor()100%11100%
.ctor()100%11100%
get_Comparer()100%11100%
get_Resort()100%11100%
Set(...)100%11100%
Clear()100%11100%
Invalidate()100%210%
OnNext(...)100%22100%
Dispose()100%11100%

File(s)

https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Observable/Collections/Sorting/SortEngine.cs

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="SortEngine.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.Linq;
 10using System.Reactive;
 11using System.Reactive.Subjects;
 12
 13namespace MyNet.Observable.Collections.Sorting;
 14
 15/// <summary>
 16/// Provides sorting capabilities for a collection of items of type T. It maintains the current sorting properties and e
 17/// </summary>
 18/// <typeparam name="T">The type of items to be sorted.</typeparam>
 19public sealed class SortEngine<T> : IDisposable
 20{
 21    /// <summary>
 22    /// Preserves source order when no explicit sort keys are configured (avoids <see cref="Comparer{T}.Default"/> on no
 23    /// </summary>
 2424    private static readonly IComparer<T> SourceOrderComparer = Comparer<T>.Create(static (_, _) => 0);
 25
 19226    private readonly BehaviorSubject<IComparer<T>> _comparer = new(SourceOrderComparer);
 19227    private readonly Subject<Unit> _resort = new();
 28
 29    /// <summary>
 30    /// Gets the current sorting properties applied to the collection. This property holds an array of sorting propertie
 31    /// </summary>
 19232    public ISortingProperty<T>[] Current { get; private set; } = [];
 33
 34    /// <summary>
 35    /// Gets an observable that emits the current comparer based on the sorting properties. Whenever the sorting propert
 36    /// </summary>
 37237    public IObservable<IComparer<T>> Comparer => _comparer;
 38
 39    /// <summary>
 40    /// Gets an observable that emits a notification whenever the sorting engine is invalidated. This observable can be 
 41    /// </summary>
 37242    public IObservable<Unit> Resort => _resort;
 43
 44    /// <summary>
 45    /// Updates the sorting engine with a new set of sorting properties. This method takes an enumerable of sorting prop
 46    /// </summary>
 47    /// <param name="sorting">The new set of sorting properties to apply.</param>
 2748    public void Set(IEnumerable<ISortingProperty<T>> sorting) => OnNext(sorting);
 49
 50    /// <summary>
 51    /// Clears the sorting engine by setting the sorting properties to an empty array. This method effectively removes a
 52    /// </summary>
 7853    public void Clear() => OnNext([]);
 54
 55    /// <summary>
 56    /// Invalidates the sorting engine by re-emitting the current sorting properties. This method can be used to trigger
 57    /// </summary>
 058    public void Invalidate() => _resort.OnNext(Unit.Default);
 59
 60    /// <summary>
 61    /// Updates the sorting engine with a new set of sorting properties. This method takes an enumerable of sorting prop
 62    /// </summary>
 63    /// <param name="sorting">The new set of sorting properties to apply.</param>
 64    private void OnNext(IEnumerable<ISortingProperty<T>> sorting)
 65    {
 10566        var newSorting = sorting.ToArray();
 10567        Current = newSorting;
 68
 10569        if (newSorting.Length == 0)
 70        {
 8471            _comparer.OnNext(SourceOrderComparer);
 8472            _resort.OnNext(Unit.Default);
 8473            return;
 74        }
 75
 2176        _comparer.OnNext(new SortingComparer<T>(newSorting));
 2177        _resort.OnNext(Unit.Default);
 2178    }
 79
 80    /// <summary>
 81    /// Disposes the sort engine by disposing the underlying comparer subject. This method is called when the sort engin
 82    /// </summary>
 83    public void Dispose()
 84    {
 16285        _comparer.Dispose();
 16286        _resort.Dispose();
 16287    }
 88}
 89