diff --git a/CHANGELOG.md b/CHANGELOG.md index e4d5d2789..99423dd70 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,7 +10,7 @@ __New features__ -* Adds the the ability to listen to particular keys with LiveQueries. Requires Parse-Server 6.0.0 ([#46](https://github.com/netreconlab/Parse-Swift/pull/46)), thanks to [Corey Baker](https://github.com/cbaker6). +* Adds the the ability to watch particular keys with LiveQueries. Requires Parse-Server 6.0.0 ([#47](https://github.com/netreconlab/Parse-Swift/pull/47)), thanks to [Corey Baker](https://github.com/cbaker6). * (Breaking Change) Added a new ParseHealth.Status enum to support Parse Server. Developers can access the new status values (Status.initialized, Status.starting) diff --git a/Sources/ParseSwift/LiveQuery/Messages.swift b/Sources/ParseSwift/LiveQuery/Messages.swift index 7510b7a72..686f91d3a 100644 --- a/Sources/ParseSwift/LiveQuery/Messages.swift +++ b/Sources/ParseSwift/LiveQuery/Messages.swift @@ -49,7 +49,7 @@ struct SubscribeQuery: Encodable { let className: String let `where`: QueryWhere let fields: [String]? - let listen: [String]? + let watch: [String]? } struct SubscribeMessage: LiveQueryable, Encodable { @@ -71,7 +71,7 @@ struct SubscribeMessage: LiveQueryable, Encodable { self.query = SubscribeQuery(className: query.className, where: query.where, fields: query.fields?.sorted() ?? query.keys?.sorted(), - listen: query.listen?.sorted()) + watch: query.watch?.sorted()) } self.sessionToken = BaseParseUser.currentContainer?.sessionToken } diff --git a/Sources/ParseSwift/Types/Query.swift b/Sources/ParseSwift/Types/Query.swift index 741d1eca9..5c89b10c9 100644 --- a/Sources/ParseSwift/Types/Query.swift +++ b/Sources/ParseSwift/Types/Query.swift @@ -31,7 +31,7 @@ public struct Query: ParseTypeable where T: ParseObject { internal var distinct: String? internal var pipeline: [[String: AnyCodable]]? internal var fields: Set? - internal var listen: Set? + internal var watch: Set? var endpoint: API.Endpoint { .objects(className: T.className) } @@ -416,11 +416,11 @@ public struct Query: ParseTypeable where T: ParseObject { } /** - A variadic list of keys to listen to mutations on when using `ParseLiveQuery`. + A variadic list of keys to watch mutations on when using `ParseLiveQuery`. - Suppose the `ParseObject` Player contains three fields name, id and age. + Suppose the `ParseObject` Player contains three fields: name, position, and age. If you are only interested in the change of the **name** key, you can set - `query.listen` to **name**. In this situation, when the change of a + `query.watch` to **name**. In this situation, when the change of a Player `ParseObject` fulfills the subscription and the **name** key is updated, only the name field will be sent to the clients instead of the full Player `ParseObject`. If this is called multiple times, then all of the keys specified in each of the calls will be received. @@ -429,16 +429,16 @@ public struct Query: ParseTypeable where T: ParseObject { - parameter keys: A variadic list of fields to receive back instead of the whole `ParseObject`. - returns: The mutated instance of query for easy chaining. */ - public func listen(_ keys: String...) -> Query { - self.listen(keys) + public func watch(_ keys: String...) -> Query { + self.watch(keys) } /** - A list of keys to listen to mutations on when using `ParseLiveQuery`. + A list of keys to watch mutations on when using `ParseLiveQuery`. - Suppose the `ParseObject` Player contains three fields name, id and age. + Suppose the `ParseObject` Player contains three fields: name, position, and age. If you are only interested in the change of the **name** key, you can set - `query.listen` to **name**. In this situation, when the change of a + `query.watch` to **name**. In this situation, when the change of a Player `ParseObject` fulfills the subscription and the **name** key is updated, only the name field will be sent to the clients instead of the full Player `ParseObject`. If this is called multiple times, then all of the keys specified in each of the calls will be received. @@ -447,12 +447,12 @@ public struct Query: ParseTypeable where T: ParseObject { - parameter keys: An array of keys to lisen to instead of the whole `ParseObject`. - returns: The mutated instance of query for easy chaining. */ - public func listen(_ keys: [String]) -> Query { + public func watch(_ keys: [String]) -> Query { var mutableQuery = self - if mutableQuery.listen != nil { - mutableQuery.listen = mutableQuery.listen?.union(keys) + if mutableQuery.watch != nil { + mutableQuery.watch = mutableQuery.watch?.union(keys) } else { - mutableQuery.listen = Set(keys) + mutableQuery.watch = Set(keys) } return mutableQuery } diff --git a/Tests/ParseSwiftTests/ParseLiveQueryTests.swift b/Tests/ParseSwiftTests/ParseLiveQueryTests.swift index ec49e5baf..f92e7545a 100644 --- a/Tests/ParseSwiftTests/ParseLiveQueryTests.swift +++ b/Tests/ParseSwiftTests/ParseLiveQueryTests.swift @@ -263,9 +263,9 @@ class ParseLiveQueryTests: XCTestCase { func testSubscribeMessageListenEncoding() throws { // swiftlint:disable:next line_length - let expected = "{\"op\":\"subscribe\",\"query\":{\"className\":\"GameScore\",\"listen\":[\"hello\",\"points\"],\"where\":{\"points\":{\"$gt\":9}}},\"requestId\":1}" + let expected = "{\"op\":\"subscribe\",\"query\":{\"className\":\"GameScore\",\"watch\":[\"hello\",\"points\"],\"where\":{\"points\":{\"$gt\":9}}},\"requestId\":1}" let query = GameScore.query("points" > 9) - .listen(["hello", "points"]) + .watch(["hello", "points"]) let message = SubscribeMessage(operation: .subscribe, requestId: RequestId(value: 1), query: query, @@ -277,23 +277,23 @@ class ParseLiveQueryTests: XCTestCase { } func testListenKeys() throws { - var query = GameScore.query.listen(["yolo"]) - XCTAssertEqual(query.listen?.count, 1) - XCTAssertEqual(query.listen?.first, "yolo") + var query = GameScore.query.watch(["yolo"]) + XCTAssertEqual(query.watch?.count, 1) + XCTAssertEqual(query.watch?.first, "yolo") - query = query.listen(["hello", "wow"]) - XCTAssertEqual(query.listen?.count, 3) - XCTAssertEqual(query.listen, ["yolo", "hello", "wow"]) + query = query.watch(["hello", "wow"]) + XCTAssertEqual(query.watch?.count, 3) + XCTAssertEqual(query.watch, ["yolo", "hello", "wow"]) } func testListenKeysVariadic() throws { - var query = GameScore.query.listen("yolo") - XCTAssertEqual(query.listen?.count, 1) - XCTAssertEqual(query.listen?.first, "yolo") + var query = GameScore.query.watch("yolo") + XCTAssertEqual(query.watch?.count, 1) + XCTAssertEqual(query.watch?.first, "yolo") - query = query.listen("hello", "wow") - XCTAssertEqual(query.listen?.count, 3) - XCTAssertEqual(query.listen, ["yolo", "hello", "wow"]) + query = query.watch("hello", "wow") + XCTAssertEqual(query.watch?.count, 3) + XCTAssertEqual(query.watch, ["yolo", "hello", "wow"]) } func testRedirectResponseDecoding() throws {