Skip to content
This repository was archived by the owner on Jun 13, 2023. It is now read-only.

Commit 69c0536

Browse files
authored
Live query update (#81)
* Adds support for update operator * Fix updating currentQuery insubscription * Makes sure we copy the subscriptions
1 parent 5eb6f37 commit 69c0536

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

Sources/ParseLiveQuery/Client.swift

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,26 @@ extension Client {
164164
return handler
165165
}
166166

167+
/**
168+
Updates an existing subscription with a new query.
169+
Upon completing the registration, the subscribe handler will be called with the new query
170+
171+
- parameter handler: The specific handler to update.
172+
- parameter query: The new query for that handler.
173+
*/
174+
public func update<T>(
175+
_ handler: T,
176+
toQuery query: PFQuery<T.PFObjectSubclass>
177+
) where T: SubscriptionHandling {
178+
subscriptions = subscriptions.map {
179+
if $0.subscriptionHandler === handler {
180+
_ = sendOperationAsync(.update(requestId: $0.requestId, query: query as! PFQuery<PFObject>))
181+
return SubscriptionRecord(query: query, requestId: $0.requestId, handler: $0.subscriptionHandler as! T)
182+
}
183+
return $0
184+
}
185+
}
186+
167187
/**
168188
Unsubscribes all current subscriptions for a given query.
169189

@@ -187,11 +207,10 @@ extension Client {
187207
func unsubscribe(matching matcher: @escaping (SubscriptionRecord) -> Bool) {
188208
subscriptions.filter {
189209
matcher($0)
190-
}.forEach {
191-
_ = sendOperationAsync(.unsubscribe(requestId: $0.requestId))
210+
}.forEach {
211+
_ = sendOperationAsync(.unsubscribe(requestId: $0.requestId))
192212
}
193213
}
194-
195214
}
196215

197216
extension Client {

Sources/ParseLiveQuery/Internal/Operation.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import Parse
1313
enum ClientOperation {
1414
case connect(applicationId: String, sessionToken: String)
1515
case subscribe(requestId: Client.RequestId, query: PFQuery<PFObject>, sessionToken: String?)
16+
case update(requestId: Client.RequestId, query: PFQuery<PFObject>)
1617
case unsubscribe(requestId: Client.RequestId)
1718

1819
var JSONObjectRepresentation: [String : Any] {
@@ -27,6 +28,9 @@ enum ClientOperation {
2728
}
2829
return result
2930

31+
case .update(let requestId, let query):
32+
return [ "op": "update", "requestId": requestId.value, "query": Dictionary<String, AnyObject>(query: query) ]
33+
3034
case .unsubscribe(let requestId):
3135
return [ "op": "unsubscribe", "requestId": requestId.value ]
3236
}

0 commit comments

Comments
 (0)