All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.ru.casperix.misc.Disposable.kt Maven / Gradle / Ivy

There is a newer version: 1.8.4
Show newest version
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