| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="FilterEngine.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.Reactive.Subjects; |
| | | 9 | | |
| | | 10 | | namespace 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> |
| | | 16 | | public sealed class FilterEngine<T> : IDisposable |
| | | 17 | | { |
| | 195 | 18 | | 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> |
| | 195 | 28 | | 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> |
| | 51 | 34 | | 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> |
| | 6 | 39 | | 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> |
| | 6 | 44 | | 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 | | { |
| | 63 | 53 | | if (!force && ReferenceEquals(Current, root)) |
| | 0 | 54 | | return; |
| | | 55 | | |
| | 63 | 56 | | Current = root; |
| | | 57 | | |
| | 63 | 58 | | var expr = root?.ProvideExpression() ?? (_ => true); |
| | 63 | 59 | | var compiled = expr.Compile(); |
| | | 60 | | |
| | 63 | 61 | | _predicate.OnNext(compiled); |
| | 63 | 62 | | } |
| | | 63 | | |
| | | 64 | | /// <summary> |
| | | 65 | | /// Disposes the filter engine by completing the _predicate subject and releasing any resources associated with it. |
| | | 66 | | /// </summary> |
| | 165 | 67 | | public void Dispose() => _predicate.Dispose(); |
| | | 68 | | } |
| | | 69 | | |