diff --git a/Sources/ParseLiveQuery/Client.swift b/Sources/ParseLiveQuery/Client.swift index d9f14e8b..a6b4be00 100644 --- a/Sources/ParseLiveQuery/Client.swift +++ b/Sources/ParseLiveQuery/Client.swift @@ -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( + _ handler: T, + toQuery query: PFQuery + ) where T: SubscriptionHandling { + subscriptions = subscriptions.map { + if $0.subscriptionHandler === handler { + _ = sendOperationAsync(.update(requestId: $0.requestId, query: query as! PFQuery)) + return SubscriptionRecord(query: query, requestId: $0.requestId, handler: $0.subscriptionHandler as! T) + } + return $0 + } + } + /** Unsubscribes all current subscriptions for a given query. @@ -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 { diff --git a/Sources/ParseLiveQuery/Internal/Operation.swift b/Sources/ParseLiveQuery/Internal/Operation.swift index 7dfa1012..8cb3c0d6 100644 --- a/Sources/ParseLiveQuery/Internal/Operation.swift +++ b/Sources/ParseLiveQuery/Internal/Operation.swift @@ -13,6 +13,7 @@ import Parse enum ClientOperation { case connect(applicationId: String, sessionToken: String) case subscribe(requestId: Client.RequestId, query: PFQuery, sessionToken: String?) + case update(requestId: Client.RequestId, query: PFQuery) case unsubscribe(requestId: Client.RequestId) var JSONObjectRepresentation: [String : Any] { @@ -27,6 +28,9 @@ enum ClientOperation { } return result + case .update(let requestId, let query): + return [ "op": "update", "requestId": requestId.value, "query": Dictionary(query: query) ] + case .unsubscribe(let requestId): return [ "op": "unsubscribe", "requestId": requestId.value ] }