@@ -23,7 +23,7 @@ public class Client: NSObject {
23
23
let clientKey : String ?
24
24
25
25
var socket : SRWebSocket ?
26
- var disconnected = false
26
+ var userDisconnected = false
27
27
28
28
// This allows us to easily plug in another request ID generation scheme, or more easily change the request id type
29
29
// if needed (technically this could be a string).
@@ -119,15 +119,15 @@ extension Client {
119
119
120
120
- parameter query: The query to register for updates.
121
121
- parameter subclassType: The subclass of PFObject to be used as the type of the Subscription.
122
- This parameter can be automatically inferred from context most of the time
122
+ This parameter can be automatically inferred from context most of the time
123
123
124
124
- returns: The subscription that has just been registered
125
125
*/
126
126
public func subscribe< T where T: PFObject > (
127
127
query: PFQuery ,
128
128
subclassType: T . Type = T . self
129
129
) -> Subscription < T > {
130
- return subscribe ( query, handler: Subscription < T > ( ) )
130
+ return subscribe ( query, handler: Subscription < T > ( ) )
131
131
}
132
132
133
133
/**
@@ -142,22 +142,24 @@ extension Client {
142
142
query: PFQuery ,
143
143
handler: T
144
144
) -> T {
145
- let subscriptionRecord = SubscriptionRecord (
146
- query: query,
147
- requestId: requestIdGenerator ( ) ,
148
- handler: handler
149
- )
150
- subscriptions. append ( subscriptionRecord)
151
-
152
- if socket == nil {
153
- if !disconnected {
154
- reconnect ( )
155
- }
145
+ let subscriptionRecord = SubscriptionRecord (
146
+ query: query,
147
+ requestId: requestIdGenerator ( ) ,
148
+ handler: handler
149
+ )
150
+ subscriptions. append ( subscriptionRecord)
151
+
152
+ if socket? . readyState == . OPEN {
153
+ sendOperationAsync ( . Subscribe( requestId: subscriptionRecord. requestId, query: query) )
154
+ } else if socket == nil || socket? . readyState != . CONNECTING {
155
+ if !userDisconnected {
156
+ reconnect ( )
156
157
} else {
157
- sendOperationAsync ( . Subscribe ( requestId : subscriptionRecord . requestId , query : query ) )
158
+ debugPrint ( " Warning: The client was explicitly disconnected! You must explicitly call .reconnect() in order to process your subscriptions. " )
158
159
}
159
-
160
- return handler
160
+ }
161
+
162
+ return handler
161
163
}
162
164
163
165
/**
@@ -183,10 +185,11 @@ extension Client {
183
185
func unsubscribe( @noescape matching matcher: SubscriptionRecord -> Bool ) {
184
186
subscriptions. filter {
185
187
matcher ( $0)
186
- } . forEach {
187
- sendOperationAsync ( . Unsubscribe( requestId: $0. requestId) )
188
+ } . forEach {
189
+ sendOperationAsync ( . Unsubscribe( requestId: $0. requestId) )
188
190
}
189
191
}
192
+
190
193
}
191
194
192
195
extension Client {
@@ -203,7 +206,7 @@ extension Client {
203
206
socket. delegate = self
204
207
socket. setDelegateDispatchQueue ( queue)
205
208
socket. open ( )
206
-
209
+ userDisconnected = false
207
210
return socket
208
211
} ( )
209
212
}
@@ -221,6 +224,6 @@ extension Client {
221
224
}
222
225
socket. close ( )
223
226
self . socket = nil
224
- disconnected = true
227
+ userDisconnected = true
225
228
}
226
- }
229
+ }
0 commit comments