commonMain.ru.casperix.misc.Disposable.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of misc Show documentation
Show all versions of misc Show documentation
Small things that did not find a place in respected libraries
package ru.casperix.misc
interface Disposable {
fun dispose()
}
fun getDisposable(item: T, onDispose: (T) -> Unit): Disposable {
return object : Disposable {
override fun dispose() {
onDispose(item)
}
}
}
/**
* You can dispose every item once time
*
* For example, collect signal slot, and drop it all
*/
fun MutableCollection.disposeAll() {
forEach {
it.dispose()
}
clear()
}
fun MutableCollection.disposeAll(condition: (Custom) -> Boolean) {
removeAll {
if (condition(it)) {
it.dispose()
true
} else {
false
}
}
}
fun mutableDisposableListOf(vararg disposable: Disposable): MutableList {
return mutableListOf(*disposable)
}
open class DisposableHolder : Disposable {
var isDisposed = false; private set
val components = mutableListOf()
override fun dispose() {
isDisposed = true
components.disposeAll()
}
inline fun getComponent(): T? {
return components.filterIsInstance().firstOrNull()
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy