using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Channels; using System.Threading.Tasks; namespace Wabbajack.Common { public static class ChannelStreams { public static async Task IntoChannel(this IEnumerable coll, Channel dest, bool closeAtEnd = false) { foreach (var itm in coll) { await dest.Writer.WriteAsync(itm); } if (closeAtEnd) dest.Writer.Complete(); } public static Channel AsChannel(this IEnumerable coll) { var chan = Channel.CreateUnbounded(); coll.IntoChannel(chan, true); return chan; } public static async Task> ToIEnumerable(this Channel src) { var buffer = new List(); while (true) { var result = await src.Reader.ReadAsync(); buffer.Add(result); } return buffer; } } }