Skip to content

Commit 2ff5c99

Browse files
committed
feat(live-query): add LiveQuery.pause and LiveQuery.resume
1 parent ff7244e commit 2ff5c99

File tree

2 files changed

+33
-7
lines changed

2 files changed

+33
-7
lines changed

src/live-query.js

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@ const subscribe = (queryJSON, subscriptionId) =>
1515
});
1616

1717
module.exports = AV => {
18+
const requireRealtime = () => {
19+
if (!AV._config.realtime) {
20+
throw new Error(
21+
'LiveQuery not supported. Please use the LiveQuery bundle. https://url.leanapp.cn/enable-live-query'
22+
);
23+
}
24+
};
1825
/**
1926
* @class
2027
* A LiveQuery, created by {@link AV.Query#subscribe} is an EventEmitter notifies changes of the Query.
@@ -105,17 +112,15 @@ module.exports = AV => {
105112
});
106113
},
107114
},
115+
/** @lends AV.LiveQuery */
108116
{
109-
init: (
117+
init(
110118
query,
111119
{
112120
subscriptionId: userDefinedSubscriptionId = AV._getSubscriptionId(),
113121
} = {}
114-
) => {
115-
if (!AV._config.realtime)
116-
throw new Error(
117-
'LiveQuery not supported. Please use the LiveQuery bundle. https://url.leanapp.cn/enable-live-query'
118-
);
122+
) {
123+
requireRealtime();
119124
if (!(query instanceof AV.Query))
120125
throw new TypeError('LiveQuery must be inited with a Query');
121126
return Promise.resolve(userDefinedSubscriptionId).then(subscriptionId =>
@@ -147,6 +152,24 @@ module.exports = AV => {
147152
})
148153
);
149154
},
155+
/**
156+
* Pause the LiveQuery connection. This is useful to deactivate the SDK when the app is swtiched to background.
157+
* @static
158+
* @return void
159+
*/
160+
pause() {
161+
requireRealtime();
162+
return AV._config.realtime.pause();
163+
},
164+
/**
165+
* Resume the LiveQuery connection. All subscriptions will be restored after reconnection.
166+
* @static
167+
* @return void
168+
*/
169+
resume() {
170+
requireRealtime();
171+
return AV._config.realtime.resume();
172+
},
150173
}
151174
);
152175
};

storage.d.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -544,7 +544,10 @@ export class Query<T extends Queriable> extends BaseQuery<T> {
544544
subscribe(options?: { subscriptionId?: string }): Promise<LiveQuery<T>>;
545545
}
546546

547-
declare class LiveQuery<T> extends EventEmitter<LiveQueryEvent<T>> {
547+
export class LiveQuery<T> extends EventEmitter<LiveQueryEvent<T>> {
548+
static pause(): void;
549+
static resume(): void;
550+
548551
unsubscribe(): Promise<void>;
549552
}
550553

0 commit comments

Comments
 (0)