From 808d6c70daa10186d0084f44b3dfa2b8d4c88789 Mon Sep 17 00:00:00 2001 From: Koray Koska <11356621+koraykoska@users.noreply.github.com> Date: Fri, 24 Mar 2023 22:08:30 +0300 Subject: [PATCH] fix: remove retain of self in websocket provider (#158) --- Sources/FoundationHTTP/Web3WebSocketProvider.swift | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/Sources/FoundationHTTP/Web3WebSocketProvider.swift b/Sources/FoundationHTTP/Web3WebSocketProvider.swift index 287d36fa..6bad158a 100644 --- a/Sources/FoundationHTTP/Web3WebSocketProvider.swift +++ b/Sources/FoundationHTTP/Web3WebSocketProvider.swift @@ -273,7 +273,11 @@ public class Web3WebSocketProvider: Web3Provider, Web3BidirectionalProvider { private func registerWebSocketListeners() { // Receive response - webSocket.onText { ws, string in + webSocket.onText { [weak self] ws, string in + guard let self else { + return + } + self.receiveQueue.async { guard let data = string.data(using: .utf8) else { return @@ -298,7 +302,11 @@ public class Web3WebSocketProvider: Web3Provider, Web3BidirectionalProvider { } // Handle close - webSocket.onClose.whenComplete { result in + webSocket.onClose.whenComplete { [weak self] result in + guard let self else { + return + } + if !self.closed && self.webSocket.isClosed { self.reconnectQueue.asyncAfter(deadline: DispatchTime(uptimeNanoseconds: DispatchTime.now().uptimeNanoseconds + 100_000_000)) { try? self.reconnect()