Update ChainableRunnables.kt

This commit is contained in:
bluefireoly
2020-10-17 14:45:55 +02:00
parent b97bbee74a
commit 0daae1d064

View File

@@ -12,14 +12,17 @@ abstract class ChainedRunnablePart<T, R>(
var next: ChainedRunnablePart<R, *>? = null
abstract fun execute()
abstract fun <E : Exception> executeCatching(clazz: KClass<E>, handler: (E) -> Unit = {})
protected abstract fun invoke(data: T): R
abstract fun execute()
abstract fun <E : Exception> executeCatching(
@Suppress("UNCHECKED_CAST") exceptionClass: KClass<E> = Exception::class as KClass<E>,
exceptionHandler: ((E) -> Unit)? = null,
)
protected fun start(data: T) {
run {
this.run {
val result = invoke(data)
next?.start(result)
}
@@ -27,21 +30,21 @@ abstract class ChainedRunnablePart<T, R>(
protected fun <E : Exception> startCatching(
data: T,
clazz: KClass<E>,
exceptionHandler: (E) -> Unit = {}
exceptionClass: KClass<E>,
exceptionHandler: ((E) -> Unit)?
) {
run {
try {
val result = invoke(data)
next?.startCatching(result, clazz, exceptionHandler)
this.run {
val result = try {
invoke(data)
} catch (e: Exception) {
if (clazz.isInstance(e)) {
if (exceptionClass.isInstance(e)) {
@Suppress("UNCHECKED_CAST")
exceptionHandler(e as E)
} else {
throw e
}
exceptionHandler?.invoke(e as E)
null
} else throw e
}
if (result != null)
next?.startCatching(result, exceptionClass, exceptionHandler)
}
}
@@ -51,6 +54,7 @@ abstract class ChainedRunnablePart<T, R>(
else
Bukkit.getScheduler().runTaskAsynchronously(KSpigotMainInstance, realRunnable)
}
}
class ChainedRunnablePartFirst<R>(
@@ -58,10 +62,11 @@ class ChainedRunnablePartFirst<R>(
sync: Boolean
) : ChainedRunnablePart<Unit, R>(sync) {
override fun execute() = start(Unit)
override fun execute()
= start(Unit)
override fun <E : Exception> executeCatching(clazz: KClass<E>, handler: (E) -> Unit) =
startCatching(Unit, clazz, handler)
override fun <E : Exception> executeCatching(exceptionClass: KClass<E>, exceptionHandler: ((E) -> Unit)?)
= startCatching(Unit, exceptionClass, exceptionHandler)
override fun invoke(data: Unit) = runnable.invoke()
@@ -73,22 +78,24 @@ class ChainedRunnablePartThen<T, R>(
val previous: ChainedRunnablePart<*, T>
) : ChainedRunnablePart<T, R>(sync) {
override fun execute() = previous.execute()
override fun execute()
= previous.execute()
override fun <E : Exception> executeCatching(clazz: KClass<E>, handler: (E) -> Unit) =
previous.executeCatching(clazz, handler)
override fun <E : Exception> executeCatching(exceptionClass: KClass<E>, exceptionHandler: ((E) -> Unit)?)
= previous.executeCatching(exceptionClass, exceptionHandler)
override fun invoke(data: T) = runnable.invoke(data)
}
// FIRST
fun <R> firstDo(sync: Boolean, runnable: () -> R) = ChainedRunnablePartFirst(runnable, sync)
fun <R> firstDo(sync: Boolean, runnable: () -> R)
= ChainedRunnablePartFirst(runnable, sync)
fun <R> firstSync(runnable: () -> R) = firstDo(true, runnable)
fun <R> firstAsync(runnable: () -> R) = firstDo(false, runnable)
// THEN
fun <T, R, U> ChainedRunnablePart<T, R>.thenDo(sync: Boolean, runnable: (R) -> U) =
ChainedRunnablePartThen(runnable, sync, this).apply { previous.next = this }
fun <T, R, U> ChainedRunnablePart<T, R>.thenDo(sync: Boolean, runnable: (R) -> U)
= ChainedRunnablePartThen(runnable, sync, this).apply { previous.next = this }
fun <T, R, U> ChainedRunnablePart<T, R>.thenSync(runnable: (R) -> U) = thenDo(true, runnable)
fun <T, R, U> ChainedRunnablePart<T, R>.thenAsync(runnable: (R) -> U) = thenDo(false, runnable)