Big Chungus
01/14/2020, 8:29 AMfun runPeriodicallyAsync(intervalMS: Long, action: () -> Unit): () -> Unit {
val job = GlobalScope.launch {
coroutineScope {
while (true) {
println("Executing")
action()
println("Delaying $intervalMS ms")
delay(intervalMS)
}
}
}
println("Returning")
return {
println("Disposing")
job.cancel()
}
}
mzgreen
01/14/2020, 8:38 AM1.3.3-native-mt
version of coroutines-nativeBig Chungus
01/14/2020, 8:41 AMmzgreen
01/14/2020, 8:53 AMcoroutineScope
block inside launch
and it works fine. Also my action
block is marked with suspend
keyword.Animesh Sahu
01/14/2020, 9:02 AMBig Chungus
01/14/2020, 9:03 AMAnimesh Sahu
01/14/2020, 9:03 AMBig Chungus
01/14/2020, 9:06 AMAnimesh Sahu
01/14/2020, 9:07 AMBig Chungus
01/14/2020, 9:08 AMAnimesh Sahu
01/14/2020, 9:14 AMBig Chungus
01/14/2020, 9:15 AMmzgreen
01/14/2020, 9:15 AMGlobalScope.launch {
while(isActive) {
action()
delay(1000)
}
}
Where action
is:
suspended fun action() {
// some work
}
Animesh Sahu
01/14/2020, 9:15 AMBig Chungus
01/14/2020, 9:17 AMAnimesh Sahu
01/14/2020, 9:17 AMBig Chungus
01/14/2020, 9:18 AMAnimesh Sahu
01/14/2020, 9:18 AMBig Chungus
01/14/2020, 9:20 AMAnimesh Sahu
01/14/2020, 9:21 AMBig Chungus
01/14/2020, 9:22 AMmzgreen
01/14/2020, 9:23 AMBig Chungus
01/14/2020, 9:24 AMmzgreen
01/14/2020, 9:28 AMuli
01/14/2020, 2:47 PMBig Chungus
01/14/2020, 3:19 PM@SharedImmutable
val engineRunning = AtomicInt(0)
@SharedImmutable
private val engineDispatcher = newSingleThreadContext("engine")
actual fun startEngine(appConfigStore: AppConfigStoreImpl, universeStore: UniverseStoreImpl) {
if (engineRunning.value == 0) {
engineRunning.increment()
val stateChannel = Channel<AppConfigState>().freeze()
val dispatchChannel = Channel<Int>()
val universeStorePtr = StableRef.create(universeStore).freeze()
GlobalScope.launch {
for (action in dispatchChannel) {
universeStorePtr.get().dispatch(UniverseAction.Tick)
}
}
appConfigStore.subscribe {
GlobalScope.launch(engineDispatcher) { stateChannel.send(it.freeze()) }
}
GlobalScope.launch(engineDispatcher) {
var state = AppConfigState()
while (isActive) {
if (!stateChannel.isEmpty) {
state = stateChannel.receive()
}
if (state.running) {
dispatchChannel.send(0)
}
delay((1000 / state.fps).toLong())
}
}
} else {
println("engine already running")
}
}
julian
01/14/2020, 6:15 PMjob
is being garbage collected once you return from runPeriodicallyAsync
?Big Chungus
01/14/2020, 6:15 PMjulian
01/14/2020, 6:21 PM