LiveQuerySubscription.js 4.3 KB

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