Skip to content

fix: Switch LiveQuery listen to watch #48

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 1 commit into from
Jan 18, 2023
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
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions Sources/ParseSwift/LiveQuery/Messages.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ struct SubscribeQuery: Encodable {
let className: String
let `where`: QueryWhere
let fields: [String]?
let listen: [String]?
let watch: [String]?
}

struct SubscribeMessage<T: ParseObject>: LiveQueryable, Encodable {
Expand All @@ -71,7 +71,7 @@ struct SubscribeMessage<T: ParseObject>: 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
}
Expand Down
26 changes: 13 additions & 13 deletions Sources/ParseSwift/Types/Query.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public struct Query<T>: ParseTypeable where T: ParseObject {
internal var distinct: String?
internal var pipeline: [[String: AnyCodable]]?
internal var fields: Set<String>?
internal var listen: Set<String>?
internal var watch: Set<String>?
var endpoint: API.Endpoint {
.objects(className: T.className)
}
Expand Down Expand Up @@ -416,11 +416,11 @@ public struct Query<T>: 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.
Expand All @@ -429,16 +429,16 @@ public struct Query<T>: 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<T> {
self.listen(keys)
public func watch(_ keys: String...) -> Query<T> {
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.
Expand All @@ -447,12 +447,12 @@ public struct Query<T>: 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<T> {
public func watch(_ keys: [String]) -> Query<T> {
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
}
Expand Down
28 changes: 14 additions & 14 deletions Tests/ParseSwiftTests/ParseLiveQueryTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 {
Expand Down