Skip to content

refactor: add defaultClient to LiveQuery to replace getDefault() #342

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 8, 2022
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
[Full Changelog](https://github.com/parse-community/Parse-Swift/compare/4.1.0...main)
* _Contributing to this repo? Add info about your change here to be included in the next release_

__Improvements__
- Add clientDefault static property to ParseLiveQuery which replaces the getDefault() method. getDefault() is still avaiable, but will be deprecated in ParseSwift 5.0.0 so it is recommended to switch to clientDefault ([#342](https://github.com/parse-community/Parse-Swift/pull/342)), thanks to [Corey Baker](https://github.com/cbaker6).

### 4.1.0
[Full Changelog](https://github.com/parse-community/Parse-Swift/compare/4.0.1...4.1.0)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class LiveQueryDelegate: ParseLiveQueryDelegate {

//: Set the delegate.
let delegate = LiveQueryDelegate()
if let socket = ParseLiveQuery.getDefault() {
if let socket = ParseLiveQuery.defaultClient {
socket.receiveDelegate = delegate
}

Expand Down
31 changes: 27 additions & 4 deletions Sources/ParseSwift/LiveQuery/ParseLiveQuery.swift
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ Not attempting to open ParseLiveQuery socket anymore
}
}

// MARK: Client Intents
extension ParseLiveQuery {

/// Current LiveQuery client.
Expand Down Expand Up @@ -265,20 +266,29 @@ extension ParseLiveQuery {
}
}

/// The default `ParseLiveQuery` client for all LiveQuery connections.
class public var defaultClient: ParseLiveQuery? {
Self.client
}

/// Set a specific ParseLiveQuery client to be the default for all `ParseLiveQuery` connections.
/// - parameter client: The client to set as the default.
class public func setDefault(_ client: ParseLiveQuery) {
ParseLiveQuery.client = nil
ParseLiveQuery.client = client
Self.client = nil
Self.client = client
}

/// Get the default `ParseLiveQuery` client for all LiveQuery connections.
/// - returns: The default `ParseLiveQuery` client.
/// - warning: This will be deprecated in ParseSwift 5.0.0 in favor of `defaultClient`.
class public func getDefault() -> ParseLiveQuery? {
ParseLiveQuery.client
Self.defaultClient
}

/// Check if a query has an active subscription on this `ParseLiveQuery` client.
/// - parameter query: Query to verify.
/// - returns: **true** if subscribed. **false** otherwise.
/// - throws: An error of type `ParseError`.
public func isSubscribed<T: ParseObject>(_ query: Query<T>) throws -> Bool {
let queryData = try ParseCoding.jsonEncoder().encode(query)
return subscriptions.contains(where: { (_, value) -> Bool in
Expand All @@ -292,6 +302,8 @@ extension ParseLiveQuery {

/// Check if a query has a pending subscription on this `ParseLiveQuery` client.
/// - parameter query: Query to verify.
/// - returns: **true** if query is a pending subscription. **false** otherwise.
/// - throws: An error of type `ParseError`.
public func isPendingSubscription<T: ParseObject>(_ query: Query<T>) throws -> Bool {
let queryData = try ParseCoding.jsonEncoder().encode(query)
return pendingSubscriptions.contains(where: { (_, value) -> Bool in
Expand All @@ -305,6 +317,7 @@ extension ParseLiveQuery {

/// Remove a pending subscription on this `ParseLiveQuery` client.
/// - parameter query: Query to remove.
/// - throws: An error of type `ParseError`.
public func removePendingSubscription<T: ParseObject>(_ query: Query<T>) throws {
let queryData = try ParseCoding.jsonEncoder().encode(query)
pendingSubscriptions.removeAll(where: { (_, value) -> Bool in
Expand Down Expand Up @@ -851,7 +864,8 @@ public extension Query {
as the subscription can be used as a SwiftUI publisher. Meaning it can serve
indepedently as a ViewModel in MVVM.
- parameter client: A specific client.
- returns: The subscription that has just been registered
- returns: The subscription that has just been registered.
- throws: An error of type `ParseError`.
*/
func subscribe(_ client: ParseLiveQuery) throws -> Subscription<ResultType> {
try client.subscribe(Subscription(query: self))
Expand All @@ -862,6 +876,7 @@ public extension Query {
Registers a query for live updates, using a custom subscription handler.
- parameter handler: A custom subscription handler.
- returns: Your subscription handler, for easy chaining.
- throws: An error of type `ParseError`.
*/
static func subscribe<T: QuerySubscribable>(_ handler: T) throws -> T {
if let client = ParseLiveQuery.client {
Expand All @@ -876,6 +891,7 @@ public extension Query {
- parameter handler: A custom subscription handler.
- parameter client: A specific client.
- returns: Your subscription handler, for easy chaining.
- throws: An error of type `ParseError`.
*/
static func subscribe<T: QuerySubscribable>(_ handler: T, client: ParseLiveQuery) throws -> T {
try client.subscribe(handler)
Expand All @@ -894,6 +910,7 @@ public extension Query {
and a specific `ParseLiveQuery` client.
- parameter client: A specific client.
- returns: The subscription that has just been registered.
- throws: An error of type `ParseError`.
*/
func subscribeCallback(_ client: ParseLiveQuery) throws -> SubscriptionCallback<ResultType> {
try client.subscribe(SubscriptionCallback(query: self))
Expand All @@ -905,6 +922,7 @@ public extension Query {
/**
Unsubscribes all current subscriptions for a given query on the default
`ParseLiveQuery` client.
- throws: An error of type `ParseError`.
*/
func unsubscribe() throws {
try ParseLiveQuery.client?.unsubscribe(self)
Expand All @@ -914,6 +932,7 @@ public extension Query {
Unsubscribes all current subscriptions for a given query on a specific
`ParseLiveQuery` client.
- parameter client: A specific client.
- throws: An error of type `ParseError`.
*/
func unsubscribe(client: ParseLiveQuery) throws {
try client.unsubscribe(self)
Expand All @@ -923,6 +942,7 @@ public extension Query {
Unsubscribes from a specific query-handler on the default
`ParseLiveQuery` client.
- parameter handler: The specific handler to unsubscribe from.
- throws: An error of type `ParseError`.
*/
func unsubscribe<T: QuerySubscribable>(_ handler: T) throws {
try ParseLiveQuery.client?.unsubscribe(handler)
Expand All @@ -933,6 +953,7 @@ public extension Query {
`ParseLiveQuery` client.
- parameter handler: The specific handler to unsubscribe from.
- parameter client: A specific client.
- throws: An error of type `ParseError`.
*/
func unsubscribe<T: QuerySubscribable>(_ handler: T, client: ParseLiveQuery) throws {
try client.unsubscribe(handler)
Expand All @@ -945,6 +966,7 @@ public extension Query {
Updates an existing subscription with a new query on the default `ParseLiveQuery` client.
Upon completing the registration, the subscribe handler will be called with the new query.
- parameter handler: The specific handler to update.
- throws: An error of type `ParseError`.
*/
func update<T: QuerySubscribable>(_ handler: T) throws {
try ParseLiveQuery.client?.update(handler)
Expand All @@ -955,6 +977,7 @@ public extension Query {
Upon completing the registration, the subscribe handler will be called with the new query.
- parameter handler: The specific handler to update.
- parameter client: A specific client.
- throws: An error of type `ParseError`.
*/
func update<T: QuerySubscribable>(_ handler: T, client: ParseLiveQuery) throws {
try client.update(handler)
Expand Down
6 changes: 3 additions & 3 deletions Tests/ParseSwiftTests/ParseLiveQueryAsyncTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class ParseLiveQueryAsyncTests: XCTestCase { // swiftlint:disable:this type_body

@MainActor
func testOpen() async throws {
guard let client = ParseLiveQuery.getDefault() else {
guard let client = ParseLiveQuery.defaultClient else {
XCTFail("Should be able to get client")
return
}
Expand All @@ -57,7 +57,7 @@ class ParseLiveQueryAsyncTests: XCTestCase { // swiftlint:disable:this type_body

@MainActor
func testPingSocketNotEstablished() async throws {
guard let client = ParseLiveQuery.getDefault() else {
guard let client = ParseLiveQuery.defaultClient else {
XCTFail("Should be able to get client")
return
}
Expand All @@ -80,7 +80,7 @@ class ParseLiveQueryAsyncTests: XCTestCase { // swiftlint:disable:this type_body

@MainActor
func testPing() async throws {
guard let client = ParseLiveQuery.getDefault() else {
guard let client = ParseLiveQuery.defaultClient else {
XCTFail("Should be able to get client")
return
}
Expand Down
6 changes: 3 additions & 3 deletions Tests/ParseSwiftTests/ParseLiveQueryCombineTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class ParseLiveQueryCombineTests: XCTestCase {
}

func testOpen() throws {
guard let client = ParseLiveQuery.getDefault() else {
guard let client = ParseLiveQuery.defaultClient else {
XCTFail("Should be able to get client")
return
}
Expand Down Expand Up @@ -69,7 +69,7 @@ class ParseLiveQueryCombineTests: XCTestCase {
}

func testPingSocketNotEstablished() throws {
guard let client = ParseLiveQuery.getDefault() else {
guard let client = ParseLiveQuery.defaultClient else {
XCTFail("Should be able to get client")
return
}
Expand Down Expand Up @@ -104,7 +104,7 @@ class ParseLiveQueryCombineTests: XCTestCase {
}

func testPing() throws {
guard let client = ParseLiveQuery.getDefault() else {
guard let client = ParseLiveQuery.defaultClient else {
XCTFail("Should be able to get client")
return
}
Expand Down
Loading