-
-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Expose Subscription properties (#66)
- Loading branch information
Showing
6 changed files
with
111 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// | ||
// SubscriptionCallback.swift | ||
// ParseSwift | ||
// | ||
// Created by Corey Baker on 1/24/21. | ||
// Copyright © 2021 Parse Community. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
/** | ||
A default implementation of the `ParseSubscription` protocol using closures for callbacks. | ||
*/ | ||
open class SubscriptionCallback<T: ParseObject>: ParseSubscription { | ||
//The query subscribed to. | ||
public var query: Query<T> | ||
//The ParseObject | ||
public typealias Object = T | ||
fileprivate var eventHandlers: [(Query<T>, Event<T>) -> Void] = [] | ||
fileprivate var subscribeHandlers: [(Query<T>, Bool) -> Void] = [] | ||
fileprivate var unsubscribeHandlers: [(Query<T>) -> Void] = [] | ||
|
||
/** | ||
Creates a new subscription that can be used to handle updates. | ||
*/ | ||
public init(query: Query<T>) { | ||
self.query = query | ||
} | ||
|
||
/** | ||
Register a callback for when an event occurs. | ||
- parameter handler: The callback to register. | ||
- returns: The same subscription, for easy chaining. | ||
*/ | ||
@discardableResult open func handleEvent(_ handler: @escaping (Query<T>, | ||
Event<T>) -> Void) -> SubscriptionCallback { | ||
eventHandlers.append(handler) | ||
return self | ||
} | ||
|
||
/** | ||
Register a callback for when a client succesfully subscribes to a query. | ||
- parameter handler: The callback to register. | ||
- returns: The same subscription, for easy chaining. | ||
*/ | ||
@discardableResult open func handleSubscribe(_ handler: @escaping (Query<T>, | ||
Bool) -> Void) -> SubscriptionCallback { | ||
subscribeHandlers.append(handler) | ||
return self | ||
} | ||
|
||
/** | ||
Register a callback for when a query has been unsubscribed. | ||
- parameter handler: The callback to register. | ||
- returns: The same subscription, for easy chaining. | ||
*/ | ||
@discardableResult open func handleUnsubscribe(_ handler: @escaping (Query<T>) -> Void) -> SubscriptionCallback { | ||
unsubscribeHandlers.append(handler) | ||
return self | ||
} | ||
|
||
open func didReceive(_ eventData: Data) throws { | ||
// Need to decode the event with respect to the `ParseObject`. | ||
let eventMessage = try ParseCoding.jsonDecoder().decode(EventResponse<T>.self, from: eventData) | ||
guard let event = Event(event: eventMessage) else { | ||
throw ParseError(code: .unknownError, message: "ParseLiveQuery Error: couldn't create event.") | ||
} | ||
eventHandlers.forEach { $0(query, event) } | ||
} | ||
|
||
open func didSubscribe(_ new: Bool) { | ||
subscribeHandlers.forEach { $0(query, new) } | ||
} | ||
|
||
open func didUnsubscribe() { | ||
unsubscribeHandlers.forEach { $0(query) } | ||
} | ||
} |