Skip to content

Commit 33f8fbe

Browse files
committed
docs: LiveQuery
1 parent 6f9d1b2 commit 33f8fbe

File tree

3 files changed

+59
-3
lines changed

3 files changed

+59
-3
lines changed

src/live-query.js

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@ const { inherits } = require('./utils');
44
const { request } = require('./request');
55

66
module.exports = (AV) => {
7-
const LiveQuery = AV.LiveQuery = inherits(EventEmitter, {
7+
/**
8+
* @class
9+
* A LiveQuery, created by {@link AV.Query#subscribe} is an EventEmitter notifies changes of the Query.
10+
* @since 3.0.0
11+
*/
12+
AV.LiveQuery = inherits(EventEmitter, /** @lends AV.LiveQuery.prototype */ {
813
constructor(id, client) {
914
EventEmitter.apply(this);
1015
this.id = id;
@@ -24,12 +29,43 @@ module.exports = (AV) => {
2429
__type: object.className === '_File' ? 'File' : 'Object',
2530
}, object));
2631
if (updatedKeys) {
32+
/**
33+
* An existing AV.Object which fulfills the Query you subscribe is updated.
34+
* @event AV.LiveQuery#update
35+
* @param {AV.Object|AV.File} target updated object
36+
* @param {String[]} updatedKeys updated keys
37+
*/
2738
this.emit(op, target, updatedKeys);
2839
} else {
40+
/**
41+
* A new AV.Object which fulfills the Query you subscribe is created.
42+
* @event AV.LiveQuery#create
43+
* @param {AV.Object|AV.File} target updated object
44+
*/
45+
/**
46+
* An existing AV.Object which fulfills the Query you subscribe is deleted.
47+
* @event AV.LiveQuery#delete
48+
* @param {AV.Object|AV.File} target updated object
49+
*/
50+
/**
51+
* An existing AV.Object which doesn't fulfill the Query is updated and now it fulfills the Query.
52+
* @event AV.LiveQuery#enter
53+
* @param {AV.Object|AV.File} target updated object
54+
*/
55+
/**
56+
* An existing AV.Object which fulfills the Query is updated and now it doesn't fulfill the Query.
57+
* @event AV.LiveQuery#leave
58+
* @param {AV.Object|AV.File} target updated object
59+
*/
2960
this.emit(op, target);
3061
}
3162
});
3263
},
64+
/**
65+
* unsubscribe the query
66+
*
67+
* @return {Promise}
68+
*/
3369
unsubscribe() {
3470
this._client.deregister(this);
3571
return request({
@@ -64,7 +100,7 @@ module.exports = (AV) => {
64100
}).then(({
65101
query_id: queryId,
66102
}) => AV._config.realtime.createLiveQueryClient(subscriptionId)
67-
.then(liveQueryClient => new LiveQuery(queryId, liveQueryClient))
103+
.then(liveQueryClient => new AV.LiveQuery(queryId, liveQueryClient))
68104
)
69105
);
70106
},

src/query.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1014,6 +1014,14 @@ module.exports = function(AV) {
10141014
});
10151015
},
10161016

1017+
/**
1018+
* Subscribe the changes of this query.
1019+
*
1020+
* LiveQuery is not included in the default bundle: {@link https://url.leanapp.cn/enable-live-query}.
1021+
*
1022+
* @since 3.0.0
1023+
* @return {AV.LiveQuery} An eventemitter which can be used to get LiveQuery updates;
1024+
*/
10171025
subscribe(options) {
10181026
return AV.LiveQuery.init(this, options);
10191027
},

storage.d.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@ interface AsyncIterator<T> {
22
next(): Promise<IteratorResult<T>>
33
}
44

5+
declare class EventEmitter {
6+
on(evt: string, listener: Function): EventEmitter;
7+
once(evt: string, listener: Function): EventEmitter;
8+
off(evt: string, listener: Function): EventEmitter;
9+
emit(evt: string, ...args: any[]): Boolean;
10+
}
11+
512
declare namespace AV {
613

714
export var applicationId: string;
@@ -469,7 +476,12 @@ declare namespace AV {
469476
withinKilometers(key: string, point: GeoPoint, maxDistance: number): Query;
470477
withinMiles(key: string, point: GeoPoint, maxDistance: number): Query;
471478
withinRadians(key: string, point: GeoPoint, maxDistance: number): Query;
472-
scan<T>(options?:{ orderedBy?: string, batchSize?: number }, authOptions?: AuthOptions): AsyncIterator<T>
479+
scan<T>(options?:{ orderedBy?: string, batchSize?: number }, authOptions?: AuthOptions): AsyncIterator<T>;
480+
subscribe(options?:{ subscriptionId?: string }): Promise<LiveQuery>;
481+
}
482+
483+
class LiveQuery extends EventEmitter {
484+
unsubscribe(): Promise<void>;
473485
}
474486

475487
export namespace Query {

0 commit comments

Comments
 (0)