Skip to content

Commit

Permalink
Merge pull request #483 from vinceglb/concurrent-realtime-connect-calls
Browse files Browse the repository at this point in the history
Fix concurrent calls on Realtime.connect()
  • Loading branch information
jan-tennert authored Feb 19, 2024
2 parents 3dc2ad9 + cb19f77 commit cd1c961
Showing 1 changed file with 4 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.isActive
import kotlinx.coroutines.job
import kotlinx.coroutines.launch
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import kotlinx.serialization.json.buildJsonObject

internal class RealtimeImpl(override val supabaseClient: SupabaseClient, override val config: Realtime.Config) : Realtime {
Expand All @@ -41,6 +43,7 @@ internal class RealtimeImpl(override val supabaseClient: SupabaseClient, overrid
override val subscriptions: Map<String, RealtimeChannel>
get() = _subscriptions.toMap()
private val scope = CoroutineScope(Dispatchers.Default + SupervisorJob())
private val mutex = Mutex()
var heartbeatJob: Job? = null
var messageJob: Job? = null
var ref by atomic(0)
Expand All @@ -56,7 +59,7 @@ internal class RealtimeImpl(override val supabaseClient: SupabaseClient, overrid

override suspend fun connect() = connect(false)

suspend fun connect(reconnect: Boolean) {
suspend fun connect(reconnect: Boolean): Unit = mutex.withLock {
if (reconnect) {
delay(config.reconnectDelay)
Logger.d("Realtime") { "Reconnecting..." }
Expand Down

0 comments on commit cd1c961

Please sign in to comment.