Skip to content

[Repo Assist] feat: add TaskSeq.toChannelAsync and TaskSeq.ofChannel for System.Threading.Channels integration#416

Draft
github-actions[bot] wants to merge 2 commits intomainfrom
repo-assist/feat-channel-integration-20260430-c520bd85af285b66
Draft

[Repo Assist] feat: add TaskSeq.toChannelAsync and TaskSeq.ofChannel for System.Threading.Channels integration#416
github-actions[bot] wants to merge 2 commits intomainfrom
repo-assist/feat-channel-integration-20260430-c520bd85af285b66

Conversation

@github-actions
Copy link
Copy Markdown
Contributor

🤖 This is an automated pull request from Repo Assist, an AI assistant.

Closes #415.

Summary

Adds two new functions to bridge TaskSeq with System.Threading.Channels:

Function Description
TaskSeq.toChannelAsync Writes all elements of a TaskSeq<'T> to a ChannelWriter<'T>, then marks the writer complete
TaskSeq.ofChannel Creates a TaskSeq<'T> from a ChannelReader<'T>, yielding elements as they become available

These enable natural producer/consumer patterns with BCL channels — for example writing results from an async sequence pipeline directly into a Channel<'T> for parallel consumption, or turning a channel into a composable TaskSeq.

Implementation

toChannelAsync writer source — iterates source using the standard while! e.MoveNextAsync() enumerator pattern and calls writer.WriteAsync for each element. On normal completion it calls writer.TryComplete(); on exception it calls writer.TryComplete(exn) so downstream readers observe the failure.

ofChannel reader — uses the idiomatic channels drain loop:

taskSeq {
    while! reader.WaitToReadAsync() do
        let mutable item = Unchecked.defaultof<_>
        while reader.TryRead &item do
            yield item
}

WaitToReadAsync() returns false when the channel is completed and empty, ending the sequence naturally.

New dependency

System.Threading.Channels 8.0.0 is added to the library fsproj. This package is owned by Microsoft/dotnetframework (1.4 billion downloads) and provides the System.Threading.Channels namespace for netstandard2.1 consumers. For net5.0+ consumers the package is a no-op (the types are already built in). This is the minimal addition needed to expose the channel API surface.

Tests

29 new xUnit tests in TaskSeq.ToXXX.Tests.fs covering:

  • Null argument guards for both functions
  • Empty sequence → channel marked complete, reader yields empty
  • Immutable variants: all 10 elements written and read back correctly
  • Completion task resolves after data is drained
  • ofChannel terminates naturally when channel is completed and drained
  • Side-effect sequences execute correctly via toChannelAsync

Test Status

✅ Build succeeded (Release)
dotnet fantomas . --check — no formatting issues
✅ All 5280 tests pass (5258 pre-existing + 29 new; 2 real-world tests remain skipped as before)

Generated by 🌈 Repo Assist, see workflow run. Learn more.

To install this agentic workflow, run

gh aw add githubnext/agentics/workflows/repo-assist.md@96b9d4c39aa22359c0b38265927eadb31dcf4e2a

…tegration, closes #415

- TaskSeq.toChannelAsync: writes all elements of a TaskSeq to a ChannelWriter<'T>,
  then marks the writer complete. On exception, completes the writer with the exception
  so downstream readers observe it.

- TaskSeq.ofChannel: creates a TaskSeq<'T> from a ChannelReader<'T>, yielding elements
  as they become available. The sequence ends naturally when the channel is completed
  and fully drained, using the 'while! reader.WaitToReadAsync()' pattern.

- Adds System.Threading.Channels 8.0.0 package reference to the library (needed for
  netstandard2.1; built-in to net5.0+). This is the only new dependency.

- 29 new tests in TaskSeq.ToXXX.Tests.fs covering empty/non-empty sequences, null
  argument guards, round-trip correctness, side-effect semantics, and completion.

All 5280 tests pass (5258 pre-existing + 29 new + 2 skipped).

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Functions for working with TaskSeq and Channel?

0 participants