Add LiveQuery

This commit is contained in:
wangmengyan95
2016-03-10 14:27:00 -08:00
parent cf3606246f
commit 555e25bf33
33 changed files with 3580 additions and 48 deletions

View File

@@ -0,0 +1,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
}