Skip to content

Commit 3abbe51

Browse files
authored
Merge pull request #24 from sproctor/fix/command-channel
switch command queue from StateFlow<List> to Channel
2 parents 73bfac4 + 0ae0d95 commit 3abbe51

File tree

1 file changed

+9
-10
lines changed

1 file changed

+9
-10
lines changed

stormfront/src/main/kotlin/warlockfe/warlock3/stormfront/network/StormfrontClient.kt

+9-10
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import kotlinx.coroutines.Dispatchers
1515
import kotlinx.coroutines.ExperimentalCoroutinesApi
1616
import kotlinx.coroutines.NonCancellable
1717
import kotlinx.coroutines.cancel
18+
import kotlinx.coroutines.channels.Channel
19+
import kotlinx.coroutines.channels.consumeEach
1820
import kotlinx.coroutines.flow.MutableSharedFlow
1921
import kotlinx.coroutines.flow.MutableStateFlow
2022
import kotlinx.coroutines.flow.SharedFlow
@@ -161,7 +163,7 @@ class StormfrontClient(
161163
private val mainStream = getStream("main")
162164
private val windows = ConcurrentHashMap<String, StormfrontWindow>()
163165

164-
private val commandQueue = MutableStateFlow<List<String>>(emptyList())
166+
private val commandQueue = Channel<String>(Channel.UNLIMITED)
165167
private val currentTypeAhead = MutableStateFlow(0)
166168

167169
private var menuCount = 0
@@ -217,15 +219,12 @@ class StormfrontClient(
217219
),
218220
).forEach { addWindow(it) }
219221
scope.launch {
220-
commandQueue.collect { commands ->
221-
commands.firstOrNull()?.let { command ->
222-
if (maxTypeAhead > 0) {
223-
currentTypeAhead.first { it < maxTypeAhead }
224-
}
225-
currentTypeAhead.update { it + 1 }
226-
commandQueue.update { it.drop(1) }
227-
sendCommandDirect(command)
222+
commandQueue.consumeEach { command ->
223+
if (maxTypeAhead > 0) {
224+
currentTypeAhead.first { it < maxTypeAhead }
228225
}
226+
currentTypeAhead.update { it + 1 }
227+
sendCommandDirect(command)
229228
}
230229
}
231230
}
@@ -722,7 +721,7 @@ class StormfrontClient(
722721
printCommand(line)
723722
simpleFileLogger?.write(">$line\n")
724723
completeFileLogger.write("command: $line\n")
725-
commandQueue.update { it + line }
724+
commandQueue.send(line)
726725
SendCommandType.COMMAND
727726
}
728727

0 commit comments

Comments
 (0)