Skip to content

Commit 91eea96

Browse files
committed
Make a bunch of things public. Fixes socketio#803
1 parent 486a89d commit 91eea96

File tree

6 files changed

+313
-72
lines changed

6 files changed

+313
-72
lines changed

Source/SocketIO/Ack/SocketAckEmitter.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,13 @@ public final class SocketAckEmitter : NSObject {
3939
return ackNum != -1
4040
}
4141

42-
init(socket: SocketIOClient, ackNum: Int) {
42+
// MARK: Initializers
43+
44+
/// Creates a new `SocketAckEmitter`.
45+
///
46+
/// - parameter socket: The socket for this emitter.
47+
/// - parameter ackNum: The ack number for this emitter.
48+
public init(socket: SocketIOClient, ackNum: Int) {
4349
self.socket = socket
4450
self.ackNum = ackNum
4551
}

Source/SocketIO/Client/SocketEventHandler.swift

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,27 @@
2424

2525
import Foundation
2626

27-
struct SocketEventHandler {
28-
let event: String
29-
let id: UUID
30-
let callback: NormalCallback
31-
32-
func executeCallback(with items: [Any], withAck ack: Int, withSocket socket: SocketIOClient) {
27+
/// A wrapper around a handler.
28+
public struct SocketEventHandler {
29+
// MARK: Properties
30+
31+
/// The event for this handler.
32+
public let event: String
33+
34+
/// A unique identifier for this handler.
35+
public let id: UUID
36+
37+
/// The actual handler function.
38+
public let callback: NormalCallback
39+
40+
// MARK: Methods
41+
42+
/// Causes this handler to be executed.
43+
///
44+
/// - parameter with: The data that this handler should be called with.
45+
/// - parameter withAck: The ack number that this event expects. Pass -1 to say this event doesn't expect an ack.
46+
/// - parameter withSocket: The socket that is calling this event.
47+
public func executeCallback(with items: [Any], withAck ack: Int, withSocket socket: SocketIOClient) {
3348
callback(items, SocketAckEmitter(socket: socket, ackNum: ack))
3449
}
3550
}

Source/SocketIO/Client/SocketIOClient.swift

Lines changed: 61 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -35,26 +35,6 @@ open class SocketIOClient : NSObject, SocketIOClientSpec, SocketEngineClient, So
3535

3636
private static let logType = "SocketIOClient"
3737

38-
/// The engine for this client.
39-
@objc
40-
public private(set) var engine: SocketEngineSpec?
41-
42-
/// The status of this client.
43-
@objc
44-
public private(set) var status = SocketIOClientStatus.notConnected {
45-
didSet {
46-
switch status {
47-
case .connected:
48-
reconnecting = false
49-
currentReconnectAttempt = 0
50-
default:
51-
break
52-
}
53-
54-
handleClientEvent(.statusChange, data: [status])
55-
}
56-
}
57-
5838
/// If `true` then every time `connect` is called, a new engine will be created.
5939
@objc
6040
public var forceNew = false
@@ -94,20 +74,46 @@ open class SocketIOClient : NSObject, SocketIOClientSpec, SocketEngineClient, So
9474
@objc
9575
public var socketURL: URL
9676

97-
var ackHandlers = SocketAckManager()
98-
9977
/// A list of packets that are waiting for binary data.
10078
///
10179
/// The way that socket.io works all data should be sent directly after each packet.
10280
/// So this should ideally be an array of one packet waiting for data.
103-
var waitingPackets = [SocketPacket]()
81+
///
82+
/// **This should not be modified directly.**
83+
public var waitingPackets = [SocketPacket]()
84+
85+
/// A handler that will be called on any event.
86+
public private(set) var anyHandler: ((SocketAnyEvent) -> ())?
87+
88+
/// The engine for this client.
89+
@objc
90+
public private(set) var engine: SocketEngineSpec?
91+
92+
/// The array of handlers for this socket.
93+
public private(set) var handlers = [SocketEventHandler]()
94+
95+
/// The status of this client.
96+
@objc
97+
public private(set) var status = SocketIOClientStatus.notConnected {
98+
didSet {
99+
switch status {
100+
case .connected:
101+
reconnecting = false
102+
currentReconnectAttempt = 0
103+
default:
104+
break
105+
}
106+
107+
handleClientEvent(.statusChange, data: [status])
108+
}
109+
}
110+
111+
var ackHandlers = SocketAckManager()
104112

105113
private(set) var currentAck = -1
106114
private(set) var reconnectAttempts = -1
107115

108-
private var anyHandler: ((SocketAnyEvent) -> ())?
109116
private var currentReconnectAttempt = 0
110-
private var handlers = [SocketEventHandler]()
111117
private var reconnecting = false
112118

113119
// MARK: Initializers
@@ -232,7 +238,9 @@ open class SocketIOClient : NSObject, SocketIOClientSpec, SocketEngineClient, So
232238

233239
/// Called when the client connects to a namespace. If the client was created with a namespace upfront,
234240
/// then this is only called when the client connects to that namespace.
235-
func didConnect(toNamespace namespace: String) {
241+
///
242+
/// - parameter toNamespace: The namespace that was connected to.
243+
open func didConnect(toNamespace namespace: String) {
236244
DefaultSocketLogger.Logger.log("Socket connected", type: SocketIOClient.logType)
237245

238246
status = .connected
@@ -241,7 +249,9 @@ open class SocketIOClient : NSObject, SocketIOClientSpec, SocketEngineClient, So
241249
}
242250

243251
/// Called when the client has disconnected from socket.io.
244-
func didDisconnect(reason: String) {
252+
///
253+
/// - parameter reason: The reason for the disconnection.
254+
open func didDisconnect(reason: String) {
245255
guard status != .disconnected else { return }
246256

247257
DefaultSocketLogger.Logger.log("Disconnected: \(reason)", type: SocketIOClient.logType)
@@ -361,8 +371,13 @@ open class SocketIOClient : NSObject, SocketIOClientSpec, SocketEngineClient, So
361371
engine?.send(str, withData: packet.binary)
362372
}
363373

364-
// If the server wants to know that the client received data
365-
func emitAck(_ ack: Int, with items: [Any]) {
374+
/// Call when you wish to tell the server that you've received the event for `ack`.
375+
///
376+
/// **You shouldn't need to call this directly.** Instead use an `SocketAckEmitter` that comes in an event callback.
377+
///
378+
/// - parameter ack: The ack number.
379+
/// - parameter with: The data for this ack.
380+
open func emitAck(_ ack: Int, with items: [Any]) {
366381
guard status == .connected else { return }
367382

368383
let packet = SocketPacket.packetFromEmit(items, id: ack, nsp: nsp, ack: true)
@@ -419,21 +434,25 @@ open class SocketIOClient : NSObject, SocketIOClientSpec, SocketEngineClient, So
419434
DefaultSocketLogger.Logger.log(reason, type: SocketIOClient.logType)
420435
}
421436

422-
// Called when the socket gets an ack for something it sent
423-
func handleAck(_ ack: Int, data: [Any]) {
437+
/// Called when socket.io has acked one of our emits. Causes the corresponding ack callback to be called.
438+
///
439+
/// - parameter ack: The number for this ack.
440+
/// - parameter data: The data sent back with this ack.
441+
@objc
442+
open func handleAck(_ ack: Int, data: [Any]) {
424443
guard status == .connected else { return }
425444

426445
DefaultSocketLogger.Logger.log("Handling ack: \(ack) with data: \(data)", type: SocketIOClient.logType)
427446

428447
ackHandlers.executeAck(ack, with: data, onQueue: handleQueue)
429448
}
430449

431-
/// Causes an event to be handled, and any event handlers for that event to be called.
450+
/// Called when we get an event from socket.io.
432451
///
433-
/// - parameter event: The event that is to be handled.
434-
/// - parameter data: the data associated with this event.
435-
/// - parameter isInternalMessage: If `true` event handlers for this event will be called regardless of status.
436-
/// - parameter withAck: The ack number for this event. May be left out.
452+
/// - parameter event: The name of the event.
453+
/// - parameter data: The data that was sent with this event.
454+
/// - parameter isInternalMessage: Whether this event was sent internally. If `true` it is always sent to handlers.
455+
/// - parameter withAck: If > 0 then this event expects to get an ack back from the client.
437456
@objc
438457
open func handleEvent(_ event: String, data: [Any], isInternalMessage: Bool, withAck ack: Int = -1) {
439458
guard status == .connected || isInternalMessage else { return }
@@ -447,7 +466,11 @@ open class SocketIOClient : NSObject, SocketIOClientSpec, SocketEngineClient, So
447466
}
448467
}
449468

450-
func handleClientEvent(_ event: SocketClientEvent, data: [Any]) {
469+
/// Called on socket.io specific events.
470+
///
471+
/// - parameter event: The `SocketClientEvent`.
472+
/// - parameter data: The data for this event.
473+
open func handleClientEvent(_ event: SocketClientEvent, data: [Any]) {
451474
handleEvent(event.rawValue, data: data, isInternalMessage: true)
452475
}
453476

@@ -615,6 +638,7 @@ open class SocketIOClient : NSObject, SocketIOClientSpec, SocketEngineClient, So
615638
}
616639

617640
/// Removes all handlers.
641+
///
618642
/// Can be used after disconnecting to break any potential remaining retain cycles.
619643
@objc
620644
open func removeAllHandlers() {

0 commit comments

Comments
 (0)