Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

observe config value as flow #71

Open
sproctor opened this issue Oct 21, 2021 · 5 comments
Open

observe config value as flow #71

sproctor opened this issue Oct 21, 2021 · 5 comments
Labels

Comments

@sproctor
Copy link

sproctor commented Oct 21, 2021

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

@uchuhimo
Copy link
Owner

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.

@sproctor
Copy link
Author

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
Copy link
Author

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

@uchuhimo
Copy link
Owner

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

@stale
Copy link

stale bot commented Apr 16, 2022

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 stale bot added the stale label Apr 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants