commonMain.org.brightify.hyperdrive.CancellationToken.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of runtime Show documentation
Show all versions of runtime Show documentation
Hyperdrive implementation that's needed for observations and such
package org.brightify.hyperdrive
import org.brightify.hyperdrive.impl.BlockCancellationToken
/**
* Implemented by classes that contain some form of cancellation logic.
*/
public interface CancellationToken {
/**
* Whether the `cancel()` method has already been called
* and the cancellation logic successfully executed.
*/
public val isCanceled: Boolean
/**
* Execute the cancellation logic.
*/
public fun cancel()
public companion object {
/**
* Create a new [CancellationToken] with a closure as the cancellation logic.
*/
public operator fun invoke(onCancel: () -> Unit): CancellationToken {
return BlockCancellationToken(onCancel)
}
/**
* Combine provided [CancellationToken] instances into one.
*/
public fun concat(tokens: Iterable): CancellationToken {
return CancellationToken {
for (token in tokens) {
token.cancel()
}
}
}
}
}
/**
* Combine all contained [CancellationToken] instances into one.
*/
public fun Iterable.concat(): CancellationToken = CancellationToken.concat(this)