From 22069d8e08b2d00f58b9e35175b62bf41f72b16f Mon Sep 17 00:00:00 2001 From: Florent Vilmart Date: Tue, 25 Oct 2016 16:22:08 -0400 Subject: [PATCH 1/3] Adds support for update operator --- Sources/ParseLiveQuery/Client.swift | 35 +++++++++++++++++-- .../ParseLiveQuery/Internal/Operation.swift | 4 +++ 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/Sources/ParseLiveQuery/Client.swift b/Sources/ParseLiveQuery/Client.swift index 6cff996b..7db43238 100644 --- a/Sources/ParseLiveQuery/Client.swift +++ b/Sources/ParseLiveQuery/Client.swift @@ -163,6 +163,31 @@ extension Client { return handler } + public func update( + _ fromQuery: PFQuery, + toQuery query: PFQuery, + subclassType: T.Type = T.self + ) where T: PFObject { + update(toQuery: query) { $0.query == fromQuery } + } + + public func update( + _ subscription: Subscription, + toQuery query: PFQuery) where T: PFObject { + update(toQuery: query) { + $0.subscriptionHandler === subscription + } + } + + func update( + toQuery query: PFQuery, + matching matcher: @escaping (SubscriptionRecord) -> Bool + ) where T: PFObject { + subscriptions(matching: matcher).forEach { + _ = sendOperationAsync(.update(requestId: $0.requestId, query: query as! PFQuery)) + } + } + /** Unsubscribes all current subscriptions for a given query. @@ -184,12 +209,16 @@ extension Client { } func unsubscribe(matching matcher: @escaping (SubscriptionRecord) -> Bool) { - subscriptions.filter { - matcher($0) - }.forEach { + subscriptions(matching: matcher).forEach { _ = sendOperationAsync(.unsubscribe(requestId: $0.requestId)) } } + + func subscriptions(matching matcher: @escaping (SubscriptionRecord) -> Bool) -> [SubscriptionRecord] { + return subscriptions.filter { + matcher($0) + } + } } diff --git a/Sources/ParseLiveQuery/Internal/Operation.swift b/Sources/ParseLiveQuery/Internal/Operation.swift index 27984d34..e8e362f9 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) + case update(requestId: Client.RequestId, query: PFQuery) case unsubscribe(requestId: Client.RequestId) var JSONObjectRepresentation: [String : Any] { @@ -23,6 +24,9 @@ enum ClientOperation { case .subscribe(let requestId, let query): return [ "op": "subscribe", "requestId": requestId.value, "query": Dictionary(query: query) ] + 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 ] } From d1b8b010949925d83829c63664c9babcd2193985 Mon Sep 17 00:00:00 2001 From: Florent Vilmart Date: Tue, 25 Oct 2016 17:35:27 -0400 Subject: [PATCH 2/3] Fix updating currentQuery insubscription --- Sources/ParseLiveQuery/Client.swift | 1 + Sources/ParseLiveQuery/Internal/ClientPrivate.swift | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/Sources/ParseLiveQuery/Client.swift b/Sources/ParseLiveQuery/Client.swift index 7db43238..cf409cbc 100644 --- a/Sources/ParseLiveQuery/Client.swift +++ b/Sources/ParseLiveQuery/Client.swift @@ -184,6 +184,7 @@ extension Client { matching matcher: @escaping (SubscriptionRecord) -> Bool ) where T: PFObject { subscriptions(matching: matcher).forEach { + $0.query = query as! PFQuery _ = sendOperationAsync(.update(requestId: $0.requestId, query: query as! PFQuery)) } } diff --git a/Sources/ParseLiveQuery/Internal/ClientPrivate.swift b/Sources/ParseLiveQuery/Internal/ClientPrivate.swift index 2b442e2f..85f59207 100644 --- a/Sources/ParseLiveQuery/Internal/ClientPrivate.swift +++ b/Sources/ParseLiveQuery/Internal/ClientPrivate.swift @@ -55,7 +55,7 @@ extension Client { var subscribeHandlerClosure: (Client) -> Void var unsubscribeHandlerClosure: (Client) -> Void - let query: PFQuery + var query: PFQuery let requestId: RequestId init(query: PFQuery, requestId: RequestId, handler: T) where T:SubscriptionHandling { From 30db8d919b6c5082aca7607eb76bd5fbc608803a Mon Sep 17 00:00:00 2001 From: Florent Vilmart Date: Tue, 25 Oct 2016 20:34:49 -0400 Subject: [PATCH 3/3] Makes sure we copy the subscriptions --- Sources/ParseLiveQuery/Client.swift | 47 +++++++------------ .../Internal/ClientPrivate.swift | 2 +- 2 files changed, 19 insertions(+), 30 deletions(-) diff --git a/Sources/ParseLiveQuery/Client.swift b/Sources/ParseLiveQuery/Client.swift index cf409cbc..8cb1ee97 100644 --- a/Sources/ParseLiveQuery/Client.swift +++ b/Sources/ParseLiveQuery/Client.swift @@ -163,29 +163,23 @@ extension Client { return handler } - public func update( - _ fromQuery: PFQuery, - toQuery query: PFQuery, - subclassType: T.Type = T.self - ) where T: PFObject { - update(toQuery: query) { $0.query == fromQuery } - } + /** + 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( - _ subscription: Subscription, - toQuery query: PFQuery) where T: PFObject { - update(toQuery: query) { - $0.subscriptionHandler === subscription - } - } - - func update( - toQuery query: PFQuery, - matching matcher: @escaping (SubscriptionRecord) -> Bool - ) where T: PFObject { - subscriptions(matching: matcher).forEach { - $0.query = query as! PFQuery - _ = sendOperationAsync(.update(requestId: $0.requestId, query: query as! PFQuery)) + _ 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 } } @@ -210,17 +204,12 @@ extension Client { } func unsubscribe(matching matcher: @escaping (SubscriptionRecord) -> Bool) { - subscriptions(matching: matcher).forEach { - _ = sendOperationAsync(.unsubscribe(requestId: $0.requestId)) - } - } - - func subscriptions(matching matcher: @escaping (SubscriptionRecord) -> Bool) -> [SubscriptionRecord] { - return subscriptions.filter { + subscriptions.filter { matcher($0) + }.forEach { + _ = sendOperationAsync(.unsubscribe(requestId: $0.requestId)) } } - } extension Client { diff --git a/Sources/ParseLiveQuery/Internal/ClientPrivate.swift b/Sources/ParseLiveQuery/Internal/ClientPrivate.swift index 85f59207..2b442e2f 100644 --- a/Sources/ParseLiveQuery/Internal/ClientPrivate.swift +++ b/Sources/ParseLiveQuery/Internal/ClientPrivate.swift @@ -55,7 +55,7 @@ extension Client { var subscribeHandlerClosure: (Client) -> Void var unsubscribeHandlerClosure: (Client) -> Void - var query: PFQuery + let query: PFQuery let requestId: RequestId init(query: PFQuery, requestId: RequestId, handler: T) where T:SubscriptionHandling {