@@ -23,6 +23,7 @@ import kotlinx.coroutines.Dispatchers
23
23
import kotlinx.coroutines.Job
24
24
import kotlinx.coroutines.SupervisorJob
25
25
import kotlinx.coroutines.delay
26
+ import kotlinx.coroutines.ensureActive
26
27
import kotlinx.coroutines.flow.MutableStateFlow
27
28
import kotlinx.coroutines.flow.StateFlow
28
29
import kotlinx.coroutines.flow.asStateFlow
@@ -36,6 +37,7 @@ import kotlinx.serialization.json.JsonObject
36
37
import kotlinx.serialization.json.buildJsonObject
37
38
import kotlinx.serialization.json.jsonPrimitive
38
39
import kotlinx.serialization.json.longOrNull
40
+ import kotlin.coroutines.coroutineContext
39
41
import kotlin.io.encoding.ExperimentalEncodingApi
40
42
41
43
@PublishedApi internal class RealtimeImpl (override val supabaseClient : SupabaseClient , override val config : Realtime .Config ) : Realtime {
@@ -83,6 +85,7 @@ import kotlin.io.encoding.ExperimentalEncodingApi
83
85
rejoinChannels()
84
86
}
85
87
} catch (e: Exception ) {
88
+ coroutineContext.ensureActive()
86
89
Realtime .logger.e(e) { """
87
90
Error while trying to connect to realtime websocket. Trying again in ${config.reconnectDelay}
88
91
URL: $websocketUrl
@@ -127,7 +130,7 @@ import kotlin.io.encoding.ExperimentalEncodingApi
127
130
}
128
131
}
129
132
} catch (e: Exception ) {
130
- if ( ! isActive) return @launch
133
+ coroutineContext.ensureActive()
131
134
Realtime .logger.e(e) { " Error while listening for messages. Trying again in ${config.reconnectDelay} " }
132
135
reconnect()
133
136
}
@@ -274,6 +277,7 @@ import kotlin.io.encoding.ExperimentalEncodingApi
274
277
try {
275
278
ws?.send(message)
276
279
} catch (e: Exception ) {
280
+ coroutineContext.ensureActive()
277
281
Realtime .logger.e(e) { " Error while sending message $message . Reconnecting in ${config.reconnectDelay} " }
278
282
reconnect()
279
283
}
0 commit comments