Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don’t create empty closures when we don’t need them #1119

Merged
merged 3 commits into from
Nov 7, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions Source/SocketIO/Client/SocketIOClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ open class SocketIOClient : NSObject, SocketIOClientSpec {
/// - parameter event: The event to send.
/// - parameter items: The items to send with this event. May be left out.
/// - parameter completion: Callback called on transport write completion.
open func emit(_ event: String, _ items: SocketData..., completion: @escaping () -> ()) {
open func emit(_ event: String, _ items: SocketData..., completion: (() -> ())? = nil) {
vonox7 marked this conversation as resolved.
Show resolved Hide resolved
do {
try emit(event, with: items.map({ try $0.socketRepresentation() }), completion: completion)
} catch {
Expand All @@ -256,7 +256,7 @@ open class SocketIOClient : NSObject, SocketIOClientSpec {
/// - parameter items: The items to send with this event. Send an empty array to send no data.
/// - parameter completion: Callback called on transport write completion.
@objc
open func emit(_ event: String, with items: [Any], completion: @escaping () -> ()) {
open func emit(_ event: String, with items: [Any], completion: (() -> ())? = nil) {
emit([event] + items, completion: completion)
}

Expand Down Expand Up @@ -317,18 +317,18 @@ open class SocketIOClient : NSObject, SocketIOClientSpec {
ack: Int? = nil,
binary: Bool = true,
isAck: Bool = false,
completion: @escaping () -> () = {}
completion: (() -> ())? = nil
) {
// wrap the completion handler so it always runs async via handlerQueue
let wrappedCompletion = {[weak self] in
let wrappedCompletion: (() -> ())? = (completion == nil) ? nil : {[weak self] in
guard let this = self else { return }
this.manager?.handleQueue.async {
completion()
completion!()
}
}

guard status == .connected else {
wrappedCompletion()
wrappedCompletion?()
handleClientEvent(.error, data: ["Tried emitting when not connected"])
return
}
Expand Down
2 changes: 1 addition & 1 deletion Source/SocketIO/Client/SocketIOClientSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public protocol SocketIOClientSpec : AnyObject {
/// - parameter event: The event to send.
/// - parameter items: The items to send with this event. May be left out.
/// - parameter completion: Callback called on transport write completion.
func emit(_ event: String, _ items: SocketData..., completion: @escaping () -> ())
func emit(_ event: String, _ items: SocketData..., completion: (() -> ())?)

/// Call when you wish to tell the server that you've received the event for `ack`.
///
Expand Down
14 changes: 7 additions & 7 deletions Source/SocketIO/Engine/SocketEngine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ open class SocketEngine : NSObject, URLSessionDelegate, SocketEnginePollable, So
if polling {
disconnectPolling(reason: reason)
} else {
sendWebSocketMessage("", withType: .close, withData: [], completion: {})
sendWebSocketMessage("", withType: .close, withData: [], completion: nil)
closeOutEngine(reason: reason)
}
}
Expand All @@ -372,7 +372,7 @@ open class SocketEngine : NSObject, URLSessionDelegate, SocketEnginePollable, So

DefaultSocketLogger.Logger.log("Switching to WebSockets", type: SocketEngine.logType)

sendWebSocketMessage("", withType: .upgrade, withData: [], completion: {})
sendWebSocketMessage("", withType: .upgrade, withData: [], completion: nil)
polling = false
fastUpgrade = false
probing = false
Expand All @@ -390,7 +390,7 @@ open class SocketEngine : NSObject, URLSessionDelegate, SocketEnginePollable, So
DefaultSocketLogger.Logger.log("Flushing probe wait", type: SocketEngine.logType)

for waiter in probeWait {
write(waiter.msg, withType: waiter.type, withData: waiter.data, completion:waiter.completion)
write(waiter.msg, withType: waiter.type, withData: waiter.data, completion: waiter.completion)
}

probeWait.removeAll(keepingCapacity: false)
Expand Down Expand Up @@ -550,7 +550,7 @@ open class SocketEngine : NSObject, URLSessionDelegate, SocketEnginePollable, So
}

pongsMissed += 1
write("", withType: .ping, withData: [], completion: {})
write("", withType: .ping, withData: [], completion: nil)

engineQueue.asyncAfter(deadline: .now() + .milliseconds(pingInterval)) {[weak self, id = self.sid] in
// Make sure not to ping old connections
Expand Down Expand Up @@ -606,7 +606,7 @@ open class SocketEngine : NSObject, URLSessionDelegate, SocketEnginePollable, So
DefaultSocketLogger.Logger.log("Upgrading transport to WebSockets", type: SocketEngine.logType)

fastUpgrade = true
sendPollMessage("", withType: .noop, withData: [], completion: {})
sendPollMessage("", withType: .noop, withData: [], completion: nil)
// After this point, we should not send anymore polling messages
}
}
Expand All @@ -617,10 +617,10 @@ open class SocketEngine : NSObject, URLSessionDelegate, SocketEnginePollable, So
/// - parameter type: The type of this message.
/// - parameter data: Any data that this message has.
/// - parameter completion: Callback called on transport write completion.
open func write(_ msg: String, withType type: SocketEnginePacketType, withData data: [Data], completion: @escaping () -> ()) {
open func write(_ msg: String, withType type: SocketEnginePacketType, withData data: [Data], completion: (() -> ())? = nil) {
engineQueue.async {
guard self.connected else {
completion()
completion?()
return
}
guard !self.probing else {
Expand Down
6 changes: 3 additions & 3 deletions Source/SocketIO/Engine/SocketEnginePollable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public protocol SocketEnginePollable : SocketEngineSpec {
/// - parameter message: The message to send.
/// - parameter withType: The type of message to send.
/// - parameter withData: The data associated with this message.
func sendPollMessage(_ message: String, withType type: SocketEnginePacketType, withData datas: [Data], completion: @escaping () -> ())
func sendPollMessage(_ message: String, withType type: SocketEnginePacketType, withData datas: [Data], completion: (() -> ())?)

/// Call to stop polling and invalidate the URLSession.
func stopPolling()
Expand All @@ -75,7 +75,7 @@ public protocol SocketEnginePollable : SocketEngineSpec {
extension SocketEnginePollable {
func createRequestForPostWithPostWait() -> URLRequest {
defer {
for packet in postWait { packet.completion() }
for packet in postWait { packet.completion?() }
postWait.removeAll(keepingCapacity: true)
}

Expand Down Expand Up @@ -219,7 +219,7 @@ extension SocketEnginePollable {
/// - parameter withType: The type of message to send.
/// - parameter withData: The data associated with this message.
/// - parameter completion: Callback called on transport write completion.
public func sendPollMessage(_ message: String, withType type: SocketEnginePacketType, withData datas: [Data], completion: @escaping () -> ()) {
public func sendPollMessage(_ message: String, withType type: SocketEnginePacketType, withData datas: [Data], completion: (() -> ())? = nil) {
DefaultSocketLogger.Logger.log("Sending poll: \(message) as type: \(type.rawValue)", type: "SocketEnginePolling")

postWait.append((String(type.rawValue) + message, completion))
Expand Down
4 changes: 2 additions & 2 deletions Source/SocketIO/Engine/SocketEngineSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ import Starscream
/// - parameter type: The type of this message.
/// - parameter data: Any data that this message has.
/// - parameter completion: Callback called on transport write completion.
func write(_ msg: String, withType type: SocketEnginePacketType, withData data: [Data], completion: @escaping () -> ())
func write(_ msg: String, withType type: SocketEnginePacketType, withData data: [Data], completion: (() -> ())?)
}

extension SocketEngineSpec {
Expand Down Expand Up @@ -180,7 +180,7 @@ extension SocketEngineSpec {
}

/// Send an engine message (4)
func send(_ msg: String, withData datas: [Data], completion: @escaping () -> () = {}) {
func send(_ msg: String, withData datas: [Data], completion: (() -> ())? = nil) {
write(msg, withType: .message, withData: datas, completion: completion)
}
}
6 changes: 3 additions & 3 deletions Source/SocketIO/Engine/SocketEngineWebsocket.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ public protocol SocketEngineWebsocket : SocketEngineSpec {
func sendWebSocketMessage(_ str: String,
withType type: SocketEnginePacketType,
withData datas: [Data],
completion: @escaping () -> ())
completion: (() -> ())?)
}

// WebSocket methods
extension SocketEngineWebsocket {
func probeWebSocket() {
if ws?.isConnected ?? false {
sendWebSocketMessage("probe", withType: .ping, withData: [], completion: {})
sendWebSocketMessage("probe", withType: .ping, withData: [], completion: nil)
}
}

Expand All @@ -63,7 +63,7 @@ extension SocketEngineWebsocket {
public func sendWebSocketMessage(_ str: String,
withType type: SocketEnginePacketType,
withData datas: [Data],
completion: @escaping () -> ()
completion: (() -> ())?
) {
DefaultSocketLogger.Logger.log("Sending ws: \(str) as type: \(type.rawValue)", type: "SocketEngineWebSocket")

Expand Down
2 changes: 1 addition & 1 deletion Source/SocketIO/Manager/SocketManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ open class SocketManager : NSObject, SocketManagerSpec, SocketParsable, SocketDa
/// - parameter items: The data to send with this event.
open func emitAll(_ event: String, withItems items: [Any]) {
forAll {socket in
socket.emit(event, with: items, completion: {})
socket.emit(event, with: items, completion: nil)
}
}

Expand Down
4 changes: 2 additions & 2 deletions Source/SocketIO/Util/SocketTypes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,10 @@ public typealias AckCallback = ([Any]) -> ()
public typealias NormalCallback = ([Any], SocketAckEmitter) -> ()

/// A typealias for a queued POST
public typealias Post = (msg: String, completion: (() -> ()))
public typealias Post = (msg: String, completion: (() -> ())?)

typealias JSON = [String: Any]
typealias Probe = (msg: String, type: SocketEnginePacketType, data: [Data], completion: (() -> ()))
typealias Probe = (msg: String, type: SocketEnginePacketType, data: [Data], completion: (() -> ())?)
typealias ProbeWaitQueue = [Probe]

enum Either<E, V> {
Expand Down
2 changes: 1 addition & 1 deletion Tests/TestSocketIO/SocketMangerTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ public class TestSocket : SocketIOClient {
super.didDisconnect(reason: reason)
}

public override func emit(_ event: String, with items: [Any], completion: @escaping () -> ()) {
public override func emit(_ event: String, with items: [Any], completion: (() -> ())?) {
expectations[ManagerExpectation.emitAllEventCalled]?.fulfill()
expectations[ManagerExpectation.emitAllEventCalled] = nil

Expand Down
2 changes: 1 addition & 1 deletion Tests/TestSocketIO/SocketSideEffectTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -511,5 +511,5 @@ class TestEngine : SocketEngineSpec {
func flushWaitingForPostToWebSocket() { }
func parseEngineData(_ data: Data) { }
func parseEngineMessage(_ message: String) { }
func write(_ msg: String, withType type: SocketEnginePacketType, withData data: [Data], completion: @escaping () -> ()) { }
func write(_ msg: String, withType type: SocketEnginePacketType, withData data: [Data], completion: (() -> ())?) { }
}