Add support for watching watchers
This commit is contained in:
@@ -19,6 +19,8 @@ class FileWatcher(private val program: Program, val file: File, private val onCh
|
||||
SensitivityWatchEventModifier.HIGH
|
||||
)
|
||||
}
|
||||
val watchers = mutableListOf<() -> Unit>()
|
||||
|
||||
init {
|
||||
watchThread
|
||||
watching.getOrPut(path) {
|
||||
@@ -34,6 +36,7 @@ class FileWatcher(private val program: Program, val file: File, private val onCh
|
||||
internal fun triggerChange() {
|
||||
program.launch {
|
||||
onChange(file)
|
||||
watchers.forEach { it() }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -77,6 +80,21 @@ fun <T> (()->T).triggerChange() {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* add watcher to file watcher
|
||||
*/
|
||||
fun <T, R> (() -> T).watch(transducer: (T) -> R):()->R {
|
||||
|
||||
var result = transducer(this())
|
||||
|
||||
watchers[this as () -> Any?]!!.watchers.add {
|
||||
result = transducer(this())
|
||||
}
|
||||
|
||||
return { result }
|
||||
}
|
||||
|
||||
|
||||
@JvmName("programWatchFile")
|
||||
fun <T> Program.watchFile(file: File, transducer: (File) -> T): () -> T = watchFile(this, file, transducer)
|
||||
|
||||
@@ -110,6 +128,7 @@ fun main() {
|
||||
it.readText()
|
||||
}
|
||||
|
||||
|
||||
a.stop()
|
||||
a.triggerChange()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user