mirror of
https://github.com/2006-Scape/apollo.git
synced 2026-07-05 16:49:04 +00:00
Add an await function to the AsyncActionRunner trait
This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
package org.apollo.game.action
|
||||
|
||||
import kotlinx.coroutines.experimental.*
|
||||
import kotlinx.coroutines.experimental.CommonPool
|
||||
import kotlinx.coroutines.experimental.Job
|
||||
import kotlinx.coroutines.experimental.channels.Channel
|
||||
import kotlinx.coroutines.experimental.launch
|
||||
import kotlinx.coroutines.experimental.selects.select
|
||||
import org.apollo.game.model.entity.Mob
|
||||
import java.util.function.Supplier
|
||||
|
||||
class AsyncActionRunner(val actionSupplier: () -> Action<*>, val callback: suspend () -> Unit) {
|
||||
var job: Job? = null
|
||||
@@ -53,4 +53,15 @@ class AsyncActionRunner(val actionSupplier: () -> Action<*>, val callback: suspe
|
||||
remainingPulses -= numPulses
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun await(condition: () -> Boolean, timeout: Int = 15) {
|
||||
var remainingPulsesBeforeTimeout = timeout
|
||||
|
||||
while (!condition.invoke()) {
|
||||
remainingPulsesBeforeTimeout -= pulseChannel.receive()
|
||||
if (remainingPulsesBeforeTimeout <= 0) {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user