LiveQuerySubscription.js 4.3 KB

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