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
Way to Register Change Listener? #64
Comments
@MrThreepwood You can use the following API to subscribe the update event of any item: object Server : ConfigSpec() {
val host by optional("0.0.0.0")
}
val handler = Server.host.onSet { value -> println("the host has changed to $value") }
// cancel the subscription
handler.cancel() I have added the API description to README. |
Hope its ok to misuse this issue for a follow-up question: @uchuhimo When using onSet to trigger some kind of "re-read" of the changed config value (reading from the config not from the provided value in onSet), a com.uchuhimo.konf.UnsetValueException is raised. It seems that this happens because the onSet is triggered before the actual setState is performed (in BaseConfig.kt):
My use case is that I want to reconfigure my application by reading from the current config after the config was changed. Is there some other way to do this? Or is there no special reason for having the Thanks a lot and best regards! |
@dieterdeagle If |
@uchuhimo But why would one want to access the old value of the item? Is that a real use case? And isn't that the case anyway because the Item is 'unset' - so you can't read the old nor the new value? I probably didn't explain well enough what I was trying to achieve, so I'll give it another try (or maybe I am misunderstanding a basic concept?): So let's say we have some kind of Spec called ServerSpec which we use in a config that is read from a watched file:
After that, we do the initial expensive setup of our application based on this config:
where
After this initial setup, we would like to re-trigger this expensive setup using the then new value(s) whenever (but only then) there was a change to the item. So my idea was, to register a listener that calls
My expectation was, that when a listener is informed about a change to a property, he can read the new value from the config. But apparently, the value is in an If the current implementation is in fact behaving as intended: is there a 'correct' way to achieve what I tried to explain above? Thanks a lot for your time! :) |
@dieterdeagle Config { addSpec(ServerSpec) }.from.yaml.watchFile("myConfig.yml") { config ->
doExpensiveStuffToSetupMyApplication(config)
} so that the setup will be triggered when file is reloaded. Any suggestion? |
@uchuhimo That would definitely work and would be much better than triggering the setup on individual property updates. |
@dieterdeagle If a config-based setup is re-triggered once the config changes, a config file with 10 items will trigger 10 expensive setups. |
@uchuhimo That's a good point, didn't think about that. |
@dieterdeagle I have released v1.1.1, which contains all the listeners mentioned above. Check release notes (https://github.com/uchuhimo/konf/releases/tag/v1.1.1) and README (https://github.com/uchuhimo/konf) for the new APIs. For example, you can use the following API for your use case: Config { addSpec(ServerSpec) }.from.yaml.watchFile("myConfig.yml") { config, source ->
doExpensiveStuffToSetupMyApplication(config)
} or val config = Config { addSpec(ServerSpec) }.from.yaml.watchFile("myConfig.yml")
config.afterLoad { source -> doExpensiveStuffToSetupMyApplication(config) } |
@uchuhimo Works like a charm! Again: Thanks a lot! :) |
Is there a way to call a callback when a value changes? We can setup watchFile to look for changes which is really nifty, but is there any way to be alerted when a property has changed, or even just be alerted when the file is changed?
Since the values in a property file are often used for longer lived classes we often need to take action when a property has changed. Specifically in my case, our DB usernames and passwords rotate, and i need to update my connection pool whenever this happens.
The text was updated successfully, but these errors were encountered: