From 413c587e29d3b54d69391bde63b48aaa876ca014 Mon Sep 17 00:00:00 2001 From: bluefireoly Date: Mon, 12 Oct 2020 18:12:05 +0200 Subject: [PATCH] Improved ChainableRunnables --- .../kspigot/runnables/ChainableRunnables.kt | 66 +++++++++++-------- 1 file changed, 39 insertions(+), 27 deletions(-) diff --git a/src/main/kotlin/net/axay/kspigot/runnables/ChainableRunnables.kt b/src/main/kotlin/net/axay/kspigot/runnables/ChainableRunnables.kt index a9008b34..c1945cab 100644 --- a/src/main/kotlin/net/axay/kspigot/runnables/ChainableRunnables.kt +++ b/src/main/kotlin/net/axay/kspigot/runnables/ChainableRunnables.kt @@ -5,24 +5,19 @@ package net.axay.kspigot.runnables import net.axay.kspigot.main.KSpigotMainInstance import org.bukkit.Bukkit -/* - * Chainable bukkit runnable. - */ - -class ChainedRunnablePart( - val runnable: (T?) -> R, - val sync: Boolean, - var previous: ChainedRunnablePart<*, T>? = null, - var next: ChainedRunnablePart? = null +abstract class ChainedRunnablePart( + val sync: Boolean ) { - fun execute() { - previous?.execute() ?: kotlin.run { start(null) } - } + var next: ChainedRunnablePart? = null - private fun start(data: T?) { + abstract fun execute() + + protected abstract fun invoke(data: T): R + + protected fun start(data: T) { val realRunnable = Runnable { - val result = runnable.invoke(data) + val result = invoke(data) next?.start(result) } if (sync) @@ -33,19 +28,36 @@ class ChainedRunnablePart( } +class ChainedRunnablePartFirst( + val runnable: () -> R, + sync: Boolean +) : ChainedRunnablePart(sync) { + + override fun execute() = start(Unit) + + override fun invoke(data: Unit) = runnable.invoke() + +} + +class ChainedRunnablePartThen( + val runnable: (T) -> R, + sync: Boolean, + val previous: ChainedRunnablePart<*, T> +) : ChainedRunnablePart(sync) { + + override fun execute() = previous.execute() + + override fun invoke(data: T) = runnable.invoke(data) + +} + // FIRST -fun firstDo(sync: Boolean, runnable: (Unit?) -> R) - = ChainedRunnablePart(runnable, sync) -fun firstSync(runnable: (Unit?) -> R) = firstDo(true, runnable) -fun firstAsync(runnable: (Unit?) -> R) = firstDo(false, runnable) +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): ChainedRunnablePart { - ChainedRunnablePart(runnable, sync).apply { - previous = this@thenDo - this@thenDo.next = this - return 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 +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