Allow changing config after init but before connect. Fixes #680
This commit is contained in:
parent
91eea96308
commit
321bb186df
@ -351,6 +351,25 @@ class SocketSideEffectTest: XCTestCase {
|
||||
waitForExpectations(timeout: 0.2)
|
||||
}
|
||||
|
||||
func testSettingConfigAfterInit() {
|
||||
socket.setTestStatus(.notConnected)
|
||||
socket.config.insert(.log(true))
|
||||
|
||||
XCTAssertTrue(DefaultSocketLogger.Logger.log, "It should set logging to true after creation")
|
||||
|
||||
socket.config = [.log(false), .nsp("/test")]
|
||||
|
||||
XCTAssertFalse(DefaultSocketLogger.Logger.log, "It should set logging to false after creation")
|
||||
XCTAssertEqual(socket.nsp, "/test", "It should set the namespace after creation")
|
||||
}
|
||||
|
||||
func testSettingConfigAfterInitWhenConnectedIgnoresChanges() {
|
||||
socket.config = [.log(true), .nsp("/test")]
|
||||
|
||||
XCTAssertFalse(DefaultSocketLogger.Logger.log, "It should set logging to false after creation")
|
||||
XCTAssertEqual(socket.nsp, "/", "It should set the namespace after creation")
|
||||
}
|
||||
|
||||
let data = "test".data(using: String.Encoding.utf8)!
|
||||
let data2 = "test2".data(using: String.Encoding.utf8)!
|
||||
private var socket: SocketIOClient!
|
||||
|
||||
@ -51,7 +51,24 @@ open class SocketIOClient : NSObject, SocketIOClientSpec, SocketEngineClient, So
|
||||
public var nsp = "/"
|
||||
|
||||
/// The configuration for this client.
|
||||
public var config: SocketIOClientConfiguration
|
||||
///
|
||||
/// **This cannot be set after calling one of the connect methods**.
|
||||
public var config: SocketIOClientConfiguration {
|
||||
didSet {
|
||||
guard status == .notConnected else {
|
||||
DefaultSocketLogger.Logger.error("Tried setting config after calling connect",
|
||||
type: SocketIOClient.logType)
|
||||
return
|
||||
}
|
||||
|
||||
if socketURL.absoluteString.hasPrefix("https://") {
|
||||
config.insert(.secure(true))
|
||||
}
|
||||
|
||||
config.insert(.path("/socket.io/"), replacing: false)
|
||||
setConfigs()
|
||||
}
|
||||
}
|
||||
|
||||
/// If `true`, this client will try and reconnect on any disconnects.
|
||||
@objc
|
||||
@ -130,32 +147,11 @@ open class SocketIOClient : NSObject, SocketIOClientSpec, SocketEngineClient, So
|
||||
self.config.insert(.secure(true))
|
||||
}
|
||||
|
||||
for option in config {
|
||||
switch option {
|
||||
case let .reconnects(reconnects):
|
||||
self.reconnects = reconnects
|
||||
case let .reconnectAttempts(attempts):
|
||||
reconnectAttempts = attempts
|
||||
case let .reconnectWait(wait):
|
||||
reconnectWait = abs(wait)
|
||||
case let .nsp(nsp):
|
||||
self.nsp = nsp
|
||||
case let .log(log):
|
||||
DefaultSocketLogger.Logger.log = log
|
||||
case let .logger(logger):
|
||||
DefaultSocketLogger.Logger = logger
|
||||
case let .handleQueue(queue):
|
||||
handleQueue = queue
|
||||
case let .forceNew(force):
|
||||
forceNew = force
|
||||
default:
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
self.config.insert(.path("/socket.io/"), replacing: false)
|
||||
|
||||
super.init()
|
||||
|
||||
setConfigs()
|
||||
}
|
||||
|
||||
/// Not so type safe way to create a SocketIOClient, meant for Objective-C compatiblity.
|
||||
@ -670,6 +666,31 @@ open class SocketIOClient : NSObject, SocketIOClientSpec, SocketEngineClient, So
|
||||
handleQueue.asyncAfter(deadline: DispatchTime.now() + Double(reconnectWait), execute: _tryReconnect)
|
||||
}
|
||||
|
||||
private func setConfigs() {
|
||||
for option in config {
|
||||
switch option {
|
||||
case let .reconnects(reconnects):
|
||||
self.reconnects = reconnects
|
||||
case let .reconnectAttempts(attempts):
|
||||
reconnectAttempts = attempts
|
||||
case let .reconnectWait(wait):
|
||||
reconnectWait = abs(wait)
|
||||
case let .nsp(nsp):
|
||||
self.nsp = nsp
|
||||
case let .log(log):
|
||||
DefaultSocketLogger.Logger.log = log
|
||||
case let .logger(logger):
|
||||
DefaultSocketLogger.Logger = logger
|
||||
case let .handleQueue(queue):
|
||||
handleQueue = queue
|
||||
case let .forceNew(force):
|
||||
forceNew = force
|
||||
default:
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Test properties
|
||||
|
||||
var testHandlers: [SocketEventHandler] {
|
||||
|
||||
@ -32,6 +32,9 @@ public protocol SocketIOClientSpec : class {
|
||||
/// A handler that will be called on any event.
|
||||
var anyHandler: ((SocketAnyEvent) -> ())? { get }
|
||||
|
||||
/// The configuration for this client.
|
||||
var config: SocketIOClientConfiguration { get set }
|
||||
|
||||
/// The queue that all interaction with the client must be on.
|
||||
var handleQueue: DispatchQueue { get set }
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user