< Summary

Information
Class: MyNet.Observable.Collections.Filters.FilterEngine<T>
Assembly: MyNet.Observable
File(s): https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Observable/Collections/Filters/FilterEngine.cs
Tag: 323_28699572109
Line coverage
92%
Covered lines: 12
Uncovered lines: 1
Coverable lines: 13
Total lines: 69
Line coverage: 92.3%
Branch coverage
90%
Covered branches: 9
Total branches: 10
Branch coverage: 90%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor()100%22100%
get_Predicate()100%11100%
Set(...)100%11100%
Clear()100%11100%
Invalidate()100%11100%
OnNext(...)87.5%8885.71%
Dispose()100%11100%

File(s)

https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Observable/Collections/Filters/FilterEngine.cs

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="FilterEngine.cs" company="Stéphane ANDRE">
 3// Copyright (c) Stéphane ANDRE. All rights reserved.
 4// </copyright>
 5// -----------------------------------------------------------------------
 6
 7using System;
 8using System.Reactive.Subjects;
 9
 10namespace MyNet.Observable.Collections.Filters;
 11
 12/// <summary>
 13/// Represents an engine that compiles a collection of filters into a single predicate function. The engine listens for 
 14/// </summary>
 15/// <typeparam name="T">The type of objects to be filtered.</typeparam>
 16public sealed class FilterEngine<T> : IDisposable
 17{
 19518    private readonly BehaviorSubject<Func<T, bool>> _predicate = new(static _ => true);
 19
 20    /// <summary>
 21    /// Gets the current root filter node that represents the combined logic of all filters in the collection. This prop
 22    /// </summary>
 23    public IFilter<T>? Current { get; private set; }
 24
 25    /// <summary>
 26    /// Gets an observable that emits the compiled predicate function whenever the filters are updated. Subscribers can 
 27    /// </summary>
 19528    public IObservable<Func<T, bool>> Predicate => _predicate;
 29
 30    /// <summary>
 31    /// Updates the filter engine with a new root filter node. This method checks if the new root is different from the 
 32    /// </summary>
 33    /// <param name="root">The new root filter node to be used by the filter engine.</param>
 5134    public void Set(IFilter<T> root) => OnNext(root);
 35
 36    /// <summary>
 37    /// Clears the filter engine by setting the root filter node to null. This method effectively removes all filters fr
 38    /// </summary>
 639    public void Clear() => OnNext(null);
 40
 41    /// <summary>
 42    /// Invalidates the filter engine by re-emitting the current root filter node. This method can be used to trigger a 
 43    /// </summary>
 644    public void Invalidate() => OnNext(Current, true);
 45
 46    /// <summary>
 47    /// Updates the filter engine with a new root filter node. This method checks if the new root is different from the 
 48    /// </summary>
 49    /// <param name="root">The new root filter node to be used by the filter engine.</param>
 50    /// <param name="force">If set to true, the filter engine will re-emit the current root filter node even if it hasn'
 51    private void OnNext(IFilter<T>? root, bool force = false)
 52    {
 6353        if (!force && ReferenceEquals(Current, root))
 054            return;
 55
 6356        Current = root;
 57
 6358        var expr = root?.ProvideExpression() ?? (_ => true);
 6359        var compiled = expr.Compile();
 60
 6361        _predicate.OnNext(compiled);
 6362    }
 63
 64    /// <summary>
 65    /// Disposes the filter engine by completing the _predicate subject and releasing any resources associated with it. 
 66    /// </summary>
 16567    public void Dispose() => _predicate.Dispose();
 68}
 69