| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="PipelineEngine.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.Linq; |
| | | 9 | | using DynamicData; |
| | | 10 | | using MyNet.Observable.Collections.Filters; |
| | | 11 | | using MyNet.Observable.Collections.Paging; |
| | | 12 | | using MyNet.Observable.Collections.Sorting; |
| | | 13 | | |
| | | 14 | | namespace MyNet.Observable.Collections; |
| | | 15 | | |
| | | 16 | | /// <summary> |
| | | 17 | | /// Provides a pipeline engine that builds sorted and filtered observables from a source observable of change sets. The |
| | | 18 | | /// </summary> |
| | | 19 | | internal static class PipelineEngine |
| | | 20 | | { |
| | | 21 | | /// <summary> |
| | | 22 | | /// Builds sorted and filtered observables from a source observable of change sets, applying sorting and filtering o |
| | | 23 | | /// </summary> |
| | | 24 | | /// <param name="source">The source observable of change sets.</param> |
| | | 25 | | /// <param name="filterEngine">The filter engine to apply filtering operations.</param> |
| | | 26 | | /// <param name="sortEngine">The sort engine to apply sorting operations.</param> |
| | | 27 | | /// <typeparam name="T">The type of items in the collection.</typeparam> |
| | | 28 | | /// <returns>A tuple containing the sorted and filtered observables.</returns> |
| | | 29 | | public static (IObservable<IChangeSet<T>> Sorted, IObservable<IChangeSet<T>> Filtered) Build<T>( |
| | | 30 | | IObservable<IChangeSet<T>> source, |
| | | 31 | | FilterEngine<T> filterEngine, |
| | | 32 | | SortEngine<T> sortEngine) |
| | | 33 | | where T : notnull |
| | | 34 | | { |
| | 186 | 35 | | var sorted = source |
| | 186 | 36 | | .Sort(sortEngine.Comparer, resort: sortEngine.Resort); |
| | | 37 | | |
| | 186 | 38 | | var filtered = source |
| | 186 | 39 | | .Filter(filterEngine.Predicate) |
| | 186 | 40 | | .Sort(sortEngine.Comparer, resort: sortEngine.Resort); |
| | | 41 | | |
| | 186 | 42 | | return (sorted, filtered); |
| | | 43 | | } |
| | | 44 | | |
| | | 45 | | /// <summary> |
| | | 46 | | /// Applies a paging window on top of a filtered and sorted change set stream. |
| | | 47 | | /// </summary> |
| | | 48 | | /// <param name="filtered">The filtered and sorted change set stream.</param> |
| | | 49 | | /// <param name="pagingEngine">The paging engine providing the active page window.</param> |
| | | 50 | | /// <typeparam name="T">The type of items in the collection.</typeparam> |
| | | 51 | | /// <returns>The paged change set stream.</returns> |
| | | 52 | | public static IObservable<IChangeSet<T>> ApplyPaging<T>( |
| | | 53 | | IObservable<IChangeSet<T>> filtered, |
| | | 54 | | PagingEngine pagingEngine) |
| | | 55 | | where T : notnull |
| | | 56 | | { |
| | 186 | 57 | | var pageRequests = pagingEngine.State |
| | 186 | 58 | | .Select(state => state switch |
| | 186 | 59 | | { |
| | 186 | 60 | | null => null, |
| | 186 | 61 | | var (page, pageSize) => (IPageRequest?)new PageRequest(Math.Max(page, 1), pageSize) |
| | 186 | 62 | | }) |
| | 186 | 63 | | .Where(request => request is not null) |
| | 186 | 64 | | .Select(request => request!); |
| | | 65 | | |
| | 186 | 66 | | return pagingEngine.State |
| | 186 | 67 | | .Select(state => state is null ? filtered : filtered.Page(pageRequests)) |
| | 186 | 68 | | .Switch(); |
| | | 69 | | } |
| | | 70 | | } |
| | | 71 | | |