Library / SDK
akarnokd/kotlin-flow-extensions avatar
akarnokd/kotlin-flow-extensions

Kotlin Flow Extensions for sharing one upstream collection

Extensions to the Kotlin Flow library.

510 stars19 forksKotlinApache-2.0

At a glance

What is it?
Kotlin Flow Extensions adds shared collection, replay and concurrency policies to coroutine pipelines. Its publish overload can wait for a known number of collectors before starting a source.
Who is it for?
Consider this library for a Flow transform with a known number of consumers that need one pass of the source. A single sequential consumer can stay with standard Flow.
Can I use it commercially?
Yes. Apache-2.0 is a permissive licence: you can use, modify and sell software built on it, as long as you keep its copyright and licence notices.
Is it still maintained?
Yes. The repository last received commits 44 days ago.
What is it written in?
Mainly Kotlin, according to GitHub's language statistics.

Answers come from the project's GitHub data, last synced on October 20, 2026, and from our analysis. They are not legal advice.

Editorial analysis

One upstream collection for two branches

Kotlin Flow Extensions gives coroutine developers operators for pipelines in which several consumers need to observe the same upstream collection. Its README illustrates this with a numbered range split into even and odd branches. Both branches collect a shared flow inside the transform passed to publish. One collector of the output starts one upstream collection, and the transform can collect the shared flow more than once during that operation. This scope matters. If two callers each collect the output, the documented guarantee applies separately to each caller. The operator is useful when the branches need the same pass of a source and the result should still be exposed as a Flow.

Collector readiness changes what a branch sees

The README describes a race between the upstream source and the coroutine code that establishes the inner collectors. A fast source can finish before the transform has attached any collectors. In the documented example, the author uses the expectedCollectors overload with two collectors because merge attaches both filtered branches. Upstream collection waits for them to become ready. This makes the count part of the design of the transform. If a later refactor makes one branch optional, its readiness assumption needs review alongside the filter logic. Also distinguish the set of values the branches receive from their delivery order. The example demonstrates which values are present and gives no general ordering guarantee for all merged transforms.

Add the published artifact to a Gradle module

The README gives this declaration for a Gradle build using Groovy syntax. Put it in the dependencies block for the module that will use the extensions. The coordinate identifies version 0.0.14 of the library in Maven Central.

groovy
dependencies {
    implementation "com.github.akarnokd:kotlin-flow-extensions:0.0.14"
}

For a first use, follow the README's publish example. Start with its finite range, share it with two collectors in the transform, and use the even and odd filters to divide the items before merging their outputs. Check that the merged result contains all five input values. A local test using the coroutines version already resolved by your application can make the readiness assumption visible. A test with a deliberately fast source is especially relevant because a slow producer could hide a collector startup race.

Pick a subject by what late collectors need

PublishSubject coordinates value delivery with its collectors and waits for them to become ready for the next item or termination. That behavior fits a case where the producer can wait on the active consumers. BehaviorSubject also retains the latest value and can start with an initial one. A status display that needs to show the last known state on attachment is a concrete reason to choose that behavior. ReplaySubject retains a history and offers bounds by size, elapsed time or both. A consumer that needs several recent events must know how far back it can look. An explicit bound gives that consumer a defined history and gives the producer a way to limit what it retains.

Dropping work and buffering work have different costs

Flow.flatMapDrop maps an upstream item to an inner Flow, relays its output and ignores new upstream items while that inner flow runs. A trigger for a refresh can sometimes tolerate this behavior because another trigger during an active refresh may be redundant. A pipeline where every input represents a job needs a different guarantee. The README describes UnicastWorkSubject as buffering work for one collector and retaining subsequent items for a replacement after cancellation. Its internal buffer is unbounded. If the producer routinely outruns the collector, pending items can accumulate in memory. Standard sequential Flow collection is an alternative for a single consumer whose producer can wait. It avoids retaining a separate queue for work arriving between collectors.

Check the released build against your coroutine stack

The build file at tag v0.0.14 uses Kotlin 1.6.10 and kotlinx.coroutines 1.6.0. Those versions describe the tagged build and give a starting point for evaluating a newer application's dependency graph. Run the startup and cancellation cases your pipeline depends on with the versions your app actually resolves. The repository's last push record is dated 2026-10-12, while the v0.0.14 release record dates to 2021-12-23. Use the tag when checking what is present in that published version. The tag includes the Apache License, Version 2.0. Keep its license and notice requirements in view if you redistribute the library.

Editorial conclusion

Consider this library for a Flow transform with a known number of consumers that need one pass of the source. A single sequential consumer can stay with standard Flow. For a two-branch transform, verify that both collectors attach before the first item arrives.

Frequently asked questions

Where is Kotlin Flow Extensions published?

The README lists Maven Central and gives com.github.akarnokd:kotlin-flow-extensions:0.0.14 as the Gradle coordinate.

Can Kotlin Flow Extensions limit replay by time?

Yes. ReplaySubject provides time-bounded, size-bounded and combined constructors.

What does Kotlin Flow Extensions do with incoming items during flatMapDrop?

After an upstream item starts an inner Flow, the operator ignores further upstream items until that inner Flow completes.

Official sources

  1. akarnokd/kotlin-flow-extensions on GitHub
  2. Issues
  3. License: Apache-2.0
  4. README
  5. Releases

Was this useful?