LiveQueryController.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = exports.LiveQueryController = void 0;
  6. var _ParseCloudCodePublisher = require("../LiveQuery/ParseCloudCodePublisher");
  7. var _Options = require("../Options");
  8. var _triggers = require("./../triggers");
  9. class LiveQueryController {
  10. constructor(config) {
  11. // If config is empty, we just assume no classs needs to be registered as LiveQuery
  12. if (!config || !config.classNames) {
  13. this.classNames = new Set();
  14. } else if (config.classNames instanceof Array) {
  15. const classNames = config.classNames.map(name => {
  16. const _name = (0, _triggers.getClassName)(name);
  17. return new RegExp(`^${_name}$`);
  18. });
  19. this.classNames = new Set(classNames);
  20. } else {
  21. throw 'liveQuery.classes should be an array of string';
  22. }
  23. this.liveQueryPublisher = new _ParseCloudCodePublisher.ParseCloudCodePublisher(config);
  24. }
  25. connect() {
  26. return this.liveQueryPublisher.connect();
  27. }
  28. onAfterSave(className, currentObject, originalObject, classLevelPermissions) {
  29. if (!this.hasLiveQuery(className)) {
  30. return;
  31. }
  32. const req = this._makePublisherRequest(currentObject, originalObject, classLevelPermissions);
  33. this.liveQueryPublisher.onCloudCodeAfterSave(req);
  34. }
  35. onAfterDelete(className, currentObject, originalObject, classLevelPermissions) {
  36. if (!this.hasLiveQuery(className)) {
  37. return;
  38. }
  39. const req = this._makePublisherRequest(currentObject, originalObject, classLevelPermissions);
  40. this.liveQueryPublisher.onCloudCodeAfterDelete(req);
  41. }
  42. hasLiveQuery(className) {
  43. for (const name of this.classNames) {
  44. if (name.test(className)) {
  45. return true;
  46. }
  47. }
  48. return false;
  49. }
  50. clearCachedRoles(user) {
  51. if (!user) {
  52. return;
  53. }
  54. return this.liveQueryPublisher.onClearCachedRoles(user);
  55. }
  56. _makePublisherRequest(currentObject, originalObject, classLevelPermissions) {
  57. const req = {
  58. object: currentObject
  59. };
  60. if (currentObject) {
  61. req.original = originalObject;
  62. }
  63. if (classLevelPermissions) {
  64. req.classLevelPermissions = classLevelPermissions;
  65. }
  66. return req;
  67. }
  68. }
  69. exports.LiveQueryController = LiveQueryController;
  70. var _default = exports.default = LiveQueryController;
  71. //# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJuYW1lcyI6WyJfUGFyc2VDbG91ZENvZGVQdWJsaXNoZXIiLCJyZXF1aXJlIiwiX09wdGlvbnMiLCJfdHJpZ2dlcnMiLCJMaXZlUXVlcnlDb250cm9sbGVyIiwiY29uc3RydWN0b3IiLCJjb25maWciLCJjbGFzc05hbWVzIiwiU2V0IiwiQXJyYXkiLCJtYXAiLCJuYW1lIiwiX25hbWUiLCJnZXRDbGFzc05hbWUiLCJSZWdFeHAiLCJsaXZlUXVlcnlQdWJsaXNoZXIiLCJQYXJzZUNsb3VkQ29kZVB1Ymxpc2hlciIsImNvbm5lY3QiLCJvbkFmdGVyU2F2ZSIsImNsYXNzTmFtZSIsImN1cnJlbnRPYmplY3QiLCJvcmlnaW5hbE9iamVjdCIsImNsYXNzTGV2ZWxQZXJtaXNzaW9ucyIsImhhc0xpdmVRdWVyeSIsInJlcSIsIl9tYWtlUHVibGlzaGVyUmVxdWVzdCIsIm9uQ2xvdWRDb2RlQWZ0ZXJTYXZlIiwib25BZnRlckRlbGV0ZSIsIm9uQ2xvdWRDb2RlQWZ0ZXJEZWxldGUiLCJ0ZXN0IiwiY2xlYXJDYWNoZWRSb2xlcyIsInVzZXIiLCJvbkNsZWFyQ2FjaGVkUm9sZXMiLCJvYmplY3QiLCJvcmlnaW5hbCIsImV4cG9ydHMiLCJfZGVmYXVsdCIsImRlZmF1bHQiXSwic291cmNlcyI6WyIuLi8uLi9zcmMvQ29udHJvbGxlcnMvTGl2ZVF1ZXJ5Q29udHJvbGxlci5qcyJdLCJzb3VyY2VzQ29udGVudCI6WyJpbXBvcnQgeyBQYXJzZUNsb3VkQ29kZVB1Ymxpc2hlciB9IGZyb20gJy4u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