Hey I am working on a custom coroutine dispatcher ...
# coroutines
f
Hey I am working on a custom coroutine dispatcher for a programming game. I was wondering if anyone has experience doing that with Javascript to create a coroutine and then manual continue it onwards to create some sort of fake multi threading
o
i'm curious what the point of another dispatcher on js would be, what advantage does it have?
f
So the game is about building bots (you might have heard of screeps) and the main thing in that game is that you have N number of rooms and agents (creeps) that you control. But you have a limited amount of CPU time available. So using some sort of OS based approach is pretty helpful. The idea behind coroutines would be that I can split out execution of my tasks over multiple game ticks as the overall javascript VM does not reset between ticks
I've implemented a basic version but it feels a bit hacky and was wondering if people have better ideas
o
I think you might want to avoid using a dispatcher, and maybe just add some sort of custom
suspend
function that suspends until the next "tick", offering a much simpler interface than the dispatcher interface
f
sorry I was just calling about my code beeing the one triggering the resume etc. The only thing that I don't like at the moment is that: 1. I create a coroutine
body.createCoroutine(Continuation(process) {})
2. I store this continuation in a Map 3. In my loop I get the last continuation from that Map and call
cont.resume(Unit)cont.resume(Unit)
4. In all my suspend functions that pauses execution I have code like
Copy code
protected suspend fun yield(): Any? = suspendCoroutine { continuation ->
        process.kernel.storeContinuation(continuation)
    }
Which updates my stored continuation. and was wondering if there is not an easier/cleaner way for doing that
o
this doesn't seem like you're using a dispatcher at all?
I thought you were referring to a full
CoroutineDispatcher
or maybe just called
CoroutineInterceptor
iirc
f
yeah I was naming my code a dispatcher. I was not talking about the actual
CoroutineDispatcher