forked from parse-community/parse-server
-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathSubscription.js
More file actions
55 lines (47 loc) · 1.5 KB
/
Subscription.js
File metadata and controls
55 lines (47 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import {matchesQuery, queryHash} from './QueryTools';
import PLog from './PLog';
export type FlattenedObjectData = { [attr: string]: any };
export type QueryData = { [attr: string]: any };
class Subscription {
// It is query condition eg query.where
query: QueryData;
className: string;
hash: string;
clientRequestIds: Object;
constructor(className: string, query: QueryData, queryHash: string) {
this.className = className;
this.query = query;
this.hash = queryHash;
this.clientRequestIds = new Map();
}
addClientSubscription(clientId: number, requestId: number): void {
if (!this.clientRequestIds.has(clientId)) {
this.clientRequestIds.set(clientId, []);
}
let requestIds = this.clientRequestIds.get(clientId);
requestIds.push(requestId);
}
deleteClientSubscription(clientId: number, requestId: number): void {
let requestIds = this.clientRequestIds.get(clientId);
if (typeof requestIds === 'undefined') {
PLog.error('Can not find client %d to delete', clientId);
return;
}
let index = requestIds.indexOf(requestId);
if (index < 0) {
PLog.error('Can not find client %d subscription %d to delete', clientId, requestId);
return;
}
requestIds.splice(index, 1);
// Delete client reference if it has no subscription
if (requestIds.length == 0) {
this.clientRequestIds.delete(clientId);
}
}
hasSubscribingClient(): boolean {
return this.clientRequestIds.size > 0;
}
}
export {
Subscription
}