Skip to content

Commit 4795205

Browse files
author
Ondrej Vesely
committed
1 parent af5ce97 commit 4795205

30 files changed

+242
-244
lines changed

Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import PackageDescription
55
let package = Package(
66
name: "SocketIO",
77
products: [
8-
.library(name: "SocketIO", targets: ["SocketIO"])
8+
.library(name: "SocketIO", targets: ["SocketIO"]),
99
],
1010
dependencies: [
1111
.package(url: "https://github.com/daltoniam/Starscream", .upToNextMinor(from: "4.0.0")),

Source/SocketIO/Ack/SocketAckEmitter.swift

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import Foundation
2828
/// A class that represents a waiting ack call.
2929
///
3030
/// **NOTE**: You should not store this beyond the life of the event handler.
31-
public final class SocketAckEmitter : NSObject {
31+
public final class SocketAckEmitter: NSObject {
3232
private unowned let socket: SocketIOClient
3333
private let ackNum: Int
3434

@@ -74,7 +74,7 @@ public final class SocketAckEmitter : NSObject {
7474
guard ackNum != -1 else { return }
7575

7676
do {
77-
socket.emitAck(ackNum, with: try items.map({ try $0.socketRepresentation() }))
77+
socket.emitAck(ackNum, with: try items.map { try $0.socketRepresentation() })
7878
} catch {
7979
socket.handleClientEvent(.error, data: [ackNum, items, error])
8080
}
@@ -89,7 +89,6 @@ public final class SocketAckEmitter : NSObject {
8989

9090
socket.emitAck(ackNum, with: items)
9191
}
92-
9392
}
9493

9594
/// A class that represents an emit that will request an ack that has not yet been sent.
@@ -101,7 +100,7 @@ public final class SocketAckEmitter : NSObject {
101100
/// ...
102101
/// }
103102
/// ```
104-
public final class OnAckCallback : NSObject {
103+
public final class OnAckCallback: NSObject {
105104
private let ackNumber: Int
106105
private let binary: Bool
107106
private let items: [Any]
@@ -129,18 +128,17 @@ public final class OnAckCallback : NSObject {
129128
/// To check for timeout, use `SocketAckStatus`'s `noAck` case.
130129
@objc
131130
public func timingOut(after seconds: Double, callback: @escaping AckCallback) {
132-
guard let socket = self.socket, ackNumber != -1 else { return }
131+
guard let socket = socket, ackNumber != -1 else { return }
133132

134133
socket.ackHandlers.addAck(ackNumber, callback: callback)
135134
socket.emit(items, ack: ackNumber, binary: binary)
136135

137136
guard seconds != 0 else { return }
138137

139-
socket.manager?.handleQueue.asyncAfter(deadline: DispatchTime.now() + seconds) {[weak socket] in
138+
socket.manager?.handleQueue.asyncAfter(deadline: DispatchTime.now() + seconds) { [weak socket] in
140139
guard let socket = socket else { return }
141140

142141
socket.ackHandlers.timeoutAck(self.ackNumber)
143142
}
144143
}
145-
146144
}

Source/SocketIO/Ack/SocketAckManager.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import Dispatch
2626
import Foundation
2727

2828
/// The status of an ack.
29-
public enum SocketAckStatus : String {
29+
public enum SocketAckStatus: String {
3030
// MARK: Cases
3131

3232
/// The ack timed out.
@@ -43,7 +43,7 @@ public enum SocketAckStatus : String {
4343
}
4444
}
4545

46-
private struct SocketAck : Hashable {
46+
private struct SocketAck: Hashable {
4747
let ack: Int
4848
var callback: AckCallback!
4949

@@ -60,11 +60,11 @@ private struct SocketAck : Hashable {
6060
ack.hash(into: &hasher)
6161
}
6262

63-
fileprivate static func <(lhs: SocketAck, rhs: SocketAck) -> Bool {
63+
fileprivate static func < (lhs: SocketAck, rhs: SocketAck) -> Bool {
6464
return lhs.ack < rhs.ack
6565
}
6666

67-
fileprivate static func ==(lhs: SocketAck, rhs: SocketAck) -> Bool {
67+
fileprivate static func == (lhs: SocketAck, rhs: SocketAck) -> Bool {
6868
return lhs.ack == rhs.ack
6969
}
7070
}
@@ -83,6 +83,6 @@ class SocketAckManager {
8383

8484
/// Should be called on handle queue
8585
func timeoutAck(_ ack: Int) {
86-
acks.remove(SocketAck(ack: ack))?.callback?([SocketAckStatus.noAck.rawValue])
86+
acks.remove(SocketAck(ack: ack))?.callback?([SocketAckStatus.noAck.rawValue])
8787
}
8888
}

Source/SocketIO/Client/SocketAnyEvent.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import Foundation
2626

2727
/// Represents some event that was received.
28-
public final class SocketAnyEvent : NSObject {
28+
public final class SocketAnyEvent: NSObject {
2929
// MARK: Properties
3030

3131
/// The event name.

Source/SocketIO/Client/SocketIOClient.swift

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ open class SocketIOClient: NSObject, SocketIOClientSpec {
4949
public let nsp: String
5050

5151
/// A handler that will be called on any event.
52-
public private(set) var anyHandler: ((SocketAnyEvent) -> ())?
52+
public private(set) var anyHandler: ((SocketAnyEvent) -> Void)?
5353

5454
/// The array of handlers for this socket.
5555
public private(set) var handlers = [SocketEventHandler]()
@@ -122,10 +122,10 @@ open class SocketIOClient: NSObject, SocketIOClientSpec {
122122
/// - parameter timeoutAfter: The number of seconds after which if we are not connected we assume the connection
123123
/// has failed. Pass 0 to never timeout.
124124
/// - parameter handler: The handler to call when the client fails to connect.
125-
open func connect(withPayload payload: [String: Any]? = nil, timeoutAfter: Double, withHandler handler: (() -> ())?) {
125+
open func connect(withPayload payload: [String: Any]? = nil, timeoutAfter: Double, withHandler handler: (() -> Void)?) {
126126
assert(timeoutAfter >= 0, "Invalid timeout: \(timeoutAfter)")
127127

128-
guard let manager = self.manager, status != .connected else {
128+
guard let manager = manager, status != .connected else {
129129
DefaultSocketLogger.Logger.log("Tried connecting on an already connected socket", type: logType)
130130
return
131131
}
@@ -148,7 +148,7 @@ open class SocketIOClient: NSObject, SocketIOClientSpec {
148148

149149
guard timeoutAfter != 0 else { return }
150150

151-
manager.handleQueue.asyncAfter(deadline: DispatchTime.now() + timeoutAfter) {[weak self] in
151+
manager.handleQueue.asyncAfter(deadline: DispatchTime.now() + timeoutAfter) { [weak self] in
152152
guard let this = self, this.status == .connecting || this.status == .notConnected else { return }
153153

154154
this.status = .disconnected
@@ -158,7 +158,7 @@ open class SocketIOClient: NSObject, SocketIOClientSpec {
158158
}
159159
}
160160

161-
func createOnAck(_ items: [Any], binary: Bool = true) -> OnAckCallback {
161+
func createOnAck(_ items: [Any], binary _: Bool = true) -> OnAckCallback {
162162
currentAck += 1
163163

164164
return OnAckCallback(ackNumber: currentAck, items: items, socket: self)
@@ -211,10 +211,10 @@ open class SocketIOClient: NSObject, SocketIOClientSpec {
211211
/// - parameter event: The event to send.
212212
/// - parameter items: The items to send with this event. May be left out.
213213
/// - parameter completion: Callback called on transport write completion.
214-
open func emit(_ event: String, _ items: SocketData..., completion: (() -> ())? = nil) {
214+
open func emit(_ event: String, _ items: SocketData..., completion: (() -> Void)? = nil) {
215215
emit(event, with: items, completion: completion)
216216
}
217-
217+
218218
/// Send an event to the server, with optional data items and optional write completion handler.
219219
///
220220
/// If an error occurs trying to transform `items` into their socket representation, a `SocketClientEvent.error`
@@ -223,10 +223,9 @@ open class SocketIOClient: NSObject, SocketIOClientSpec {
223223
/// - parameter event: The event to send.
224224
/// - parameter items: The items to send with this event. May be left out.
225225
/// - parameter completion: Callback called on transport write completion.
226-
open func emit(_ event: String, with items: [SocketData], completion: (() -> ())?) {
227-
226+
open func emit(_ event: String, with items: [SocketData], completion: (() -> Void)?) {
228227
do {
229-
emit([event] + (try items.map({ try $0.socketRepresentation() })), completion: completion)
228+
emit([event] + (try items.map { try $0.socketRepresentation() }), completion: completion)
230229
} catch {
231230
DefaultSocketLogger.Logger.error("Error creating socketRepresentation for emit: \(event), \(items)",
232231
type: logType)
@@ -257,7 +256,7 @@ open class SocketIOClient: NSObject, SocketIOClientSpec {
257256
open func emitWithAck(_ event: String, _ items: SocketData...) -> OnAckCallback {
258257
emitWithAck(event, with: items)
259258
}
260-
259+
261260
/// Sends a message to the server, requesting an ack.
262261
///
263262
/// **NOTE**: It is up to the server send an ack back, just calling this method does not mean the server will ack.
@@ -278,9 +277,8 @@ open class SocketIOClient: NSObject, SocketIOClientSpec {
278277
/// - parameter items: The items to send with this event. May be left out.
279278
/// - returns: An `OnAckCallback`. You must call the `timingOut(after:)` method before the event will be sent.
280279
open func emitWithAck(_ event: String, with items: [SocketData]) -> OnAckCallback {
281-
282280
do {
283-
return createOnAck([event] + (try items.map({ try $0.socketRepresentation() })))
281+
return createOnAck([event] + (try items.map { try $0.socketRepresentation() }))
284282
} catch {
285283
DefaultSocketLogger.Logger.error("Error creating socketRepresentation for emit: \(event), \(items)",
286284
type: logType)
@@ -295,10 +293,10 @@ open class SocketIOClient: NSObject, SocketIOClientSpec {
295293
ack: Int? = nil,
296294
binary: Bool = true,
297295
isAck: Bool = false,
298-
completion: (() -> ())? = nil
299-
) {
296+
completion: (() -> Void)? = nil)
297+
{
300298
// wrap the completion handler so it always runs async via handlerQueue
301-
let wrappedCompletion: (() -> ())? = (completion == nil) ? nil : {[weak self] in
299+
let wrappedCompletion: (() -> Void)? = (completion == nil) ? nil : { [weak self] in
302300
guard let this = self else { return }
303301
this.manager?.handleQueue.async {
304302
completion!()
@@ -421,7 +419,7 @@ open class SocketIOClient: NSObject, SocketIOClientSpec {
421419
open func off(_ event: String) {
422420
DefaultSocketLogger.Logger.log("Removing handler for event: \(event)", type: logType)
423421

424-
handlers = handlers.filter({ $0.event != event })
422+
handlers = handlers.filter { $0.event != event }
425423
}
426424

427425
/// Removes a handler with the specified UUID gotten from an `on` or `once`
@@ -432,7 +430,7 @@ open class SocketIOClient: NSObject, SocketIOClientSpec {
432430
open func off(id: UUID) {
433431
DefaultSocketLogger.Logger.log("Removing handler with id: \(id)", type: logType)
434432

435-
handlers = handlers.filter({ $0.id != id })
433+
handlers = handlers.filter { $0.id != id }
436434
}
437435

438436
/// Adds a handler for an event.
@@ -489,7 +487,7 @@ open class SocketIOClient: NSObject, SocketIOClientSpec {
489487

490488
let id = UUID()
491489

492-
let handler = SocketEventHandler(event: event, id: id) {[weak self] data, ack in
490+
let handler = SocketEventHandler(event: event, id: id) { [weak self] data, ack in
493491
guard let this = self else { return }
494492
this.off(id: id)
495493
callback(data, ack)
@@ -503,13 +501,13 @@ open class SocketIOClient: NSObject, SocketIOClientSpec {
503501
/// Adds a handler that will be called on every event.
504502
///
505503
/// - parameter handler: The callback that will execute whenever an event is received.
506-
open func onAny(_ handler: @escaping (SocketAnyEvent) -> ()) {
504+
open func onAny(_ handler: @escaping (SocketAnyEvent) -> Void) {
507505
anyHandler = handler
508506
}
509507

510508
/// Tries to reconnect to the server.
511509
@available(*, unavailable, message: "Call the manager's reconnect method")
512-
open func reconnect() { }
510+
open func reconnect() {}
513511

514512
/// Removes all handlers.
515513
///

Source/SocketIO/Client/SocketIOClientConfiguration.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
// THE SOFTWARE.
2424

2525
/// An array-like type that holds `SocketIOClientOption`s
26-
public struct SocketIOClientConfiguration : ExpressibleByArrayLiteral, Collection, MutableCollection {
26+
public struct SocketIOClientConfiguration: ExpressibleByArrayLiteral, Collection, MutableCollection {
2727
// MARK: Typealiases
2828

2929
/// Type of element stored.
@@ -36,7 +36,7 @@ public struct SocketIOClientConfiguration : ExpressibleByArrayLiteral, Collectio
3636
public typealias Iterator = Array<SocketIOClientOption>.Iterator
3737

3838
/// SubSequence type.
39-
public typealias SubSequence = Array<SocketIOClientOption>.SubSequence
39+
public typealias SubSequence = Array<SocketIOClientOption>.SubSequence
4040

4141
// MARK: Properties
4242

@@ -115,7 +115,7 @@ public struct SocketIOClientConfiguration : ExpressibleByArrayLiteral, Collectio
115115
/// - parameter element: The element to insert.
116116
/// - parameter replacing: Whether to replace any occurrences of element to the new item. Default is `true`.
117117
public mutating func insert(_ element: Element, replacing replace: Bool = true) {
118-
for i in 0..<backingArray.count where backingArray[i] == element {
118+
for i in 0 ..< backingArray.count where backingArray[i] == element {
119119
guard replace else { return }
120120

121121
backingArray[i] = element

Source/SocketIO/Client/SocketIOClientOption.swift

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@ public enum SocketIOVersion: Int {
3434
case three = 3
3535
}
3636

37-
protocol ClientOption : CustomStringConvertible, Equatable {
37+
protocol ClientOption: CustomStringConvertible, Equatable {
3838
func getSocketIOOptionValue() -> Any
3939
}
4040

4141
/// The options for a client.
42-
public enum SocketIOClientOption : ClientOption {
42+
public enum SocketIOClientOption: ClientOption {
4343
/// If given, the WebSocket transport will attempt to use compression.
4444
case compress
4545

@@ -213,7 +213,7 @@ public enum SocketIOClientOption : ClientOption {
213213
value = delegate
214214
case let .enableSOCKSProxy(enable):
215215
value = enable
216-
case let.version(versionNum):
216+
case let .version(versionNum):
217217
value = versionNum
218218
}
219219

@@ -227,8 +227,7 @@ public enum SocketIOClientOption : ClientOption {
227227
/// - parameter lhs: Left operand to compare.
228228
/// - parameter rhs: Right operand to compare.
229229
/// - returns: `true` if the two are the same option.
230-
public static func ==(lhs: SocketIOClientOption, rhs: SocketIOClientOption) -> Bool {
230+
public static func == (lhs: SocketIOClientOption, rhs: SocketIOClientOption) -> Bool {
231231
return lhs.description == rhs.description
232232
}
233-
234233
}

Source/SocketIO/Client/SocketIOClientSpec.swift

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ import Dispatch
2626
import Foundation
2727

2828
/// Defines the interface for a SocketIOClient.
29-
public protocol SocketIOClientSpec : AnyObject {
29+
public protocol SocketIOClientSpec: AnyObject {
3030
// MARK: Properties
3131

3232
/// A handler that will be called on any event.
33-
var anyHandler: ((SocketAnyEvent) -> ())? { get }
33+
var anyHandler: ((SocketAnyEvent) -> Void)? { get }
3434

3535
/// The array of handlers for this socket.
3636
var handlers: [SocketEventHandler] { get }
@@ -77,7 +77,7 @@ public protocol SocketIOClientSpec : AnyObject {
7777
/// - parameter timeoutAfter: The number of seconds after which if we are not connected we assume the connection
7878
/// has failed. Pass 0 to never timeout.
7979
/// - parameter handler: The handler to call when the client fails to connect.
80-
func connect(withPayload payload: [String: Any]?, timeoutAfter: Double, withHandler handler: (() -> ())?)
80+
func connect(withPayload payload: [String: Any]?, timeoutAfter: Double, withHandler handler: (() -> Void)?)
8181

8282
/// Called when the client connects to a namespace. If the client was created with a namespace upfront,
8383
/// then this is only called when the client connects to that namespace.
@@ -106,8 +106,8 @@ public protocol SocketIOClientSpec : AnyObject {
106106
/// - parameter event: The event to send.
107107
/// - parameter items: The items to send with this event. May be left out.
108108
/// - parameter completion: Callback called on transport write completion.
109-
func emit(_ event: String, _ items: SocketData..., completion: (() -> ())?)
110-
109+
func emit(_ event: String, _ items: SocketData..., completion: (() -> Void)?)
110+
111111
/// Send an event to the server, with optional data items and optional write completion handler.
112112
///
113113
/// If an error occurs trying to transform `items` into their socket representation, a `SocketClientEvent.error`
@@ -116,7 +116,7 @@ public protocol SocketIOClientSpec : AnyObject {
116116
/// - parameter event: The event to send.
117117
/// - parameter items: The items to send with this event. May be left out.
118118
/// - parameter completion: Callback called on transport write completion.
119-
func emit(_ event: String, with items: [SocketData], completion: (() -> ())?)
119+
func emit(_ event: String, with items: [SocketData], completion: (() -> Void)?)
120120

121121
/// Call when you wish to tell the server that you've received the event for `ack`.
122122
///
@@ -144,7 +144,7 @@ public protocol SocketIOClientSpec : AnyObject {
144144
/// - parameter items: The items to send with this event. May be left out.
145145
/// - returns: An `OnAckCallback`. You must call the `timingOut(after:)` method before the event will be sent.
146146
func emitWithAck(_ event: String, _ items: SocketData...) -> OnAckCallback
147-
147+
148148
/// Sends a message to the server, requesting an ack.
149149
///
150150
/// **NOTE**: It is up to the server send an ack back, just calling this method does not mean the server will ack.
@@ -260,7 +260,7 @@ public protocol SocketIOClientSpec : AnyObject {
260260
/// Adds a handler that will be called on every event.
261261
///
262262
/// - parameter handler: The callback that will execute whenever an event is received.
263-
func onAny(_ handler: @escaping (SocketAnyEvent) -> ())
263+
func onAny(_ handler: @escaping (SocketAnyEvent) -> Void)
264264

265265
/// Removes all handlers.
266266
///
@@ -284,7 +284,7 @@ public extension SocketIOClientSpec {
284284
}
285285

286286
/// The set of events that are generated by the client.
287-
public enum SocketClientEvent : String {
287+
public enum SocketClientEvent: String {
288288
// MARK: Cases
289289

290290
/// Emitted when the client connects. This is also called on a successful reconnection. A connect event gets one

0 commit comments

Comments
 (0)