konf icon indicating copy to clipboard operation
konf copied to clipboard

observe config value as flow

Open sproctor opened this issue 4 years ago • 5 comments

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

sproctor avatar Oct 21 '21 13:10 sproctor

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.

uchuhimo avatar Oct 22 '21 02:10 uchuhimo

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.

sproctor avatar Oct 25 '21 21:10 sproctor

Actually, I was looking in the wrong gradle file. kotlinx-coroutines-core is already a dependency, so you could add that extension.

sproctor avatar Oct 25 '21 22:10 sproctor

Cool! I can add Kotlin Flow integration when I have time and release it in the next version. Thank you for your advise.

uchuhimo avatar Oct 26 '21 01:10 uchuhimo

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.

stale[bot] avatar Apr 16 '22 05:04 stale[bot]