Skip to content

Commit

Permalink
Cancel coroutine started in startContext when stopContext is invoked (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
being-eyram authored Dec 29, 2022
1 parent d6339f1 commit 03904b9
Showing 1 changed file with 4 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ class Client (
) : ApiClient {
private val streams: MutableList<SluStream> = mutableListOf()
private val deviceId: UUID = deviceIdProvider.getDeviceId()
private val coroutineScope = CoroutineScope(Job())

init {
GlobalScope.launch(Dispatchers.IO) {
Expand Down Expand Up @@ -184,15 +185,15 @@ class Client (
@DelicateCoroutinesApi
@ExperimentalCoroutinesApi
override fun startContext() {
GlobalScope.launch(Dispatchers.IO) {
coroutineScope.launch(Dispatchers.IO) {
val token = identityService.authenticate(appId, deviceId)
try {
val audioFlow: Flow<ByteArray> = audioRecorder.startRecording()
val stream = sluClient.stream(token, streamConfig, audioFlow)
streams.add(stream)

var segment: Segment? = null
GlobalScope.launch(Dispatchers.Default) {
withContext(Dispatchers.Default) {
try {
stream.responseFlow?.collect { response: SLUResponse ->
if (segment == null) {
Expand Down Expand Up @@ -278,6 +279,7 @@ class Client (
it.close()
this.streams.remove(it)
}
this.coroutineScope.cancel()
}

override fun close() {
Expand Down

0 comments on commit 03904b9

Please sign in to comment.