client: Add javafx utilities and basic WalletMonitor models

This commit is contained in:
Andras Slemmer 2016-08-30 08:53:02 +01:00
parent f673d18fb8
commit 91508efbd4

View File

@ -0,0 +1,38 @@
package com.r3corda.client.fxutils
import javafx.application.Platform
import javafx.beans.property.SimpleObjectProperty
import javafx.beans.value.ObservableValue
import javafx.collections.FXCollections
import javafx.collections.ObservableList
import org.reactfx.EventStream
/**
* Simple utilities for converting an [EventStream] into an [ObservableValue]/[ObservableList]
*/
fun <A, B> EventStream<A>.foldToObservable(initial: B, folderFun: (A, B) -> B): ObservableValue<B> {
val result = SimpleObjectProperty<B>(initial)
subscribe {
Platform.runLater {
result.set(folderFun(it, result.get()))
}
}
return result
}
fun <A, B, C> EventStream<A>.foldToObservableList(
initialAccumulator: C, folderFun: (A, C, ObservableList<B>) -> C
): ObservableList<B> {
val result = FXCollections.observableArrayList<B>()
/**
* This capture is fine, as [Platform.runLater] runs closures in order
*/
var currentAccumulator = initialAccumulator
subscribe {
Platform.runLater {
currentAccumulator = folderFun(it, currentAccumulator, result)
}
}
return result
}