LiveQuerySubscription.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. "use strict";
  2. var _Object$defineProperty = require("@babel/runtime-corejs3/core-js-stable/object/define-property");
  3. var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault");
  4. _Object$defineProperty(exports, "__esModule", {
  5. value: true
  6. });
  7. exports.default = void 0;
  8. var _defineProperty2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/defineProperty"));
  9. var _CoreManager = _interopRequireDefault(require("./CoreManager"));
  10. var _promiseUtils = require("./promiseUtils");
  11. /**
  12. * Creates a new LiveQuery Subscription.
  13. * <a href="https://nodejs.org/api/events.html#events_class_eventemitter">cloud functions</a>.
  14. *
  15. * <p>Response Object - Contains data from the client that made the request
  16. * <ul>
  17. * <li>clientId</li>
  18. * <li>installationId - requires Parse Server 4.0.0+</li>
  19. * </ul>
  20. * </p>
  21. *
  22. * <p>Open Event - When you call query.subscribe(), we send a subscribe request to
  23. * the LiveQuery server, when we get the confirmation from the LiveQuery server,
  24. * this event will be emitted. When the client loses WebSocket connection to the
  25. * LiveQuery server, we will try to auto reconnect the LiveQuery server. If we
  26. * reconnect the LiveQuery server and successfully resubscribe the ParseQuery,
  27. * you'll also get this event.
  28. *
  29. * <pre>
  30. * subscription.on('open', (response) => {
  31. *
  32. * });</pre></p>
  33. *
  34. * <p>Create Event - When a new ParseObject is created and it fulfills the ParseQuery you subscribe,
  35. * you'll get this event. The object is the ParseObject which is created.
  36. *
  37. * <pre>
  38. * subscription.on('create', (object, response) => {
  39. *
  40. * });</pre></p>
  41. *
  42. * <p>Update Event - When an existing ParseObject (original) which fulfills the ParseQuery you subscribe
  43. * is updated (The ParseObject fulfills the ParseQuery before and after changes),
  44. * you'll get this event. The object is the ParseObject which is updated.
  45. * Its content is the latest value of the ParseObject.
  46. *
  47. * Parse-Server 3.1.3+ Required for original object parameter
  48. *
  49. * <pre>
  50. * subscription.on('update', (object, original, response) => {
  51. *
  52. * });</pre></p>
  53. *
  54. * <p>Enter Event - When an existing ParseObject's (original) old value doesn't fulfill the ParseQuery
  55. * but its new value fulfills the ParseQuery, you'll get this event. The object is the
  56. * ParseObject which enters the ParseQuery. Its content is the latest value of the ParseObject.
  57. *
  58. * Parse-Server 3.1.3+ Required for original object parameter
  59. *
  60. * <pre>
  61. * subscription.on('enter', (object, original, response) => {
  62. *
  63. * });</pre></p>
  64. *
  65. *
  66. * <p>Update Event - When an existing ParseObject's old value fulfills the ParseQuery but its new value
  67. * doesn't fulfill the ParseQuery, you'll get this event. The object is the ParseObject
  68. * which leaves the ParseQuery. Its content is the latest value of the ParseObject.
  69. *
  70. * <pre>
  71. * subscription.on('leave', (object, response) => {
  72. *
  73. * });</pre></p>
  74. *
  75. *
  76. * <p>Delete Event - When an existing ParseObject which fulfills the ParseQuery is deleted, you'll
  77. * get this event. The object is the ParseObject which is deleted.
  78. *
  79. * <pre>
  80. * subscription.on('delete', (object, response) => {
  81. *
  82. * });</pre></p>
  83. *
  84. *
  85. * <p>Close Event - When the client loses the WebSocket connection to the LiveQuery
  86. * server and we stop receiving events, you'll get this event.
  87. *
  88. * <pre>
  89. * subscription.on('close', () => {
  90. *
  91. * });</pre></p>
  92. */
  93. class Subscription {
  94. /*
  95. * @param {string | number} id - subscription id
  96. * @param {string} query - query to subscribe to
  97. * @param {string} sessionToken - optional session token
  98. */
  99. constructor(id, query, sessionToken) {
  100. var _this = this;
  101. (0, _defineProperty2.default)(this, "id", void 0);
  102. (0, _defineProperty2.default)(this, "query", void 0);
  103. (0, _defineProperty2.default)(this, "sessionToken", void 0);
  104. (0, _defineProperty2.default)(this, "subscribePromise", void 0);
  105. (0, _defineProperty2.default)(this, "unsubscribePromise", void 0);
  106. (0, _defineProperty2.default)(this, "subscribed", void 0);
  107. (0, _defineProperty2.default)(this, "emitter", void 0);
  108. (0, _defineProperty2.default)(this, "on", void 0);
  109. (0, _defineProperty2.default)(this, "emit", void 0);
  110. this.id = id;
  111. this.query = query;
  112. this.sessionToken = sessionToken;
  113. this.subscribePromise = (0, _promiseUtils.resolvingPromise)();
  114. this.unsubscribePromise = (0, _promiseUtils.resolvingPromise)();
  115. this.subscribed = false;
  116. const EventEmitter = _CoreManager.default.getEventEmitter();
  117. this.emitter = new EventEmitter();
  118. this.on = (eventName, listener) => this.emitter.on(eventName, listener);
  119. this.emit = function (eventName) {
  120. for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
  121. args[_key - 1] = arguments[_key];
  122. }
  123. return _this.emitter.emit(eventName, ...args);
  124. };
  125. // adding listener so process does not crash
  126. // best practice is for developer to register their own listener
  127. this.on('error', () => {});
  128. }
  129. /**
  130. * Close the subscription
  131. *
  132. * @returns {Promise}
  133. */
  134. unsubscribe() {
  135. return _CoreManager.default.getLiveQueryController().getDefaultLiveQueryClient().then(liveQueryClient => {
  136. this.emit('close');
  137. return liveQueryClient.unsubscribe(this);
  138. });
  139. }
  140. }
  141. var _default = exports.default = Subscription;