Exemple #1
0
        public static async Task <int> AllLinesTo(this TextReader source,
                                                  ITargetBlock <string> target,
                                                  bool completeAndAwait = false,
                                                  CancellationToken cancellationToken = default)
        {
            if (source is null)
            {
                throw new NullReferenceException();
            }
            Contract.EndContractBlock();

            var    count = 0;
            string line;

            while (!cancellationToken.IsCancellationRequested &&
                   (line = await source.ReadLineAsync()) is not null)
            {
                if (!target.Post(line) && !await target.SendAsync(line, cancellationToken))
                {
                    break;
                }

                count++;
            }

            cancellationToken.ThrowIfCancellationRequested();

            if (completeAndAwait)
            {
                await target.CompleteAsync();
            }

            return(count);
        }