Пример #1
0
        public static ISourceBlock <T> BufferMany <T>(this ISourceBlock <T[]> source, ExecutionDataflowBlockOptions?dataflowBlockOptions = null)
        {
            if (source is null)
            {
                throw new NullReferenceException();
            }
            Contract.EndContractBlock();

            var output = dataflowBlockOptions is null
                                ? new TransformManyBlock <T[], T>(t => t)
                                : new TransformManyBlock <T[], T>(t => t, dataflowBlockOptions);;

            source.LinkToWithCompletion(output);
            return(output);
        }
Пример #2
0
        public static ISourceBlock <T> Buffer <T>(this ISourceBlock <T> source, DataflowBlockOptions?dataflowBlockOptions = null)
        {
            if (source is null)
            {
                throw new NullReferenceException();
            }
            Contract.EndContractBlock();

            var output = dataflowBlockOptions is null
                                ? new BufferBlock <T>()
                                : new BufferBlock <T>(dataflowBlockOptions);

            source.LinkToWithCompletion(output);
            return(output);
        }
Пример #3
0
        public static ISourceBlock <T[]> Batch <T>(this ISourceBlock <T> source,
                                                   int batchSize,
                                                   GroupingDataflowBlockOptions?dataflowBlockOptions = null)
        {
            if (source is null)
            {
                throw new NullReferenceException();
            }
            Contract.EndContractBlock();

            var batchBlock = dataflowBlockOptions is null
                                ? new BatchBlock <T>(batchSize)
                                : new BatchBlock <T>(batchSize, dataflowBlockOptions);

            source.LinkToWithCompletion(batchBlock);
            return(batchBlock);
        }
        /// <summary>
        /// Pipes the source data to the target.
        /// </summary>
        /// <typeparam name="T">The input type.</typeparam>
        /// <param name="source">The source block to receive from.</param>
        /// <param name="transform">The target block to post to.</param>
        /// <returns>The target block.</returns>
        public static TBlock Pipe <T, TBlock>(this ISourceBlock <T> source,
                                              TBlock target)
            where TBlock : ITargetBlock <T>
        {
            if (source is null)
            {
                throw new NullReferenceException();
            }
            if (target is null)
            {
                throw new ArgumentNullException(nameof(target));
            }
            Contract.EndContractBlock();

            source.LinkToWithCompletion(target);
            return(target);
        }
        /// <summary>
        /// Produces a source block that contains transformed results.
        /// </summary>
        /// <typeparam name="T">The input type.</typeparam>
        /// <param name="source">The source block to receive from.</param>
        /// <param name="transform">The transfrom function to apply.</param>
        /// <param name="options">Optional execution options.</param>
        /// <returns>The source block created.</returns>
        public static IReceivableSourceBlock <TOut> PipeAsync <TIn, TOut>(this ISourceBlock <TIn> source,
                                                                          Func <TIn, Task <TOut> > transform, ExecutionDataflowBlockOptions?options = null)
        {
            if (source is null)
            {
                throw new NullReferenceException();
            }
            if (transform is null)
            {
                throw new ArgumentNullException(nameof(transform));
            }
            Contract.EndContractBlock();

            var output = TransformBlock.NewAsync(transform, options);

            source.LinkToWithCompletion(output);
            return(output);
        }
        /// <summary>
        /// Pipes the source data through a single producer constrained ActionBlock.
        /// </summary>
        /// <typeparam name="T">The input type.</typeparam>
        /// <param name="source">The source block to receive from.</param>
        /// <param name="handler">The handler function to apply.</param>
        /// <param name="options">Optional execution options.</param>
        /// <returns>The ActionBlock created.</returns>
        public static ActionBlock <T> Pipe <T>(this ISourceBlock <T> source,
                                               Action <T> handler,
                                               ExecutionDataflowBlockOptions?options = null)
        {
            if (source is null)
            {
                throw new NullReferenceException();
            }
            if (handler is null)
            {
                throw new ArgumentNullException(nameof(handler));
            }
            Contract.EndContractBlock();

            var receiver = options is null
                                ? new ActionBlock <T>(handler)
                                : new ActionBlock <T>(handler, options);

            source.LinkToWithCompletion(receiver);
            return(receiver);
        }