From 4515da28db0dece1221f59d91209bb0c09ac88db Mon Sep 17 00:00:00 2001 From: Corey Baker Date: Tue, 8 Feb 2022 09:50:08 -0500 Subject: [PATCH 1/3] refactor: add defaultClient to LiveQuery to replace getDefault() --- .../Contents.swift | 2 +- .../ParseSwift/LiveQuery/ParseLiveQuery.swift | 30 +++++++- .../ParseLiveQueryAsyncTests.swift | 6 +- .../ParseLiveQueryCombineTests.swift | 6 +- .../ParseSwiftTests/ParseLiveQueryTests.swift | 74 +++++++++---------- 5 files changed, 70 insertions(+), 48 deletions(-) diff --git a/ParseSwift.playground/Pages/11 - LiveQuery.xcplaygroundpage/Contents.swift b/ParseSwift.playground/Pages/11 - LiveQuery.xcplaygroundpage/Contents.swift index ff37ee844..bf97c1a14 100644 --- a/ParseSwift.playground/Pages/11 - LiveQuery.xcplaygroundpage/Contents.swift +++ b/ParseSwift.playground/Pages/11 - LiveQuery.xcplaygroundpage/Contents.swift @@ -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 } diff --git a/Sources/ParseSwift/LiveQuery/ParseLiveQuery.swift b/Sources/ParseSwift/LiveQuery/ParseLiveQuery.swift index 82d70c269..d0f0dbde6 100644 --- a/Sources/ParseSwift/LiveQuery/ParseLiveQuery.swift +++ b/Sources/ParseSwift/LiveQuery/ParseLiveQuery.swift @@ -265,20 +265,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(_ query: Query) throws -> Bool { let queryData = try ParseCoding.jsonEncoder().encode(query) return subscriptions.contains(where: { (_, value) -> Bool in @@ -292,6 +301,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(_ query: Query) throws -> Bool { let queryData = try ParseCoding.jsonEncoder().encode(query) return pendingSubscriptions.contains(where: { (_, value) -> Bool in @@ -305,6 +316,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(_ query: Query) throws { let queryData = try ParseCoding.jsonEncoder().encode(query) pendingSubscriptions.removeAll(where: { (_, value) -> Bool in @@ -851,7 +863,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 { try client.subscribe(Subscription(query: self)) @@ -862,6 +875,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(_ handler: T) throws -> T { if let client = ParseLiveQuery.client { @@ -876,6 +890,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(_ handler: T, client: ParseLiveQuery) throws -> T { try client.subscribe(handler) @@ -894,6 +909,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 { try client.subscribe(SubscriptionCallback(query: self)) @@ -905,6 +921,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) @@ -914,6 +931,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) @@ -923,6 +941,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(_ handler: T) throws { try ParseLiveQuery.client?.unsubscribe(handler) @@ -933,6 +952,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(_ handler: T, client: ParseLiveQuery) throws { try client.unsubscribe(handler) @@ -945,6 +965,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(_ handler: T) throws { try ParseLiveQuery.client?.update(handler) @@ -955,6 +976,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(_ handler: T, client: ParseLiveQuery) throws { try client.update(handler) diff --git a/Tests/ParseSwiftTests/ParseLiveQueryAsyncTests.swift b/Tests/ParseSwiftTests/ParseLiveQueryAsyncTests.swift index ded49c70c..dc806f96c 100644 --- a/Tests/ParseSwiftTests/ParseLiveQueryAsyncTests.swift +++ b/Tests/ParseSwiftTests/ParseLiveQueryAsyncTests.swift @@ -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 } @@ -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 } @@ -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 } diff --git a/Tests/ParseSwiftTests/ParseLiveQueryCombineTests.swift b/Tests/ParseSwiftTests/ParseLiveQueryCombineTests.swift index 86e7f3121..a9d9645bf 100644 --- a/Tests/ParseSwiftTests/ParseLiveQueryCombineTests.swift +++ b/Tests/ParseSwiftTests/ParseLiveQueryCombineTests.swift @@ -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 } @@ -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 } @@ -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 } diff --git a/Tests/ParseSwiftTests/ParseLiveQueryTests.swift b/Tests/ParseSwiftTests/ParseLiveQueryTests.swift index 49f240040..aa0079457 100644 --- a/Tests/ParseSwiftTests/ParseLiveQueryTests.swift +++ b/Tests/ParseSwiftTests/ParseLiveQueryTests.swift @@ -83,7 +83,7 @@ class ParseLiveQueryTests: XCTestCase { components.scheme = (components.scheme == "https" || components.scheme == "wss") ? "wss" : "ws" let webSocketURL = components.url - guard let client = ParseLiveQuery.getDefault() else { + guard let client = ParseLiveQuery.defaultClient else { XCTFail("Should be able to get client") return } @@ -111,7 +111,7 @@ class ParseLiveQueryTests: XCTestCase { let webSocketURL = components.url guard let client = try? ParseLiveQuery(serverURL: originalURL), - let defaultClient = ParseLiveQuery.getDefault() else { + let defaultClient = ParseLiveQuery.defaultClient else { XCTFail("Should be able to initialize a new client") return } @@ -148,7 +148,7 @@ class ParseLiveQueryTests: XCTestCase { } func testDeinitializingNewShouldNotEffectDefault() throws { - guard let defaultClient = ParseLiveQuery.getDefault() else { + guard let defaultClient = ParseLiveQuery.defaultClient else { XCTFail("Should be able to initialize a new client") return } @@ -160,7 +160,7 @@ class ParseLiveQueryTests: XCTestCase { } XCTAssertNotEqual(client, defaultClient) client = nil - XCTAssertNotNil(ParseLiveQuery.getDefault()) + XCTAssertNotNil(ParseLiveQuery.defaultClient) let expectation1 = XCTestExpectation(description: "Socket delegate") defaultClient.synchronizationQueue.asyncAfter(deadline: .now() + 2) { let socketDelegates = URLSession.liveQuery.delegates @@ -172,7 +172,7 @@ class ParseLiveQueryTests: XCTestCase { func testBecomingSocketAuthDelegate() throws { let delegate = TestDelegate() - guard let client = ParseLiveQuery.getDefault() else { + guard let client = ParseLiveQuery.defaultClient else { XCTFail("Should have unwrapped") return } @@ -313,7 +313,7 @@ class ParseLiveQueryTests: XCTestCase { } func testSocketNotOpenState() throws { - guard let client = ParseLiveQuery.getDefault() else { + guard let client = ParseLiveQuery.defaultClient else { XCTFail("Should be able to get client") return } @@ -325,7 +325,7 @@ class ParseLiveQueryTests: XCTestCase { } func testConnectedState() throws { - guard let client = ParseLiveQuery.getDefault(), + guard let client = ParseLiveQuery.defaultClient, let task = client.task else { XCTFail("Should be able to get client and task") return @@ -357,7 +357,7 @@ class ParseLiveQueryTests: XCTestCase { } func testDisconnectedState() throws { - guard let client = ParseLiveQuery.getDefault(), + guard let client = ParseLiveQuery.defaultClient, let task = client.task else { XCTFail("Should be able to get client and task") return @@ -379,7 +379,7 @@ class ParseLiveQueryTests: XCTestCase { } func testSocketDisconnectedState() throws { - guard let client = ParseLiveQuery.getDefault() else { + guard let client = ParseLiveQuery.defaultClient else { XCTFail("Should be able to get client") return } @@ -399,7 +399,7 @@ class ParseLiveQueryTests: XCTestCase { } func testUserClosedConnectionState() throws { - guard let client = ParseLiveQuery.getDefault() else { + guard let client = ParseLiveQuery.defaultClient else { XCTFail("Should be able to get client") return } @@ -423,7 +423,7 @@ class ParseLiveQueryTests: XCTestCase { } func testOpenSocket() throws { - guard let client = ParseLiveQuery.getDefault() else { + guard let client = ParseLiveQuery.defaultClient else { XCTFail("Should be able to get client") return } @@ -437,7 +437,7 @@ class ParseLiveQueryTests: XCTestCase { } func testCloseFromServer() throws { - guard let client = ParseLiveQuery.getDefault() else { + guard let client = ParseLiveQuery.defaultClient else { throw ParseError(code: .unknownError, message: "Should be able to get client") } @@ -554,7 +554,7 @@ class ParseLiveQueryTests: XCTestCase { } func testPingSocketNotEstablished() throws { - guard let client = ParseLiveQuery.getDefault() else { + guard let client = ParseLiveQuery.defaultClient else { XCTFail("Should be able to get client") return } @@ -576,7 +576,7 @@ class ParseLiveQueryTests: XCTestCase { } func testPing() throws { - guard let client = ParseLiveQuery.getDefault() else { + guard let client = ParseLiveQuery.defaultClient else { XCTFail("Should be able to get client") return } @@ -595,7 +595,7 @@ class ParseLiveQueryTests: XCTestCase { } func testRandomIdGenerator() throws { - guard let client = ParseLiveQuery.getDefault() else { + guard let client = ParseLiveQuery.defaultClient else { XCTFail("Should be able to get client") return } @@ -611,7 +611,7 @@ class ParseLiveQueryTests: XCTestCase { XCTFail("Should create subscription") return } - guard let client = ParseLiveQuery.getDefault() else { + guard let client = ParseLiveQuery.defaultClient else { XCTFail("Should be able to get client") return } @@ -625,7 +625,7 @@ class ParseLiveQueryTests: XCTestCase { } func pretendToBeConnected() throws { - guard let client = ParseLiveQuery.getDefault() else { + guard let client = ParseLiveQuery.defaultClient else { throw ParseError(code: .unknownError, message: "Should be able to get client") } @@ -643,7 +643,7 @@ class ParseLiveQueryTests: XCTestCase { XCTFail("Should create subscription") return } - guard let client = ParseLiveQuery.getDefault() else { + guard let client = ParseLiveQuery.defaultClient else { XCTFail("Should be able to get client") return } @@ -722,7 +722,7 @@ class ParseLiveQueryTests: XCTestCase { let handler = SubscriptionCallback(query: query) let subscription = try Query.subscribe(handler) - guard let client = ParseLiveQuery.getDefault() else { + guard let client = ParseLiveQuery.defaultClient else { XCTFail("Should be able to get client") return } @@ -786,7 +786,7 @@ class ParseLiveQueryTests: XCTestCase { let handler = SubscriptionCallback(query: query) var subscription = try Query.subscribe(handler) - guard let client = ParseLiveQuery.getDefault() else { + guard let client = ParseLiveQuery.defaultClient else { XCTFail("Should be able to get client") return } @@ -866,7 +866,7 @@ class ParseLiveQueryTests: XCTestCase { } func testServerRedirectResponse() throws { - guard let client = ParseLiveQuery.getDefault() else { + guard let client = ParseLiveQuery.defaultClient else { XCTFail("Should be able to get client") return } @@ -883,7 +883,7 @@ class ParseLiveQueryTests: XCTestCase { } func testServerErrorResponse() throws { - guard let client = ParseLiveQuery.getDefault() else { + guard let client = ParseLiveQuery.defaultClient else { XCTFail("Should be able to get client") return } @@ -910,7 +910,7 @@ class ParseLiveQueryTests: XCTestCase { } func testServerErrorResponseNoReconnect() throws { - guard let client = ParseLiveQuery.getDefault() else { + guard let client = ParseLiveQuery.defaultClient else { XCTFail("Should be able to get client") return } @@ -949,7 +949,7 @@ class ParseLiveQueryTests: XCTestCase { XCTFail("Should create subscription") return } - guard let client = ParseLiveQuery.getDefault() else { + guard let client = ParseLiveQuery.defaultClient else { XCTFail("Should be able to get client") return } @@ -1001,7 +1001,7 @@ class ParseLiveQueryTests: XCTestCase { XCTFail("Should create subscription") return } - guard let client = ParseLiveQuery.getDefault() else { + guard let client = ParseLiveQuery.defaultClient else { XCTFail("Should be able to get client") return } @@ -1053,7 +1053,7 @@ class ParseLiveQueryTests: XCTestCase { XCTFail("Should create subscription") return } - guard let client = ParseLiveQuery.getDefault() else { + guard let client = ParseLiveQuery.defaultClient else { XCTFail("Should be able to get client") return } @@ -1105,7 +1105,7 @@ class ParseLiveQueryTests: XCTestCase { XCTFail("Should create subscription") return } - guard let client = ParseLiveQuery.getDefault() else { + guard let client = ParseLiveQuery.defaultClient else { XCTFail("Should be able to get client") return } @@ -1157,7 +1157,7 @@ class ParseLiveQueryTests: XCTestCase { XCTFail("Should create subscription") return } - guard let client = ParseLiveQuery.getDefault() else { + guard let client = ParseLiveQuery.defaultClient else { XCTFail("Should be able to get client") return } @@ -1209,7 +1209,7 @@ class ParseLiveQueryTests: XCTestCase { XCTFail("Should create subscription") return } - guard let client = ParseLiveQuery.getDefault() else { + guard let client = ParseLiveQuery.defaultClient else { XCTFail("Should be able to get client") return } @@ -1297,7 +1297,7 @@ class ParseLiveQueryTests: XCTestCase { XCTFail("Should create subscription") return } - guard let client = ParseLiveQuery.getDefault() else { + guard let client = ParseLiveQuery.defaultClient else { XCTFail("Should be able to get client") return } @@ -1383,7 +1383,7 @@ class ParseLiveQueryTests: XCTestCase { let query = GameScore.query("points" > 9) let handler = SubscriptionCallback(query: query) let subscription = try Query.subscribe(handler) - guard let client = ParseLiveQuery.getDefault() else { + guard let client = ParseLiveQuery.defaultClient else { XCTFail("Should be able to get client") return } @@ -1426,7 +1426,7 @@ class ParseLiveQueryTests: XCTestCase { let query = GameScore.query("points" > 9) let handler = SubscriptionCallback(query: query) let subscription = try Query.subscribe(handler) - guard let client = ParseLiveQuery.getDefault() else { + guard let client = ParseLiveQuery.defaultClient else { XCTFail("Should be able to get client") return } @@ -1469,7 +1469,7 @@ class ParseLiveQueryTests: XCTestCase { let query = GameScore.query("points" > 9) let handler = SubscriptionCallback(query: query) let subscription = try Query.subscribe(handler) - guard let client = ParseLiveQuery.getDefault() else { + guard let client = ParseLiveQuery.defaultClient else { XCTFail("Should be able to get client") return } @@ -1512,7 +1512,7 @@ class ParseLiveQueryTests: XCTestCase { let query = GameScore.query("points" > 9) let handler = SubscriptionCallback(query: query) let subscription = try Query.subscribe(handler) - guard let client = ParseLiveQuery.getDefault() else { + guard let client = ParseLiveQuery.defaultClient else { XCTFail("Should be able to get client") return } @@ -1555,7 +1555,7 @@ class ParseLiveQueryTests: XCTestCase { let query = GameScore.query("points" > 9) let handler = SubscriptionCallback(query: query) let subscription = try Query.subscribe(handler) - guard let client = ParseLiveQuery.getDefault() else { + guard let client = ParseLiveQuery.defaultClient else { XCTFail("Should be able to get client") return } @@ -1598,7 +1598,7 @@ class ParseLiveQueryTests: XCTestCase { let query = GameScore.query("points" > 9) let handler = SubscriptionCallback(query: query) let subscription = try Query.subscribe(handler) - guard let client = ParseLiveQuery.getDefault() else { + guard let client = ParseLiveQuery.defaultClient else { XCTFail("Should be able to get client") return } @@ -1668,7 +1668,7 @@ class ParseLiveQueryTests: XCTestCase { let query = GameScore.query("points" > 9) let handler = SubscriptionCallback(query: query) let subscription = try Query.subscribe(handler) - guard let client = ParseLiveQuery.getDefault() else { + guard let client = ParseLiveQuery.defaultClient else { XCTFail("Should be able to get client") return } From 5d6cf812ab6b0bafe81d700934a74865994934aa Mon Sep 17 00:00:00 2001 From: Corey Baker Date: Tue, 8 Feb 2022 09:55:38 -0500 Subject: [PATCH 2/3] nit --- Sources/ParseSwift/LiveQuery/ParseLiveQuery.swift | 1 + 1 file changed, 1 insertion(+) diff --git a/Sources/ParseSwift/LiveQuery/ParseLiveQuery.swift b/Sources/ParseSwift/LiveQuery/ParseLiveQuery.swift index d0f0dbde6..35a46bb6a 100644 --- a/Sources/ParseSwift/LiveQuery/ParseLiveQuery.swift +++ b/Sources/ParseSwift/LiveQuery/ParseLiveQuery.swift @@ -223,6 +223,7 @@ Not attempting to open ParseLiveQuery socket anymore } } +// MARK: Client Intents extension ParseLiveQuery { /// Current LiveQuery client. From 31c1373223a4bcb0b07c3a6f7d516198b7520d1b Mon Sep 17 00:00:00 2001 From: Corey Baker Date: Tue, 8 Feb 2022 10:00:07 -0500 Subject: [PATCH 3/3] change log --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 62814513f..90cb363b1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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)