2016-03-10 14:27:00 -08:00
|
|
|
import { ParseCloudCodePublisher } from '../LiveQuery/ParseCloudCodePublisher';
|
2017-10-23 08:43:05 -04:00
|
|
|
import { LiveQueryOptions } from '../Options';
|
2016-03-10 14:27:00 -08:00
|
|
|
export class LiveQueryController {
|
|
|
|
|
classNames: any;
|
|
|
|
|
liveQueryPublisher: any;
|
|
|
|
|
|
2017-10-23 08:43:05 -04:00
|
|
|
constructor(config: ?LiveQueryOptions) {
|
2016-03-10 14:27:00 -08:00
|
|
|
// If config is empty, we just assume no classs needs to be registered as LiveQuery
|
|
|
|
|
if (!config || !config.classNames) {
|
|
|
|
|
this.classNames = new Set();
|
|
|
|
|
} else if (config.classNames instanceof Array) {
|
2021-02-12 20:18:49 +11:00
|
|
|
const classNames = config.classNames.map(name => new RegExp('^' + name + '$'));
|
2021-01-20 01:19:11 +03:00
|
|
|
this.classNames = new Set(classNames);
|
2016-03-10 14:27:00 -08:00
|
|
|
} else {
|
2018-09-01 13:58:06 -04:00
|
|
|
throw 'liveQuery.classes should be an array of string';
|
2016-03-10 14:27:00 -08:00
|
|
|
}
|
|
|
|
|
this.liveQueryPublisher = new ParseCloudCodePublisher(config);
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-17 17:53:49 -04:00
|
|
|
onAfterSave(
|
|
|
|
|
className: string,
|
|
|
|
|
currentObject: any,
|
|
|
|
|
originalObject: any,
|
|
|
|
|
classLevelPermissions: ?any
|
|
|
|
|
) {
|
2016-03-10 14:27:00 -08:00
|
|
|
if (!this.hasLiveQuery(className)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-10-25 15:06:58 -05:00
|
|
|
const req = this._makePublisherRequest(currentObject, originalObject, classLevelPermissions);
|
2016-03-10 14:27:00 -08:00
|
|
|
this.liveQueryPublisher.onCloudCodeAfterSave(req);
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-17 17:53:49 -04:00
|
|
|
onAfterDelete(
|
|
|
|
|
className: string,
|
|
|
|
|
currentObject: any,
|
|
|
|
|
originalObject: any,
|
|
|
|
|
classLevelPermissions: any
|
|
|
|
|
) {
|
2016-03-10 14:27:00 -08:00
|
|
|
if (!this.hasLiveQuery(className)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-10-25 15:06:58 -05:00
|
|
|
const req = this._makePublisherRequest(currentObject, originalObject, classLevelPermissions);
|
2016-03-10 14:27:00 -08:00
|
|
|
this.liveQueryPublisher.onCloudCodeAfterDelete(req);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
hasLiveQuery(className: string): boolean {
|
2021-01-20 01:19:11 +03:00
|
|
|
for (const name of this.classNames) {
|
|
|
|
|
if (name.test(className)) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
2016-03-10 14:27:00 -08:00
|
|
|
}
|
|
|
|
|
|
2020-10-25 15:06:58 -05:00
|
|
|
_makePublisherRequest(currentObject: any, originalObject: any, classLevelPermissions: ?any): any {
|
2016-12-07 15:17:05 -08:00
|
|
|
const req = {
|
2018-09-01 13:58:06 -04:00
|
|
|
object: currentObject,
|
2016-03-10 14:27:00 -08:00
|
|
|
};
|
|
|
|
|
if (currentObject) {
|
|
|
|
|
req.original = originalObject;
|
|
|
|
|
}
|
2018-10-17 17:53:49 -04:00
|
|
|
if (classLevelPermissions) {
|
|
|
|
|
req.classLevelPermissions = classLevelPermissions;
|
|
|
|
|
}
|
2016-03-10 14:27:00 -08:00
|
|
|
return req;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default LiveQueryController;
|