| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Diagnostics.CodeAnalysis; |
| | | 4 | | using System.Threading.Tasks; |
| | | 5 | | using System.Threading.Tasks.Dataflow; |
| | | 6 | | |
| | | 7 | | namespace CounterpointCollective.Dataflow.Encapsulation |
| | | 8 | | { |
| | | 9 | | /// <exclude /> |
| | | 10 | | public sealed class EncapsulatedSourceBlock<T>(IDataflowBlock completeSide, ISourceBlock<T> sourceSide) : AbstractEn |
| | | 11 | | { |
| | | 12 | | protected override IDataflowBlock CompleteSide { get; } = completeSide; |
| | | 13 | | protected override ISourceBlock<T> SourceSide { get; } = sourceSide; |
| | | 14 | | } |
| | | 15 | | |
| | | 16 | | /// <exclude /> |
| | | 17 | | public abstract class AbstractEncapsulatedSourceBlock<T>: IReceivableSourceBlock<T> |
| | | 18 | | { |
| | | 19 | | protected abstract IDataflowBlock CompleteSide { get; } |
| | | 20 | | protected abstract ISourceBlock<T> SourceSide { get; } |
| | | 21 | | |
| | | 22 | | |
| | | 23 | | public virtual void Complete() |
| | 38 | 24 | | => CompleteSide.Complete(); |
| | | 25 | | |
| | | 26 | | public virtual void Fault(Exception exception) |
| | 16 | 27 | | => CompleteSide.Fault(exception); |
| | | 28 | | |
| | | 29 | | public virtual Task Completion |
| | 324443 | 30 | | => SourceSide.Completion; |
| | | 31 | | |
| | | 32 | | |
| | | 33 | | public T? ConsumeMessage(DataflowMessageHeader messageHeader, ITargetBlock<T> target, out bool messageConsumed) |
| | 21 | 34 | | SourceSide.ConsumeMessage(messageHeader, target, out messageConsumed); |
| | | 35 | | |
| | | 36 | | public IDisposable LinkTo(ITargetBlock<T> target, DataflowLinkOptions linkOptions) |
| | 547 | 37 | | => SourceSide.LinkToWithCustomCompletion(Completion, target, linkOptions); |
| | | 38 | | |
| | | 39 | | public void ReleaseReservation(DataflowMessageHeader messageHeader, ITargetBlock<T> target) |
| | 2 | 40 | | => SourceSide.ReleaseReservation(messageHeader, target); |
| | | 41 | | |
| | | 42 | | public bool ReserveMessage(DataflowMessageHeader messageHeader, ITargetBlock<T> target) |
| | 23 | 43 | | => SourceSide.ReserveMessage(messageHeader, target); |
| | | 44 | | |
| | | 45 | | public virtual bool TryReceive(Predicate<T>? filter, [MaybeNullWhen(false)] out T item) |
| | | 46 | | { |
| | 14734 | 47 | | if (SourceSide is IReceivableSourceBlock<T> receivableSource) |
| | | 48 | | { |
| | 14734 | 49 | | return receivableSource.TryReceive(filter, out item); |
| | | 50 | | } |
| | | 51 | | |
| | 0 | 52 | | item = default; |
| | 0 | 53 | | return false; |
| | | 54 | | } |
| | | 55 | | |
| | | 56 | | public virtual bool TryReceiveAll([NotNullWhen(true)] out IList<T>? items) |
| | | 57 | | { |
| | 840 | 58 | | if (SourceSide is IReceivableSourceBlock<T> receivableSource) |
| | | 59 | | { |
| | 840 | 60 | | return receivableSource.TryReceiveAll(out items); |
| | | 61 | | } |
| | | 62 | | |
| | 0 | 63 | | items = default; |
| | 0 | 64 | | return false; |
| | | 65 | | } |
| | | 66 | | } |
| | | 67 | | } |