Skip to content

Commit

Permalink
Handle disconnect on foreground only
Browse files Browse the repository at this point in the history
  • Loading branch information
flypaper0 committed Nov 22, 2022
1 parent 88be00f commit 0434e31
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 10 deletions.
21 changes: 20 additions & 1 deletion Sources/WalletConnectRelay/AppStateObserving.swift
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
import Foundation
#if os(iOS)

#if canImport(UIKit)
import UIKit
#elseif canImport(AppKit)
import AppKit
#endif

enum ApplicationState {
case background, foreground
}

protocol AppStateObserving: AnyObject {
var currentState: ApplicationState { get }
var onWillEnterForeground: (() -> Void)? {get set}
var onWillEnterBackground: (() -> Void)? {get set}
}

class AppStateObserver: AppStateObserving {

@objc var onWillEnterForeground: (() -> Void)?

@objc var onWillEnterBackground: (() -> Void)?
Expand All @@ -17,6 +26,16 @@ class AppStateObserver: AppStateObserving {
subscribeNotificationCenter()
}

var currentState: ApplicationState {
#if canImport(UIKit)
let isActive = UIApplication.shared.applicationState == .active
return isActive ? .foreground : .background
#elseif canImport(AppKit)
let isActive = NSApplication.shared.isActive
return isActive ? .foreground : .background
#endif
}

private func subscribeNotificationCenter() {
#if os(iOS)
NotificationCenter.default.addObserver(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ class AutomaticSocketConnectionHandler {
case manualSocketConnectionForbidden, manualSocketDisconnectionForbidden
}

let socket: WebSocketConnecting

private let socket: WebSocketConnecting
private let appStateObserver: AppStateObserving
private let networkMonitor: NetworkMonitoring
private let backgroundTaskRegistrar: BackgroundTaskRegistering
Expand Down Expand Up @@ -82,6 +81,8 @@ extension AutomaticSocketConnectionHandler: SocketConnectionHandler {
}

func handleDisconnection() {
reconnect()
if appStateObserver.currentState == .foreground {
reconnect()
}
}
}
29 changes: 23 additions & 6 deletions Tests/RelayerTests/AutomaticSocketConnectionHandlerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ final class AutomaticSocketConnectionHandlerTests: XCTestCase {
var sut: AutomaticSocketConnectionHandler!
var webSocketSession: WebSocketMock!
var networkMonitor: NetworkMonitoringMock!
var appStateObserver: AppStateObserving!
var appStateObserver: AppStateObserverMock!
var backgroundTaskRegistrar: BackgroundTaskRegistrarMock!

override func setUp() {
webSocketSession = WebSocketMock()
networkMonitor = NetworkMonitoringMock()
Expand All @@ -22,9 +23,9 @@ final class AutomaticSocketConnectionHandlerTests: XCTestCase {

func testConnectsOnConnectionSatisfied() {
webSocketSession.disconnect()
XCTAssertFalse(sut.socket.isConnected)
XCTAssertFalse(webSocketSession.isConnected)
networkMonitor.onSatisfied?()
XCTAssertTrue(sut.socket.isConnected)
XCTAssertTrue(webSocketSession.isConnected)
}

func testHandleConnectThrows() {
Expand All @@ -38,7 +39,7 @@ final class AutomaticSocketConnectionHandlerTests: XCTestCase {
func testReconnectsOnEnterForeground() {
webSocketSession.disconnect()
appStateObserver.onWillEnterForeground?()
XCTAssertTrue(sut.socket.isConnected)
XCTAssertTrue(webSocketSession.isConnected)
}

func testRegisterTaskOnEnterBackground() {
Expand All @@ -49,8 +50,24 @@ final class AutomaticSocketConnectionHandlerTests: XCTestCase {

func testDisconnectOnEndBackgroundTask() {
appStateObserver.onWillEnterBackground?()
XCTAssertTrue(sut.socket.isConnected)
XCTAssertTrue(webSocketSession.isConnected)
backgroundTaskRegistrar.completion!()
XCTAssertFalse(sut.socket.isConnected)
XCTAssertFalse(webSocketSession.isConnected)
}

func testReconnectOnDisconnectForeground() {
appStateObserver.currentState = .foreground
XCTAssertTrue(webSocketSession.isConnected)
webSocketSession.disconnect()
sut.handleDisconnection()
XCTAssertTrue(webSocketSession.isConnected)
}

func testReconnectOnDisconnectBackground() {
appStateObserver.currentState = .background
XCTAssertTrue(webSocketSession.isConnected)
webSocketSession.disconnect()
sut.handleDisconnection()
XCTAssertFalse(webSocketSession.isConnected)
}
}
1 change: 1 addition & 0 deletions Tests/RelayerTests/Mocks/AppStateObserverMock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Foundation
@testable import WalletConnectRelay

class AppStateObserverMock: AppStateObserving {
var currentState: ApplicationState = .foreground
var onWillEnterForeground: (() -> Void)?
var onWillEnterBackground: (() -> Void)?
}

0 comments on commit 0434e31

Please sign in to comment.