LiveQuerySubscription.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. * Copyright (c) 2015-present, Parse, LLC.
  16. * All rights reserved.
  17. *
  18. * This source code is licensed under the BSD-style license found in the
  19. * LICENSE file in the root directory of this source tree. An additional grant
  20. * of patent rights can be found in the PATENTS file in the same directory.
  21. *
  22. */
  23. /**
  24. * Creates a new LiveQuery Subscription.
  25. * Extends events.EventEmitter
  26. * <a href="https://nodejs.org/api/events.html#events_class_eventemitter">cloud functions</a>.
  27. *
  28. * <p>Response Object - Contains data from the client that made the request
  29. * <ul>
  30. * <li>clientId</li>
  31. * <li>installationId - requires Parse Server 4.0.0+</li>
  32. * </ul>
  33. * </p>
  34. *
  35. * <p>Open Event - When you call query.subscribe(), we send a subscribe request to
  36. * the LiveQuery server, when we get the confirmation from the LiveQuery server,
  37. * this event will be emitted. When the client loses WebSocket connection to the
  38. * LiveQuery server, we will try to auto reconnect the LiveQuery server. If we
  39. * reconnect the LiveQuery server and successfully resubscribe the ParseQuery,
  40. * you'll also get this event.
  41. *
  42. * <pre>
  43. * subscription.on('open', (response) => {
  44. *
  45. * });</pre></p>
  46. *
  47. * <p>Create Event - When a new ParseObject is created and it fulfills the ParseQuery you subscribe,
  48. * you'll get this event. The object is the ParseObject which is created.
  49. *
  50. * <pre>
  51. * subscription.on('create', (object, response) => {
  52. *
  53. * });</pre></p>
  54. *
  55. * <p>Update Event - When an existing ParseObject (original) which fulfills the ParseQuery you subscribe
  56. * is updated (The ParseObject fulfills the ParseQuery before and after changes),
  57. * you'll get this event. The object is the ParseObject which is updated.
  58. * 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('update', (object, original, response) => {
  64. *
  65. * });</pre></p>
  66. *
  67. * <p>Enter Event - When an existing ParseObject's (original) old value doesn't fulfill the ParseQuery
  68. * but its new value fulfills the ParseQuery, you'll get this event. The object is the
  69. * ParseObject which enters the ParseQuery. Its content is the latest value of the ParseObject.
  70. *
  71. * Parse-Server 3.1.3+ Required for original object parameter
  72. *
  73. * <pre>
  74. * subscription.on('enter', (object, original, response) => {
  75. *
  76. * });</pre></p>
  77. *
  78. *
  79. * <p>Update Event - When an existing ParseObject's old value fulfills the ParseQuery but its new value
  80. * doesn't fulfill the ParseQuery, you'll get this event. The object is the ParseObject
  81. * which leaves the ParseQuery. Its content is the latest value of the ParseObject.
  82. *
  83. * <pre>
  84. * subscription.on('leave', (object, response) => {
  85. *
  86. * });</pre></p>
  87. *
  88. *
  89. * <p>Delete Event - When an existing ParseObject which fulfills the ParseQuery is deleted, you'll
  90. * get this event. The object is the ParseObject which is deleted.
  91. *
  92. * <pre>
  93. * subscription.on('delete', (object, response) => {
  94. *
  95. * });</pre></p>
  96. *
  97. *
  98. * <p>Close Event - When the client loses the WebSocket connection to the LiveQuery
  99. * server and we stop receiving events, you'll get this event.
  100. *
  101. * <pre>
  102. * subscription.on('close', () => {
  103. *
  104. * });</pre></p>
  105. *
  106. * @alias Parse.LiveQuerySubscription
  107. */
  108. class Subscription extends _EventEmitter.default {
  109. /*
  110. * @param {string} id - subscription id
  111. * @param {string} query - query to subscribe to
  112. * @param {string} sessionToken - optional session token
  113. */
  114. constructor(id, query, sessionToken) {
  115. super();
  116. this.id = id;
  117. this.query = query;
  118. this.sessionToken = sessionToken;
  119. this.subscribePromise = (0, _promiseUtils.resolvingPromise)();
  120. this.subscribed = false; // adding listener so process does not crash
  121. // best practice is for developer to register their own listener
  122. this.on('error', () => {});
  123. }
  124. /**
  125. * Close the subscription
  126. *
  127. * @returns {Promise}
  128. */
  129. unsubscribe()
  130. /*: Promise*/
  131. {
  132. return _CoreManager.default.getLiveQueryController().getDefaultLiveQueryClient().then(liveQueryClient => {
  133. liveQueryClient.unsubscribe(this);
  134. this.emit('close');
  135. });
  136. }
  137. }
  138. var _default = Subscription;
  139. exports.default = _default;