observe config value as flow
I'm sorry for the multiple issues. I've just started integrating this into an app. This one would probably be trivial to implement in my app, but it might be nice to have as part of the API.
Something like:
inline fun <reified T> Config.observe(item: Item<T>): Flow<T> =
callbackFlow {
send(get(item))
val handler = afterSet { updatedItem, value ->
if (updatedItem == item && value is T) trySend(value)
}
awaitClose { handler.cancel() }
}
edit: updated code for tested/working version
Flow is introduced since Java 9. Adding Flow integration into Konf core will break the compatibility with Java 8. The best solution may be creating a new package for Flow integration, e.g., konf-flow. I'm not familiar with reactive programing like Flow. Any pull request is welcome.
I don't know anything about Java's Flow. My example is using Kotlin flows, which are in the package kotlinx.coroutines. It would add a dependency on that (but works fine on Java 8) so should probably be in a separate package, but callbackFlow is still experimental, so maybe just having a documented example is fine.
Actually, I was looking in the wrong gradle file. kotlinx-coroutines-core is already a dependency, so you could add that extension.
Cool! I can add Kotlin Flow integration when I have time and release it in the next version. Thank you for your advise.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed after 30 days if no further activity occurs, but feel free to re-open a closed issue if needed.