diff options
author | Dico Karssiens <dico.karssiens@gmail.com> | 2018-07-30 05:13:30 +0100 |
---|---|---|
committer | Dico Karssiens <dico.karssiens@gmail.com> | 2018-07-30 05:13:30 +0100 |
commit | 72c82371b1c5fa41ae96093d3929c7244ce4bcdc (patch) | |
tree | ccd0b2e64bea195b0aa81fabae9544eed541ed0b | |
parent | dee994b992b1c6df15f6c24b49cd27c25e0657d6 (diff) |
Add elapsed time to WorktimeLimiter, make /p clear more elaborate
-rw-r--r-- | src/main/kotlin/io/dico/parcels2/blockvisitor/WorktimeLimiter.kt | 45 | ||||
-rw-r--r-- | src/main/kotlin/io/dico/parcels2/command/CommandsGeneral.kt | 11 |
2 files changed, 44 insertions, 12 deletions
diff --git a/src/main/kotlin/io/dico/parcels2/blockvisitor/WorktimeLimiter.kt b/src/main/kotlin/io/dico/parcels2/blockvisitor/WorktimeLimiter.kt index 2394da0..08521fc 100644 --- a/src/main/kotlin/io/dico/parcels2/blockvisitor/WorktimeLimiter.kt +++ b/src/main/kotlin/io/dico/parcels2/blockvisitor/WorktimeLimiter.kt @@ -35,36 +35,63 @@ interface WorktimeLimiter { typealias TimeLimitedTask = suspend WorktimeLimiter.() -> Unit interface JobData { + /** + * The coroutine associated with this task, if any + */ val job: Job? + + /** + * The time that elapsed since this task was dispatched, in milliseconds + */ + val elapsedTime: Long + + /** + * true if this task has completed + */ val isComplete: Boolean + + /** + * A value indicating the progress of this task, in the range 0.0 <= progress <= 1.0 + * with no guarantees to its accuracy. May be null. + */ val progress: Double? /** * Calls the given [block] whenever the progress is updated, * if [minInterval] milliseconds expired since the last call. - * * The first call occurs after at least [minDelay] milliseconds in a likewise manner. * Repeated invocations of this method result in an [IllegalStateException] + * + * if [asCompletionListener] is true, [onCompleted] is called with the same [block] */ - fun onProgressUpdate(minDelay: Int, minInterval: Int, block: JobUpdateListener): JobData - val isUpdateBlockPresent: Boolean + fun onProgressUpdate(minDelay: Int, minInterval: Int, asCompletionListener: Boolean = true, block: JobUpdateListener): JobData /** - * Calls the given [block] when this job completes. + * Calls the given [block] when this job completes, with the progress value 1.0. + * Repeated invocations of this method result in an [IllegalStateException] */ fun onCompleted(block: JobUpdateListener): JobData } -typealias JobUpdateListener = JobData.(Double) -> Unit +typealias JobUpdateListener = JobData.(Double, Long) -> Unit class JobDataImpl(val task: TimeLimitedTask) : JobData { + override var job: Job? = null set(value) { field?.let { throw IllegalStateException() } field = value!! - value.invokeOnCompletion { onCompletedBlock?.invoke(this, 1.0) } + startTimeOrElapsedTime = System.currentTimeMillis() + value.invokeOnCompletion { + startTimeOrElapsedTime = System.currentTimeMillis() - startTimeOrElapsedTime + onCompletedBlock?.invoke(this, 1.0, elapsedTime) + } } + // when running: startTime, else: total elapsed time + private var startTimeOrElapsedTime: Long = 0L + override val elapsedTime get() = job?.let { if (it.isCompleted) startTimeOrElapsedTime else System.currentTimeMillis() - startTimeOrElapsedTime } ?: 0L + var next: Continuation<Unit>? = null override var progress: Double? = null @@ -77,20 +104,20 @@ class JobDataImpl(val task: TimeLimitedTask) : JobData { val progressUpdate = progressUpdateBlock ?: return val time = System.currentTimeMillis() if (time > lastUpdateTime + progressUpdateInterval) { - progressUpdate(progress!!) + progressUpdate(progress!!, elapsedTime) lastUpdateTime = time } } - override val isUpdateBlockPresent get() = progressUpdateBlock != null private var progressUpdateBlock: JobUpdateListener? = null private var progressUpdateInterval: Int = 0 private var lastUpdateTime: Long = 0L - override fun onProgressUpdate(minDelay: Int, minInterval: Int, block: JobUpdateListener): JobDataImpl { + override fun onProgressUpdate(minDelay: Int, minInterval: Int, asCompletionListener: Boolean, block: JobUpdateListener): JobDataImpl { progressUpdateBlock?.let { throw IllegalStateException() } progressUpdateBlock = block progressUpdateInterval = minInterval lastUpdateTime = System.currentTimeMillis() + minDelay - minInterval + if (asCompletionListener) onCompleted(block) return this } diff --git a/src/main/kotlin/io/dico/parcels2/command/CommandsGeneral.kt b/src/main/kotlin/io/dico/parcels2/command/CommandsGeneral.kt index 715e957..2de847c 100644 --- a/src/main/kotlin/io/dico/parcels2/command/CommandsGeneral.kt +++ b/src/main/kotlin/io/dico/parcels2/command/CommandsGeneral.kt @@ -1,5 +1,6 @@ package io.dico.parcels2.command +import io.dico.dicore.command.EMessageType import io.dico.dicore.command.ExecutionContext import io.dico.dicore.command.annotation.Cmd import io.dico.dicore.command.annotation.Desc @@ -83,10 +84,14 @@ class CommandsGeneral(plugin: ParcelsPlugin) : AbstractParcelCommands(plugin) { @Cmd("clear") @ParcelRequire(owner = true) fun ParcelScope.cmdClear(player: Player, context: ExecutionContext) { - val onProgressUpdate: JobUpdateListener = { progress -> context.sendMessage("[Clearing] Progress: %.06f%%".format(progress * 100)) } + val onProgressUpdate: JobUpdateListener = { progress, elapsedTime -> + context.sendMessage("[Clearing] Progress: %.06f%%".format(progress * 100)) + } world.generator.clearParcel(parcel) - .onProgressUpdate(1000, 1500, onProgressUpdate) - .onCompleted(onProgressUpdate) + .onProgressUpdate(5, 5) { progress, elapsedTime -> + context.sendMessage(EMessageType.INFORMATIVE, "Clear progress: %.06f%%, %.2fs elapsed" + .format(progress * 100, elapsedTime / 1000.0)) + } } }
\ No newline at end of file |