Repository navigation
JNI: AsyncStream results as SwiftAsyncStream - #14
Open
piercifani wants to merge 3 commits into
Open
piercifani wants to merge 3 commits into
piercifani wants to merge 3 commits into
Conversation
This was referenced Nov 3, 2026
Functions returning `AsyncStream<T>` were skipped: the type wasn't in the modeled standard library. - SwiftKitCore `SwiftAsyncStream<T>`: `subscribe` starts a Swift task that delivers each element and completion without blocking a Java thread; the returned `Subscription` cancels it. `map` gives a converted view. One subscriber, as for an AsyncStream; the task handle is released exactly once. - SwiftJavaRuntimeSupport `_attachAsyncStream` stores the stream and an element converter behind a handle on the Java object. - Generator: `AsyncStream` joins the stdlib model; a function returning `AsyncStream<T>` is translated as returning `T`, then Java passes a stream out-parameter and maps its elements with that conversion, and the Swift thunk attaches the stream. `async` functions returning streams and tuple elements are skipped with a reason. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A new `swiftkit-kotlin` artifact with `SwiftAsyncStream<T>.asFlow()`, so Kotlin callers collect Swift streams as a `Flow` without writing the adapter themselves. It lives outside SwiftKitCore, which stays a plain Java library with no kotlinx-coroutines dependency. Targets Java 11 like SwiftKitCore. The Kotlin sample now uses it instead of its own copy. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
piercifani
force-pushed
the
jni/10-async-stream
branch
from
November 3, 2026 16:46
e06e8b9 to
971543b
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Stack 10/21 · base:
jni/09-object-identity· previous: #13 · next: #15 (jni/11-async-throwing-stream)Problem
jextract couldn't extract a function returning
AsyncStream<T>: the type isn't in its model of the standard library, so the declaration was skipped (unknown(... AsyncStream<Item>)).Changes
A Java type for Swift streams. SwiftKitCore gets
SwiftAsyncStream<T>:subscribe(Subscriber<T>)starts a Swift task that iterates the stream and callsonNextfor each element andonCompletewhen the stream finishes. No Java thread is blocked.Subscription.cancel()cancels that task. Cancellation reaches the stream'sonTermination, so an endless producer can stop.map(...)returns a view with converted elements. AnAsyncStreamhas a single consumer, so a stream (and its mapped views) can be subscribed to once; a second subscription throwsIllegalStateException.Deliveryowns the Swift task handle and releases it exactly once, whether the stream finishes or is cancelled first, including when it finishes beforesubscribereturns.I chose this over
java.util.concurrent.Flow.Publisher(Android API 30+) and over a blockingIterator(a thread per stream).Kotlin
Flow: a newswiftkit-kotlinartifact.SwiftKitKotlinaddsSwiftAsyncStream<T>.asFlow(), acallbackFlowwhose cancellation cancels the Swift task; a slow collector makes the Swift task wait for buffer space. It's a separate artifact so SwiftKitCore stays a plain Java library without a kotlinx-coroutines dependency, asCompletableFutureresults rely on kotlinx'sawait()without SwiftKitCore depending on it. It targets Java 11, like SwiftKitCore.Runtime. SwiftJavaRuntimeSupport's
_attachAsyncStream(_:to:environment:convert:)stores the stream and an element converter behind a handle on the Java object. Subscribing consumes the handle and starts the task, which converts and delivers each element with the current thread'sJNIEnv.Generator.
AsyncStreamis added to the stdlib model (asSwift.AsyncStream, next toSet).AsyncStream<T>is translated as returningT, then converted: Java passes aSwiftAsyncStreamout-parameter (likefuture$for async functions) and returnsstream$.map(element$ -> <the conversion a T result would get>). The Swift thunk attaches the stream, with a converter that boxes primitives exactly as async results are boxed.asyncfunctions returning a stream, and elements that need several Java values (tuples), are skipped with a reported reason.Kotlin in this PR
StreamsTest.ktwithStreams.swift:asFlow()comes fromSwiftKitKotlin(import org.swift.swiftkit.kotlin.asFlow); the sample depends on it instead of kotlinx-coroutines directly.feed.prices(3).asFlow().map { it.id }.toList()collects a stream of structs.feed.live().asFlow().take(2)cancels an endless Swift stream, whoseonTerminationthen runs.Why Skip needs it
Skip bridges
AsyncStreamto KotlinFlow, which Compose collects withcollectAsState. A production Skip Fuse app (16 Skip modules) returns 18 streams from Swift to Kotlin this way.Testing
./gradlew :SwiftKitKotlin:build :Samples:SwiftJavaExtractJNIKotlinSampleApp:testpasses at this point in the stack (29 tests);swiftkit-kotlinclasses are Java 11 bytecode.JNIAsyncStreamTests(4 tests): the Java method and native signature, the Swift attach with its element converter, jextracted elements mapped to their Java class, and unsupported streams skipped with a reason.countdown(primitives) andshapes(jextractedSquares) collected in Java, plus the single-subscription rule.swift test --disable-experimental-prebuilts --disable-sandboxand./gradlew :Samples:SwiftJavaExtractJNISampleApp:testpass.swift format lint --stricton changed files;git diff --check.Not in this PR
AsyncThrowingStreamand otherAsyncSequencetypes, and streams as parameters or properties.AsyncStream.🤖 Generated with Claude Code