Skip to content

Commit

Permalink
Added weak self references to heartbeat
Browse files Browse the repository at this point in the history
  • Loading branch information
dsrees committed Oct 10, 2024
1 parent f7098ca commit a21c465
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
8 changes: 6 additions & 2 deletions Sources/SwiftPhoenixClient/HeartbeatTimer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ class HeartbeatTimer {
}

func start(eventHandler: @escaping () -> Void) {
queue.sync {
queue.sync { [weak self] in
guard let self = self else { return }

// Create a new DispatchSourceTimer, passing the event handler
let timer = DispatchSource.makeTimerSource(flags: [], queue: queue)
timer.setEventHandler(handler: eventHandler)
Expand All @@ -98,7 +100,9 @@ class HeartbeatTimer {

func stop() {
// Must be queued synchronously to prevent threading issues.
queue.sync {
queue.sync { [weak self] in
guard let self = self else { return }

// DispatchSourceTimer will automatically cancel when released
temporaryTimer = nil
temporaryEventHandler = nil
Expand Down
11 changes: 8 additions & 3 deletions Sources/SwiftPhoenixClient/PhoenixTransport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,9 @@ open class URLSessionTransport: NSObject, PhoenixTransport, URLSessionWebSocketD
super.init()
}

deinit {
self.delegate = nil
}


// MARK: - Transport
Expand Down Expand Up @@ -274,24 +277,26 @@ open class URLSessionTransport: NSObject, PhoenixTransport, URLSessionWebSocketD
// MARK: - Private
private func receive() {
self.task?.receive { [weak self] result in
guard let self = self else { return }

switch result {
case .success(let message):
switch message {
case .data:
print("Data received. This method is unsupported by the Client")
case .string(let text):
self?.delegate?.onMessage(message: text)
self.delegate?.onMessage(message: text)
default:
fatalError("Unknown result was received. [\(result)]")
}

// Since `.receive()` is only good for a single message, it must
// be called again after a message is received in order to
// received the next message.
self?.receive()
self.receive()
case .failure(let error):
print("Error when receiving \(error)")
self?.abnormalErrorReceived(error, response: nil)
self.abnormalErrorReceived(error, response: nil)
}
}
}
Expand Down
8 changes: 6 additions & 2 deletions Sources/SwiftPhoenixClient/Socket.swift
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,10 @@ public class Socket: PhoenixTransportDelegate {
public var isConnected: Bool {
return self.connectionState == .open
}

public var isConnecting: Bool {
return self.connectionState == .connecting
}

/// - return: The state of the connect. [.connecting, .open, .closing, .closed]
public var connectionState: PhoenixTransportReadyState {
Expand All @@ -248,8 +252,8 @@ public class Socket: PhoenixTransportDelegate {
/// will be sent through the connection. If the Socket is already connected,
/// then this call will be ignored.
public func connect() {
// Do not attempt to reconnect if the socket is currently connected
guard !isConnected else { return }
// Do not attempt to reconnect if the socket is currently connected or in the process of connecting
guard !isConnected && !isConnecting else { return }

// Reset the close status when attempting to connect
self.closeStatus = .unknown
Expand Down

0 comments on commit a21c465

Please sign in to comment.