I am working on a web application to visualize maze generation and solving algorithms based on Websockets. The generation algorithms implement the interface IMazeGenerator and return a Flow.
interface IMazeGenerator {
fun generate(maze: Maze): Flow<Maze>
}
After each step in the generator algorithm, the updated Maze is emitted. The collector then sends the intermediate steps to the client after a short delay.
generatorFlow
.map { it.toDto() }
.delay(150)
.onEach {
client.send(UpdateMaze(it))
}
.launchIn(this.scope)
And now to my problem: I want to offer the client the possibility to skip the intermediate steps and then send only the last result, i.e. the final Maze. To do this, I first need to be able to determine if the emitter is already done (i.e. the final maze was already emitted) and if so, send only the last result when a skip command comes.
Unfortunately I don't know at all at this point, I'm not even sure if this works with flows. Any help is welcome.