Skip to content

Commit a450f47

Browse files
committed
Fix race condition in WebSocketClient connection handling
1 parent 05690c0 commit a450f47

File tree

1 file changed

+5
-16
lines changed

1 file changed

+5
-16
lines changed

Sources/StreamVideo/StreamVideo.swift

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -488,24 +488,13 @@ public class StreamVideo: ObservableObject, @unchecked Sendable {
488488
} else {
489489
throw ClientError.Unknown()
490490
}
491-
var connected = false
492-
var timeout = false
493-
let control = DefaultTimer.schedule(timeInterval: 30, queue: .sdk) {
494-
timeout = true
495-
}
496491
log.debug("Listening for WS connection")
497-
webSocketClient?.onConnected = {
498-
control.cancel()
499-
connected = true
492+
let subject = PassthroughSubject<Void, Error>()
493+
webSocketClient?.onConnected = { subject.send(()) }
494+
do {
495+
try await subject.nextValue(timeout: 30)
500496
log.debug("WS connected")
501-
}
502-
503-
while (!connected && !timeout) {
504-
try await Task.sleep(nanoseconds: 100_000)
505-
}
506-
507-
if timeout {
508-
log.debug("Timeout while waiting for WS connection opening")
497+
} catch {
509498
throw ClientError.NetworkError()
510499
}
511500
}

0 commit comments

Comments
 (0)