socket.io-client-swift icon indicating copy to clipboard operation
socket.io-client-swift copied to clipboard

Timeout or way to cancel `socketClient.once()` so that `.once()` can be safely wrapped in a `withCheckedContinuation` swift async block

Open CalebKierum opened this issue 1 year ago • 0 comments

Perhaps I am over-complicating things or missing some aspect of swift async that would make this work nicely.

However the present problem I have is that socket.once() does not have a timeout nor can I get it to "cancel"

See below an example where I am trying to wrap socket.once() into swift async and use withCheckedContinuation to safely wrap up the non swift async code but have no way to call the continuation after a timeout nor propagate the cancel downwards.

private func waitForPong(_ uuid: UUID, _ count: Int) async -> Bool {
        let pongReceived = await withCheckedContinuation { continuation in
            socket.once("pong") { _, _ in
                continuation.resume(returning: true)
            }
        }
        return pongReceived
    }

If you create a task and have it await on waitForPong it will wait indefinitely for that pong. Even if you cancel() the there is no way to get rid of your now stuck Task.

Even if I off or cancel it I do not get the handler needed for the NormalCallback

CalebKierum avatar Dec 12 '24 00:12 CalebKierum