DisposableRegistry

interface DisposableRegistry : Disposable

Interface describing a container for Disposables that provides functions for disposing all its registered items. An implementing class's Disposable declarations can be tagged with .alsoRegister() to conveniently register them as they are instantiated and assigned.

Calling dispose on the registry will call Disposable.dispose on all its registered members.

The existing implementation DisposableContainer can be extended or used as a delegate to implement this interface.

Note that since DisposableRegistry implements Disposable, registries can be stacked within each other - a registry might contain other registries, keeping track of nested assets.

Inheritors

Properties

Link copied to clipboard
abstract val registeredDisposables: List<Disposable>

A copy of the registered Disposables. The order does not necessarily represent the registration order.

Functions

Link copied to clipboard
open fun <T : Disposable> T.alsoDeregister(): T

Remove this Disposable from the DisposableRegistry if it is already registered.

Link copied to clipboard
open fun <T : Disposable> T.alsoRegister(): T

Register this Disposable with the DisposableRegistry.

Link copied to clipboard
abstract fun deregister(disposable: Disposable): Boolean

Removes disposable from this registry.

Link copied to clipboard
abstract fun deregisterAll(): Boolean

Removes all disposables from the registry without disposing them.

Link copied to clipboard
abstract override fun dispose()

Calls dispose on each registered Disposable. Might throw an exception if the assets were already disposed. To prevent that and clear the registry, use deregisterAll.

Link copied to clipboard
inline fun Disposable?.dispose(onError: (Exception) -> Unit)

Allows to dispose a resource implementing Disposable interface. Will silently ignore nulls. Exceptions will be caught and passed to onError function. This is basically an alternative to try-catch block usage.

Link copied to clipboard
fun Disposable?.disposeSafely()

Allows to gracefully dispose a resource implementing Disposable interface. Will silently ignore nulls and exceptions (except for JVM internal Errors, which should not be caught anyway).

Link copied to clipboard
abstract fun register(disposable: Disposable): Boolean

Registers disposable with this registry.