jvmMain.io.mths.swing.flow.SwingFlow.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of swing-flow Show documentation
Show all versions of swing-flow Show documentation
Kotlin Flow integration for Swing.
The newest version!
package io.mths.swing.flow
import io.mths.swing.flow.lifecycle.Lifecycle
import kotlinx.coroutines.*
import javax.swing.JComponent
import kotlin.coroutines.CoroutineContext
/**
* Interface to access lifecycle-bound [CoroutineScope].
*
* In the context of a [JComponent] you can configure [bind].
*/
interface SwingFlow {
val swingScope: CoroutineScope
get() = CoroutineScope(Dispatchers.Default + SupervisorJob())
context (Type)
fun bind(
lifecycle: Lifecycle,
init: suspend CoroutineScope.() -> Unit
) {
var job: Job? = null
lifecycle.register(
onBind = {
job = swingScope.launch(start = CoroutineStart.UNDISPATCHED) {
init()
}
},
onUnbind = {
job?.cancelChildren()
}
)
}
}
fun swingFlow(
target: Type,
init: context (SwingFlow) Type.() -> Unit
): Type =
target.apply {
init(object : SwingFlow {}, target)
}