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

Live query update #81

Merged
merged 4 commits into from
Feb 23, 2017
Merged
Show file tree
Hide file tree
Changes from all 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
25 changes: 22 additions & 3 deletions Sources/ParseLiveQuery/Client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,26 @@ extension Client {
return handler
}

/**
Updates an existing subscription with a new query.
Upon completing the registration, the subscribe handler will be called with the new query

- parameter handler: The specific handler to update.
- parameter query: The new query for that handler.
*/
public func update<T>(
_ handler: T,
toQuery query: PFQuery<T.PFObjectSubclass>
) where T: SubscriptionHandling {
subscriptions = subscriptions.map {
if $0.subscriptionHandler === handler {
_ = sendOperationAsync(.update(requestId: $0.requestId, query: query as! PFQuery<PFObject>))
return SubscriptionRecord(query: query, requestId: $0.requestId, handler: $0.subscriptionHandler as! T)
}
return $0
}
}

/**
Unsubscribes all current subscriptions for a given query.

Expand All @@ -187,11 +207,10 @@ extension Client {
func unsubscribe(matching matcher: @escaping (SubscriptionRecord) -> Bool) {
subscriptions.filter {
matcher($0)
}.forEach {
_ = sendOperationAsync(.unsubscribe(requestId: $0.requestId))
}.forEach {
_ = sendOperationAsync(.unsubscribe(requestId: $0.requestId))
}
}

}

extension Client {
Expand Down
4 changes: 4 additions & 0 deletions Sources/ParseLiveQuery/Internal/Operation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import Parse
enum ClientOperation {
case connect(applicationId: String, sessionToken: String)
case subscribe(requestId: Client.RequestId, query: PFQuery<PFObject>, sessionToken: String?)
case update(requestId: Client.RequestId, query: PFQuery<PFObject>)
case unsubscribe(requestId: Client.RequestId)

var JSONObjectRepresentation: [String : Any] {
Expand All @@ -27,6 +28,9 @@ enum ClientOperation {
}
return result

case .update(let requestId, let query):
return [ "op": "update", "requestId": requestId.value, "query": Dictionary<String, AnyObject>(query: query) ]

case .unsubscribe(let requestId):
return [ "op": "unsubscribe", "requestId": requestId.value ]
}
Expand Down