@@ -15,6 +15,8 @@ import kotlinx.coroutines.Dispatchers
15
15
import kotlinx.coroutines.ExperimentalCoroutinesApi
16
16
import kotlinx.coroutines.NonCancellable
17
17
import kotlinx.coroutines.cancel
18
+ import kotlinx.coroutines.channels.Channel
19
+ import kotlinx.coroutines.channels.consumeEach
18
20
import kotlinx.coroutines.flow.MutableSharedFlow
19
21
import kotlinx.coroutines.flow.MutableStateFlow
20
22
import kotlinx.coroutines.flow.SharedFlow
@@ -161,7 +163,7 @@ class StormfrontClient(
161
163
private val mainStream = getStream(" main" )
162
164
private val windows = ConcurrentHashMap <String , StormfrontWindow >()
163
165
164
- private val commandQueue = MutableStateFlow < List < String >>(emptyList() )
166
+ private val commandQueue = Channel < String >( Channel . UNLIMITED )
165
167
private val currentTypeAhead = MutableStateFlow (0 )
166
168
167
169
private var menuCount = 0
@@ -217,15 +219,12 @@ class StormfrontClient(
217
219
),
218
220
).forEach { addWindow(it) }
219
221
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 }
228
225
}
226
+ currentTypeAhead.update { it + 1 }
227
+ sendCommandDirect(command)
229
228
}
230
229
}
231
230
}
@@ -722,7 +721,7 @@ class StormfrontClient(
722
721
printCommand(line)
723
722
simpleFileLogger?.write(" >$line \n " )
724
723
completeFileLogger.write(" command: $line \n " )
725
- commandQueue.update { it + line }
724
+ commandQueue.send( line)
726
725
SendCommandType .COMMAND
727
726
}
728
727
0 commit comments