SocketIOClient: removed unnecessary weak self

This commit is contained in:
NachoSoto 2015-09-11 05:31:41 +00:00
parent 1739afa670
commit 2bc72e1975

View File

@ -171,10 +171,10 @@ public final class SocketIOClient: NSObject, SocketEngineClient {
let time = dispatch_time(DISPATCH_TIME_NOW, Int64(timeoutAfter) * Int64(NSEC_PER_SEC)) let time = dispatch_time(DISPATCH_TIME_NOW, Int64(timeoutAfter) * Int64(NSEC_PER_SEC))
dispatch_after(time, dispatch_get_main_queue()) {[weak self] in dispatch_after(time, dispatch_get_main_queue()) {
if let this = self where this.status != SocketIOClientStatus.Connected { if self.status != .Connected {
this.status = SocketIOClientStatus.Closed self.status = .Closed
this.engine?.close(fast: true) self.engine?.close(fast: true)
handler?() handler?()
} }
@ -186,15 +186,15 @@ public final class SocketIOClient: NSObject, SocketEngineClient {
if let this = self { if let this = self {
this.ackHandlers.addAck(ack, callback: callback) this.ackHandlers.addAck(ack, callback: callback)
dispatch_async(this.emitQueue) {[weak this] in dispatch_async(this.emitQueue) {
this?._emit(items, ack: ack) this._emit(items, ack: ack)
} }
if timeout != 0 { if timeout != 0 {
let time = dispatch_time(DISPATCH_TIME_NOW, Int64(timeout * NSEC_PER_SEC)) let time = dispatch_time(DISPATCH_TIME_NOW, Int64(timeout * NSEC_PER_SEC))
dispatch_after(time, dispatch_get_main_queue()) {[weak this] in dispatch_after(time, dispatch_get_main_queue()) {
this?.ackHandlers.timeoutAck(ack) this.ackHandlers.timeoutAck(ack)
} }
} }
} }
@ -203,7 +203,7 @@ public final class SocketIOClient: NSObject, SocketEngineClient {
func didConnect() { func didConnect() {
Logger.log("Socket connected", type: logType) Logger.log("Socket connected", type: logType)
status = SocketIOClientStatus.Connected status = .Connected
currentReconnectAttempt = 0 currentReconnectAttempt = 0
clearReconnectTimer() clearReconnectTimer()
@ -213,13 +213,13 @@ public final class SocketIOClient: NSObject, SocketEngineClient {
} }
func didDisconnect(reason: String) { func didDisconnect(reason: String) {
guard status != SocketIOClientStatus.Closed else { guard status != .Closed else {
return return
} }
Logger.log("Disconnected: %@", type: logType, args: reason) Logger.log("Disconnected: %@", type: logType, args: reason)
status = SocketIOClientStatus.Closed status = .Closed
reconnects = false reconnects = false
@ -254,12 +254,12 @@ public final class SocketIOClient: NSObject, SocketEngineClient {
Same as emit, but meant for Objective-C Same as emit, but meant for Objective-C
*/ */
public func emit(event: String, withItems items: [AnyObject]) { public func emit(event: String, withItems items: [AnyObject]) {
guard status == SocketIOClientStatus.Connected else { guard status == .Connected else {
return return
} }
dispatch_async(emitQueue) {[weak self] in dispatch_async(emitQueue) {
self?._emit([event] + items) self._emit([event] + items)
} }
} }
@ -279,7 +279,7 @@ public final class SocketIOClient: NSObject, SocketEngineClient {
} }
private func _emit(data: [AnyObject], ack: Int? = nil) { private func _emit(data: [AnyObject], ack: Int? = nil) {
guard status == SocketIOClientStatus.Connected else { guard status == .Connected else {
return return
} }
@ -288,7 +288,7 @@ public final class SocketIOClient: NSObject, SocketEngineClient {
Logger.log("Emitting: %@", type: logType, args: str) Logger.log("Emitting: %@", type: logType, args: str)
if packet.type == SocketPacket.PacketType.BinaryEvent { if packet.type == .BinaryEvent {
engine?.send(str, withData: packet.binary) engine?.send(str, withData: packet.binary)
} else { } else {
engine?.send(str, withData: nil) engine?.send(str, withData: nil)
@ -297,17 +297,17 @@ public final class SocketIOClient: NSObject, SocketEngineClient {
// If the server wants to know that the client received data // If the server wants to know that the client received data
func emitAck(ack: Int, withItems items: [AnyObject]) { func emitAck(ack: Int, withItems items: [AnyObject]) {
dispatch_async(emitQueue) {[weak self] in dispatch_async(emitQueue) {
if let this = self where this.status == SocketIOClientStatus.Connected { if self.status == .Connected {
let packet = SocketPacket.packetFromEmit(items, id: ack ?? -1, nsp: this.nsp, ack: true) let packet = SocketPacket.packetFromEmit(items, id: ack ?? -1, nsp: self.nsp, ack: true)
let str = packet.packetString let str = packet.packetString
Logger.log("Emitting Ack: %@", type: this.logType, args: str) Logger.log("Emitting Ack: %@", type: self.logType, args: str)
if packet.type == SocketPacket.PacketType.BinaryAck { if packet.type == SocketPacket.PacketType.BinaryAck {
this.engine?.send(str, withData: packet.binary) self.engine?.send(str, withData: packet.binary)
} else { } else {
this.engine?.send(str, withData: nil) self.engine?.send(str, withData: nil)
} }
} }
@ -317,10 +317,10 @@ public final class SocketIOClient: NSObject, SocketEngineClient {
public func engineDidClose(reason: String) { public func engineDidClose(reason: String) {
waitingData.removeAll() waitingData.removeAll()
if status == SocketIOClientStatus.Closed || !reconnects { if status == .Closed || !reconnects {
didDisconnect(reason) didDisconnect(reason)
} else if status != SocketIOClientStatus.Reconnecting { } else if status != .Reconnecting {
status = SocketIOClientStatus.Reconnecting status = .Reconnecting
handleEvent("reconnect", data: [reason], isInternalMessage: true) handleEvent("reconnect", data: [reason], isInternalMessage: true)
tryReconnect() tryReconnect()
} }
@ -339,22 +339,21 @@ public final class SocketIOClient: NSObject, SocketEngineClient {
*/ */
public func handleEvent(event: String, data: [AnyObject]?, isInternalMessage: Bool, public func handleEvent(event: String, data: [AnyObject]?, isInternalMessage: Bool,
wantsAck ack: Int? = nil) { wantsAck ack: Int? = nil) {
guard status == SocketIOClientStatus.Connected || isInternalMessage else { guard status == .Connected || isInternalMessage else {
return return
} }
// println("Should do event: \(event) with data: \(data)")
Logger.log("Handling event: %@ with data: %@", type: logType, args: event, data ?? "") Logger.log("Handling event: %@ with data: %@", type: logType, args: event, data ?? "")
if anyHandler != nil { if anyHandler != nil {
dispatch_async(handleQueue) {[weak self] in dispatch_async(handleQueue) {
self?.anyHandler?(SocketAnyEvent(event: event, items: data)) self.anyHandler?(SocketAnyEvent(event: event, items: data))
} }
} }
for handler in handlers where handler.event == event { for handler in handlers where handler.event == event {
if let ack = ack { if let ack = ack {
dispatch_async(handleQueue) {[weak self] in dispatch_async(handleQueue) {
handler.executeCallback(data, withAck: ack, withSocket: self) handler.executeCallback(data, withAck: ack, withSocket: self)
} }
} else { } else {
@ -446,18 +445,14 @@ public final class SocketIOClient: NSObject, SocketEngineClient {
} }
public func parseSocketMessage(msg: String) { public func parseSocketMessage(msg: String) {
dispatch_async(handleQueue) {[weak self] in dispatch_async(handleQueue) {
if let this = self { SocketParser.parseSocketMessage(msg, socket: self)
SocketParser.parseSocketMessage(msg, socket: this)
}
} }
} }
public func parseBinaryData(data: NSData) { public func parseBinaryData(data: NSData) {
dispatch_async(handleQueue) {[weak self] in dispatch_async(handleQueue) {
if let this = self { SocketParser.parseBinaryData(data, socket: self)
SocketParser.parseBinaryData(data, socket: this)
}
} }
} }
@ -473,19 +468,17 @@ public final class SocketIOClient: NSObject, SocketEngineClient {
if reconnectTimer == nil { if reconnectTimer == nil {
Logger.log("Starting reconnect", type: logType) Logger.log("Starting reconnect", type: logType)
status = SocketIOClientStatus.Reconnecting status = .Reconnecting
dispatch_async(dispatch_get_main_queue()) {[weak self] in dispatch_async(dispatch_get_main_queue()) {
if let this = self { self.reconnectTimer = NSTimer.scheduledTimerWithTimeInterval(Double(self.reconnectWait),
this.reconnectTimer = NSTimer.scheduledTimerWithTimeInterval(Double(this.reconnectWait), target: self, selector: "_tryReconnect", userInfo: nil, repeats: true)
target: this, selector: "_tryReconnect", userInfo: nil, repeats: true)
}
} }
} }
} }
@objc private func _tryReconnect() { @objc private func _tryReconnect() {
if status == SocketIOClientStatus.Connected { if status == .Connected {
clearReconnectTimer() clearReconnectTimer()
return return