mess around with a more type safe way to set options

This commit is contained in:
Erik 2015-10-17 14:49:41 -04:00
parent 7f443faf92
commit 40ac53844d
4 changed files with 92 additions and 11 deletions

View File

@ -37,6 +37,10 @@
5764DFA01B51F254004FF46E /* SwiftRegex.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5764DF871B51F254004FF46E /* SwiftRegex.swift */; };
5764DFA11B51F254004FF46E /* WebSocket.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5764DF881B51F254004FF46E /* WebSocket.swift */; };
5764DFA21B51F254004FF46E /* WebSocket.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5764DF881B51F254004FF46E /* WebSocket.swift */; };
740E10441BD2C4680064FC4A /* SocketIOClientOptions .swift in Sources */ = {isa = PBXBuildFile; fileRef = 740E10431BD2C4680064FC4A /* SocketIOClientOptions .swift */; settings = {ASSET_TAGS = (); }; };
740E10451BD2C4680064FC4A /* SocketIOClientOptions .swift in Sources */ = {isa = PBXBuildFile; fileRef = 740E10431BD2C4680064FC4A /* SocketIOClientOptions .swift */; settings = {ASSET_TAGS = (); }; };
740E10461BD2C4680064FC4A /* SocketIOClientOptions .swift in Sources */ = {isa = PBXBuildFile; fileRef = 740E10431BD2C4680064FC4A /* SocketIOClientOptions .swift */; settings = {ASSET_TAGS = (); }; };
740E10471BD2C4680064FC4A /* SocketIOClientOptions .swift in Sources */ = {isa = PBXBuildFile; fileRef = 740E10431BD2C4680064FC4A /* SocketIOClientOptions .swift */; settings = {ASSET_TAGS = (); }; };
741F39EE1BD025D80026C9CC /* SocketEngineTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 741F39ED1BD025D80026C9CC /* SocketEngineTest.swift */; settings = {ASSET_TAGS = (); }; };
741F39EF1BD025D80026C9CC /* SocketEngineTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 741F39ED1BD025D80026C9CC /* SocketEngineTest.swift */; settings = {ASSET_TAGS = (); }; };
745895381BB59A0A0050ACC8 /* SocketAckManagerTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 94A20D601B99E22F00BF9E44 /* SocketAckManagerTest.swift */; };
@ -138,6 +142,7 @@
5764DF861B51F254004FF46E /* SocketTypes.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = SocketTypes.swift; path = SocketIOClientSwift/SocketTypes.swift; sourceTree = "<group>"; };
5764DF871B51F254004FF46E /* SwiftRegex.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = SwiftRegex.swift; path = SocketIOClientSwift/SwiftRegex.swift; sourceTree = "<group>"; };
5764DF881B51F254004FF46E /* WebSocket.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = WebSocket.swift; path = SocketIOClientSwift/WebSocket.swift; sourceTree = "<group>"; };
740E10431BD2C4680064FC4A /* SocketIOClientOptions .swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = "SocketIOClientOptions .swift"; path = "SocketIOClientSwift/SocketIOClientOptions .swift"; sourceTree = "<group>"; };
741F39ED1BD025D80026C9CC /* SocketEngineTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SocketEngineTest.swift; sourceTree = "<group>"; };
7472C65B1BCAB53E003CA70D /* SocketNamespacePacketTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SocketNamespacePacketTest.swift; sourceTree = "<group>"; };
7472C65E1BCAC46E003CA70D /* SocketSideEffectTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SocketSideEffectTest.swift; sourceTree = "<group>"; };
@ -293,6 +298,7 @@
5764DF801B51F254004FF46E /* SocketEventHandler.swift */,
5764DF811B51F254004FF46E /* SocketFixUTF8.swift */,
5764DF821B51F254004FF46E /* SocketIOClient.swift */,
740E10431BD2C4680064FC4A /* SocketIOClientOptions .swift */,
74781D591B7E83930042CACA /* SocketIOClientStatus.swift */,
5764DF831B51F254004FF46E /* SocketLogger.swift */,
5764DF841B51F254004FF46E /* SocketPacket.swift */,
@ -492,6 +498,7 @@
74781D5A1B7E83930042CACA /* SocketIOClientStatus.swift in Sources */,
5764DFA11B51F254004FF46E /* WebSocket.swift in Sources */,
5764DF991B51F254004FF46E /* SocketPacket.swift in Sources */,
740E10441BD2C4680064FC4A /* SocketIOClientOptions .swift in Sources */,
74F124E31BC5697B002966F4 /* SocketEngineSpec.swift in Sources */,
5764DF891B51F254004FF46E /* SocketAckManager.swift in Sources */,
74F124E81BC56BFC002966F4 /* SocketEnginePacketType.swift in Sources */,
@ -523,6 +530,7 @@
74781D5B1B7E83930042CACA /* SocketIOClientStatus.swift in Sources */,
945B653B1B5FCEEA0081E995 /* SocketIOClient.swift in Sources */,
74F124F01BC574CF002966F4 /* SocketBasicPacketTest.swift in Sources */,
740E10451BD2C4680064FC4A /* SocketIOClientOptions .swift in Sources */,
949FAE8D1B9B94E600073BE9 /* SocketParserTest.swift in Sources */,
7472C65C1BCAB53E003CA70D /* SocketNamespacePacketTest.swift in Sources */,
945B65411B5FCEEA0081E995 /* WebSocket.swift in Sources */,
@ -548,6 +556,7 @@
74781D5C1B7E83930042CACA /* SocketIOClientStatus.swift in Sources */,
5764DFA21B51F254004FF46E /* WebSocket.swift in Sources */,
5764DF9A1B51F254004FF46E /* SocketPacket.swift in Sources */,
740E10461BD2C4680064FC4A /* SocketIOClientOptions .swift in Sources */,
74F124E51BC5697B002966F4 /* SocketEngineSpec.swift in Sources */,
5764DF8A1B51F254004FF46E /* SocketAckManager.swift in Sources */,
74F124EA1BC56BFC002966F4 /* SocketEnginePacketType.swift in Sources */,
@ -579,6 +588,7 @@
745895381BB59A0A0050ACC8 /* SocketAckManagerTest.swift in Sources */,
7458953D1BB59A0A0050ACC8 /* SocketParserTest.swift in Sources */,
74F124F11BC574CF002966F4 /* SocketBasicPacketTest.swift in Sources */,
740E10471BD2C4680064FC4A /* SocketIOClientOptions .swift in Sources */,
74F124EB1BC56BFC002966F4 /* SocketEnginePacketType.swift in Sources */,
7472C65D1BCAB53E003CA70D /* SocketNamespacePacketTest.swift in Sources */,
749A7F901BA9D42D00782993 /* SocketAckEmitter.swift in Sources */,

View File

@ -32,7 +32,7 @@ public final class SocketIOClient: NSObject, SocketEngineClient {
public private(set) var status = SocketIOClientStatus.NotConnected
public var nsp = "/"
public var opts: [String: AnyObject]?
public var opts: SocketOptionsDictionary?
public var reconnects = true
public var reconnectWait = 10
public var sid: String? {
@ -57,7 +57,7 @@ public final class SocketIOClient: NSObject, SocketEngineClient {
/**
Create a new SocketIOClient. opts can be omitted
*/
public init(var socketURL: String, opts: [String: AnyObject]? = nil) {
public init(var socketURL: String, opts: SocketOptionsDictionary? = nil) {
if socketURL["https://"].matches().count != 0 {
self.secure = true
}
@ -68,37 +68,37 @@ public final class SocketIOClient: NSObject, SocketEngineClient {
self.socketURL = socketURL
self.opts = opts
if let connectParams = opts?["connectParams"] as? [String: AnyObject] {
if let connectParams = opts?[.ConnectParams] as? [String: AnyObject] {
self.connectParams = connectParams
}
if let logger = opts?["logger"] as? SocketLogger {
if let logger = opts?[.Logger] as? SocketLogger {
Logger = logger
}
if let log = opts?["log"] as? Bool {
if let log = opts?[.Log] as? Bool {
Logger.log = log
}
if let nsp = opts?["nsp"] as? String {
if let nsp = opts?[.Nsp] as? String {
self.nsp = nsp
}
if let reconnects = opts?["reconnects"] as? Bool {
if let reconnects = opts?[.Reconnects] as? Bool {
self.reconnects = reconnects
}
if let reconnectAttempts = opts?["reconnectAttempts"] as? Int {
if let reconnectAttempts = opts?[.ReconnectAttempts] as? Int {
self.reconnectAttempts = reconnectAttempts
} else {
self.reconnectAttempts = -1
}
if let reconnectWait = opts?["reconnectWait"] as? Int {
if let reconnectWait = opts?[.ReconnectWait] as? Int {
self.reconnectWait = abs(reconnectWait)
}
if let handleQueue = opts?["handleQueue"] as? dispatch_queue_t {
if let handleQueue = opts?[.HandleQueue] as? dispatch_queue_t {
self.handleQueue = handleQueue
} else {
self.handleQueue = dispatch_get_main_queue()
@ -107,6 +107,11 @@ public final class SocketIOClient: NSObject, SocketEngineClient {
super.init()
}
public convenience init(socketURL: String, opts: NSDictionary?) {
self.init(socketURL: socketURL,
opts: SocketIOClientOptions.NSDictionaryToSocketOptionsDictionary(opts ?? [:]))
}
deinit {
Logger.log("Client is being deinit", type: logType)
}
@ -114,7 +119,8 @@ public final class SocketIOClient: NSObject, SocketEngineClient {
private func addEngine() -> SocketEngine {
Logger.log("Adding engine", type: logType)
let newEngine = SocketEngine(client: self, opts: opts)
let newEngine = SocketEngine(client: self, opts:
SocketIOClientOptions.SocketOptionsDictionaryToNSDictionary(opts ?? [:]))
engine = newEngine
return newEngine

View File

@ -0,0 +1,64 @@
//
// SocketIOClientOptions .swift
// Socket.IO-Client-Swift
//
// Created by Erik Little on 10/17/15.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
import Foundation
public enum SocketIOClientOptions: String {
case ConnectParams = "connectParams"
case Reconnects = "reconnects"
case ReconnectAttempts = "reconnectAttempts"
case ReconnectWait = "reconnectWait"
case ForcePolling = "forcePolling"
case ForceWebsockets = "forceWebsockets"
case Nsp = "nsp"
case Cookies = "cookies"
case Log = "log"
case Logger = "logger"
case SessionDelegate = "sessionDelegate"
case Path = "path"
case ExtraHeaders = "extraHeaders"
case HandleQueue = "handleQueue"
static func NSDictionaryToSocketOptionsDictionary(dict: NSDictionary) -> SocketOptionsDictionary {
var options = [SocketIOClientOptions: AnyObject]()
for (rawKey, value) in dict {
if let key = rawKey as? String, opt = SocketIOClientOptions(rawValue: key) {
options[opt] = value
}
}
return options
}
static func SocketOptionsDictionaryToNSDictionary(dict: SocketOptionsDictionary) -> NSDictionary {
let options = NSMutableDictionary()
for (key, value) in dict {
options[key.rawValue] = value
}
return options
}
}

View File

@ -27,6 +27,7 @@ import Foundation
public typealias AckCallback = ([AnyObject]) -> Void
public typealias NormalCallback = ([AnyObject], SocketAckEmitter?) -> Void
public typealias OnAckCallback = (timeoutAfter: UInt64, callback: AckCallback) -> Void
public typealias SocketOptionsDictionary = [SocketIOClientOptions: AnyObject]
enum Either<E, V> {
case Left(E)