Skip to content

Commit a87214c

Browse files
committed
refactor client options
1 parent 8c405ce commit a87214c

File tree

3 files changed

+28
-27
lines changed

3 files changed

+28
-27
lines changed

SocketIOClientSwift/SocketEngine.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public final class SocketEngine: NSObject, SocketEngineSpec, WebSocketDelegate {
112112

113113
public convenience init(client: SocketEngineClient, url: String, options: NSDictionary?) {
114114
self.init(client: client, url: url,
115-
options: SocketIOClientOption.NSDictionaryToSocketOptionsSet(options ?? [:]))
115+
options: Set<SocketIOClientOption>.NSDictionaryToSocketOptionsSet(options ?? [:]))
116116
}
117117

118118
deinit {
@@ -210,7 +210,6 @@ public final class SocketEngine: NSObject, SocketEngineSpec, WebSocketDelegate {
210210
let wsUrl = urlWebSocket + (sid == "" ? "" : "&sid=\(sid)")
211211

212212
ws = WebSocket(url: NSURL(string: wsUrl)!)
213-
214213
if cookies != nil {
215214
let headers = NSHTTPCookie.requestHeaderFieldsWithCookies(cookies!)
216215
for (key, value) in headers {

SocketIOClientSwift/SocketIOClient.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public final class SocketIOClient: NSObject, SocketEngineClient {
105105
*/
106106
public convenience init(socketURL: String, options: NSDictionary?) {
107107
self.init(socketURL: socketURL,
108-
options: SocketIOClientOption.NSDictionaryToSocketOptionsSet(options ?? [:]))
108+
options: Set<SocketIOClientOption>.NSDictionaryToSocketOptionsSet(options ?? [:]))
109109
}
110110

111111
deinit {

SocketIOClientSwift/SocketIOClientOption.swift

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,11 @@
2424

2525
import Foundation
2626

27-
protocol ClientOptions {}
27+
protocol ClientOption: CustomStringConvertible, Hashable {
28+
func getSocketIOOptionValue() -> AnyObject?
29+
}
2830

29-
public enum SocketIOClientOption: CustomStringConvertible, Hashable, ClientOptions {
31+
public enum SocketIOClientOption: ClientOption {
3032
case ConnectParams([String: AnyObject])
3133
case Cookies([NSHTTPCookie])
3234
case ExtraHeaders([String: String])
@@ -98,44 +100,44 @@ public enum SocketIOClientOption: CustomStringConvertible, Hashable, ClientOptio
98100
func getSocketIOOptionValue() -> AnyObject? {
99101
return Mirror(reflecting: self).children.first?.value as? AnyObject
100102
}
103+
}
104+
105+
public func ==(lhs: SocketIOClientOption, rhs: SocketIOClientOption) -> Bool {
106+
return lhs.description == rhs.description
107+
}
108+
109+
extension Set where Element: ClientOption {
110+
mutating func insertIgnore(element: Element) {
111+
let (insertType, _) = Mirror(reflecting: element).children.first!
112+
for item in self {
113+
let (name, _) = Mirror(reflecting: item).children.first!
114+
if insertType == name {
115+
return
116+
}
117+
}
118+
119+
self.insert(element)
120+
}
101121

102122
static func NSDictionaryToSocketOptionsSet(dict: NSDictionary) -> Set<SocketIOClientOption> {
103123
var options = Set<SocketIOClientOption>()
104-
124+
105125
for (rawKey, value) in dict {
106-
if let key = rawKey as? String, opt = keyValueToSocketIOClientOption(key, value: value) {
126+
if let key = rawKey as? String, opt = SocketIOClientOption.keyValueToSocketIOClientOption(key, value: value) {
107127
options.insert(opt)
108128
}
109129
}
110130

111131
return options
112132
}
113133

114-
static func SocketOptionsSetToNSDictionary(set: Set<SocketIOClientOption>) -> NSDictionary {
134+
func SocketOptionsSetToNSDictionary() -> NSDictionary {
115135
let options = NSMutableDictionary()
116136

117-
for option in set {
137+
for option in self {
118138
options[option.description] = option.getSocketIOOptionValue()
119139
}
120140

121141
return options
122142
}
123143
}
124-
125-
public func ==(lhs: SocketIOClientOption, rhs: SocketIOClientOption) -> Bool {
126-
return lhs.description == rhs.description
127-
}
128-
129-
extension Set where Element: ClientOptions {
130-
mutating func insertIgnore(element: Element) {
131-
let (insertType, _) = Mirror(reflecting: element).children.first!
132-
for item in self {
133-
let (name, _) = Mirror(reflecting: item).children.first!
134-
if insertType == name {
135-
return
136-
}
137-
}
138-
139-
self.insert(element)
140-
}
141-
}

0 commit comments

Comments
 (0)