watch

fun <T : Any> watch(file: File, klass: KClass<T>, onReload: (T) -> Unit): FileWatcher

Watches a configuration file for changes and invokes onReload with the freshly loaded configuration whenever the file is modified.

If a watcher already exists for the given file, the previous watcher is stopped and replaced. The returned FileWatcher is started automatically.

The reload callback is debounced (default 500ms) to avoid redundant reloads during rapid successive writes. Errors during reload are caught and printed to the configured output stream.

Example:

val watcher = YamlConfigManager.watch(configFile, ServerConfig::class) { config ->
println("Config reloaded: port=${config.port}")
}

// Later, to stop:
watcher.stop()

Return

The started FileWatcher instance.

Since

1.0

Parameters

T

The configuration data class type.

file

The YAML file to watch.

klass

The KClass of the configuration type.

onReload

Callback invoked with the reloaded configuration instance.

See also


inline fun <T : Any> watch(file: File, noinline onReload: (T) -> Unit): FileWatcher

Watches a configuration file for changes and invokes onReload with the freshly loaded configuration whenever the file is modified.

This is a convenience overload that infers the KClass from the reified type parameter.

Example:

YamlConfigManager.watch<ServerConfig>(configFile) { config ->
println("Config reloaded: port=${config.port}")
}

Return

The started FileWatcher instance.

Since

1.0

Parameters

T

The configuration data class type.

file

The YAML file to watch.

onReload

Callback invoked with the reloaded configuration instance.

See also