Skip to content

Add Flows.usingChannel() for concurrent flow emission#237

Merged
adamw merged 1 commit intosoftwaremill:mainfrom
denyshorman:feature/channel-flow
Jan 19, 2026
Merged

Add Flows.usingChannel() for concurrent flow emission#237
adamw merged 1 commit intosoftwaremill:mainfrom
denyshorman:feature/channel-flow

Conversation

@denyshorman
Copy link
Contributor

@denyshorman denyshorman commented Jan 17, 2026

Motivation

Jox Flow currently supports creating flows using Flows.usingEmit(), but it only works for single-threaded, sequential emission. The provided FlowEmit is thread-unsafe and cannot be shared across multiple threads.

For concurrent emission, users must manually create channels, handle cleanup, and forward elements to the flow – which is error-prone and verbose.

Solution

This PR introduces Flows.usingChannel(), which provides a thread-safe Sink for concurrent emission from multiple threads. This is equivalent to Kotlin Coroutines' channelFlow.

Kotlin vs Jox comparison:

  • Kotlin flow {} ↔ Jox Flows.usingEmit()
  • Kotlin channelFlow {} ↔ Jox Flows.usingChannel()

Example

var flow = Flows.usingChannel(sink -> 
    supervised(scope -> {
        scope.forkUser(() -> { sink.send(1); return null; });
        scope.forkUser(() -> { sink.send(2); return null; });
        return null;
    })
);

Introduces usingChannel() as a high-level API for creating flows with
concurrent, thread-safe element emission. This fills the gap in Jox's
flow API by providing an equivalent to Kotlin Coroutines' channelFlow.
@adamw adamw merged commit c657def into softwaremill:main Jan 19, 2026
9 checks passed
@adamw
Copy link
Member

adamw commented Jan 19, 2026

Thank you!

@denyshorman
Copy link
Contributor Author

🤝

@adamw
Copy link
Member

adamw commented Jan 19, 2026

Released as 0.5.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants