diff --git a/src/main/kotlin/net/axay/kspigot/runnables/ChainableRunnables.kt b/src/main/kotlin/net/axay/kspigot/runnables/ChainableRunnables.kt index 363d392f..8bd8176b 100644 --- a/src/main/kotlin/net/axay/kspigot/runnables/ChainableRunnables.kt +++ b/src/main/kotlin/net/axay/kspigot/runnables/ChainableRunnables.kt @@ -12,14 +12,17 @@ abstract class ChainedRunnablePart( var next: ChainedRunnablePart? = null - abstract fun execute() - - abstract fun executeCatching(clazz: KClass, handler: (E) -> Unit = {}) - protected abstract fun invoke(data: T): R + abstract fun execute() + + abstract fun executeCatching( + @Suppress("UNCHECKED_CAST") exceptionClass: KClass = Exception::class as KClass, + 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( protected fun startCatching( data: T, - clazz: KClass, - exceptionHandler: (E) -> Unit = {} + exceptionClass: KClass, + 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( else Bukkit.getScheduler().runTaskAsynchronously(KSpigotMainInstance, realRunnable) } + } class ChainedRunnablePartFirst( @@ -58,10 +62,11 @@ class ChainedRunnablePartFirst( sync: Boolean ) : ChainedRunnablePart(sync) { - override fun execute() = start(Unit) + override fun execute() + = start(Unit) - override fun executeCatching(clazz: KClass, handler: (E) -> Unit) = - startCatching(Unit, clazz, handler) + override fun executeCatching(exceptionClass: KClass, exceptionHandler: ((E) -> Unit)?) + = startCatching(Unit, exceptionClass, exceptionHandler) override fun invoke(data: Unit) = runnable.invoke() @@ -73,22 +78,24 @@ class ChainedRunnablePartThen( val previous: ChainedRunnablePart<*, T> ) : ChainedRunnablePart(sync) { - override fun execute() = previous.execute() + override fun execute() + = previous.execute() - override fun executeCatching(clazz: KClass, handler: (E) -> Unit) = - previous.executeCatching(clazz, handler) + override fun executeCatching(exceptionClass: KClass, exceptionHandler: ((E) -> Unit)?) + = previous.executeCatching(exceptionClass, exceptionHandler) override fun invoke(data: T) = runnable.invoke(data) } // FIRST -fun firstDo(sync: Boolean, runnable: () -> R) = ChainedRunnablePartFirst(runnable, sync) +fun firstDo(sync: Boolean, runnable: () -> R) + = ChainedRunnablePartFirst(runnable, sync) fun firstSync(runnable: () -> R) = firstDo(true, runnable) fun firstAsync(runnable: () -> R) = firstDo(false, runnable) // THEN -fun ChainedRunnablePart.thenDo(sync: Boolean, runnable: (R) -> U) = - ChainedRunnablePartThen(runnable, sync, this).apply { previous.next = this } +fun ChainedRunnablePart.thenDo(sync: Boolean, runnable: (R) -> U) + = ChainedRunnablePartThen(runnable, sync, this).apply { previous.next = this } fun ChainedRunnablePart.thenSync(runnable: (R) -> U) = thenDo(true, runnable) fun ChainedRunnablePart.thenAsync(runnable: (R) -> U) = thenDo(false, runnable) \ No newline at end of file