Skip to content

Commit

Permalink
Added connectionState variable
Browse files Browse the repository at this point in the history
  • Loading branch information
dsrees committed Feb 27, 2023
1 parent 81f96ad commit fc38c21
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 18 deletions.
7 changes: 6 additions & 1 deletion Sources/SwiftPhoenixClient/Socket.swift
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,12 @@ public class Socket: PhoenixTransportDelegate {

/// - return: True if the socket is connected
public var isConnected: Bool {
return self.connection?.readyState == .open
return self.connectionState == .open
}

/// - return: The state of the connect. [.connecting, .open, .closing, .closed]
public var connectionState: PhoenixTransportReadyState {
return self.connection?.readyState ?? .closed
}

/// Connects the Socket. The params passed to the Socket on initialization
Expand Down
12 changes: 6 additions & 6 deletions Tests/Mocks/MockableClass.generated.swift
Original file line number Diff line number Diff line change
Expand Up @@ -816,11 +816,11 @@ class SocketMock: Socket {
var onErrorCallbackCalled: Bool {
return onErrorCallbackCallsCount > 0
}
var onErrorCallbackReceivedCallback: ((Error) -> Void)?
var onErrorCallbackReceivedCallback: (((Error, URLResponse?)) -> Void)?
var onErrorCallbackReturnValue: String!
var onErrorCallbackClosure: ((@escaping (Error) -> Void) -> String)?
var onErrorCallbackClosure: ((@escaping ((Error, URLResponse?)) -> Void) -> String)?

override func onError(callback: @escaping (Error) -> Void) -> String {
override func onError(callback: @escaping ((Error, URLResponse?)) -> Void) -> String {
onErrorCallbackCallsCount += 1
onErrorCallbackReceivedCallback = callback
return onErrorCallbackClosure.map({ $0(callback) }) ?? makeRefReturnValue
Expand All @@ -836,7 +836,7 @@ class SocketMock: Socket {
var delegateOnErrorToCallbackReturnValue: String!

override func delegateOnError<T: AnyObject>(to owner: T,
callback: @escaping ((T, Error) -> Void)) -> String {
callback: @escaping ((T, (Error, URLResponse?)) -> Void)) -> String {
delegateOnErrorToCallbackCallsCount += 1
return makeRefReturnValue
}
Expand Down Expand Up @@ -1028,7 +1028,7 @@ class SocketMock: Socket {
var onConnectionErrorReceivedError: Error?
var onConnectionErrorClosure: ((Error) -> Void)?

override func onConnectionError(_ error: Error) {
override func onConnectionError(_ error: Error, response: URLResponse?) {
onConnectionErrorCallsCount += 1
onConnectionErrorReceivedError = error
onConnectionErrorClosure?(error)
Expand Down Expand Up @@ -1178,7 +1178,7 @@ class SocketMock: Socket {
var onErrorErrorReceivedError: Error?
var onErrorErrorClosure: ((Error) -> Void)?

override func onError(error: Error) {
override func onError(error: Error, response: URLResponse?) {
onErrorErrorCallsCount += 1
onErrorErrorReceivedError = error
onErrorErrorClosure?(error)
Expand Down
4 changes: 2 additions & 2 deletions Tests/SwiftPhoenixClientTests/ChannelSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -640,7 +640,7 @@ class ChannelSpec: QuickSpec {
let mockPush = PushMock(channel: channel, event: "event")
channel.joinPush = mockPush

spySocket.onConnectionError(TestError.stub)
spySocket.onConnectionError(TestError.stub, response: nil)

fakeClock.tick(1.0)
expect(mockPush.sendCallsCount).to(equal(0))
Expand All @@ -657,7 +657,7 @@ class ChannelSpec: QuickSpec {
let mockPush = PushMock(channel: channel, event: "event")
channel.joinPush = mockPush

spySocket.onConnectionError(TestError.stub)
spySocket.onConnectionError(TestError.stub, response: nil)

fakeClock.tick(1.0)
expect(mockPush.sendCallsCount).to(equal(0))
Expand Down
18 changes: 9 additions & 9 deletions Tests/SwiftPhoenixClientTests/SocketSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ class SocketSpec: QuickSpec {
var close = 0
socket.onClose { close += 1 }

var lastError: Error?
var lastError: (Error, URLResponse?)?
socket.onError(callback: { (error) in lastError = error })

var lastMessage: Message?
Expand All @@ -219,7 +219,7 @@ class SocketSpec: QuickSpec {
mockWebSocket.delegate?.onClose(code: 1000)
expect(close).to(equal(1))

mockWebSocket.delegate?.onError(error: TestError.stub)
mockWebSocket.delegate?.onError(error: TestError.stub, response: nil)
expect(lastError).toNot(beNil())

let data: [Any] = ["2", "6", "topic", "event", ["response": ["go": true], "status": "ok"]]
Expand All @@ -235,7 +235,7 @@ class SocketSpec: QuickSpec {
var close = 0
socket.onClose { close += 1 }

var lastError: Error?
var lastError: (Error, URLResponse?)?
socket.onError(callback: { (error) in lastError = error })

var lastMessage: Message?
Expand All @@ -251,7 +251,7 @@ class SocketSpec: QuickSpec {
mockWebSocket.delegate?.onClose(code: 1000)
expect(close).to(equal(0))

mockWebSocket.delegate?.onError(error: TestError.stub)
mockWebSocket.delegate?.onError(error: TestError.stub, response: nil)
expect(lastError).to(beNil())

let data: [Any] = ["2", "6", "topic", "event", ["response": ["go": true], "status": "ok"]]
Expand Down Expand Up @@ -845,10 +845,10 @@ class SocketSpec: QuickSpec {
}

it("triggers onClose callbacks", closure: {
var lastError: Error?
var lastError: (Error, URLResponse?)?
socket.onError(callback: { (error) in lastError = error })

socket.onConnectionError(TestError.stub)
socket.onConnectionError(TestError.stub, response: nil)
expect(lastError).toNot(beNil())
})

Expand All @@ -863,7 +863,7 @@ class SocketSpec: QuickSpec {
channel.join()
expect(channel.state).to(equal(.joining))

socket.onConnectionError(TestError.stub)
socket.onConnectionError(TestError.stub, response: nil)
expect(errorCalled).to(beTrue())
})

Expand All @@ -877,7 +877,7 @@ class SocketSpec: QuickSpec {
channel.join().trigger("ok", payload: [:])
expect(channel.state).to(equal(.joined))

socket.onConnectionError(TestError.stub)
socket.onConnectionError(TestError.stub, response: nil)
expect(errorCalled).to(beTrue())
})

Expand All @@ -892,7 +892,7 @@ class SocketSpec: QuickSpec {
channel.leave()
expect(channel.state).to(equal(.closed))

socket.onConnectionError(TestError.stub)
socket.onConnectionError(TestError.stub, response: nil)
expect(errorCalled).to(beFalse())
})
}
Expand Down

0 comments on commit fc38c21

Please sign in to comment.