LiveQuerySubscription.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  3. Object.defineProperty(exports, "__esModule", {
  4. value: true
  5. });
  6. exports.default = void 0;
  7. var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
  8. var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
  9. var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));
  10. var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
  11. var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
  12. var _EventEmitter2 = _interopRequireDefault(require("./EventEmitter"));
  13. var _CoreManager = _interopRequireDefault(require("./CoreManager"));
  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. *
  29. * <p>Open Event - When you call query.subscribe(), we send a subscribe request to
  30. * the LiveQuery server, when we get the confirmation from the LiveQuery server,
  31. * this event will be emitted. When the client loses WebSocket connection to the
  32. * LiveQuery server, we will try to auto reconnect the LiveQuery server. If we
  33. * reconnect the LiveQuery server and successfully resubscribe the ParseQuery,
  34. * you'll also get this event.
  35. *
  36. * <pre>
  37. * subscription.on('open', () => {
  38. *
  39. * });</pre></p>
  40. *
  41. * <p>Create Event - When a new ParseObject is created and it fulfills the ParseQuery you subscribe,
  42. * you'll get this event. The object is the ParseObject which is created.
  43. *
  44. * <pre>
  45. * subscription.on('create', (object) => {
  46. *
  47. * });</pre></p>
  48. *
  49. * <p>Update Event - When an existing ParseObject (original) which fulfills the ParseQuery you subscribe
  50. * is updated (The ParseObject fulfills the ParseQuery before and after changes),
  51. * you'll get this event. The object is the ParseObject which is updated.
  52. * Its content is the latest value of the ParseObject.
  53. *
  54. * Parse-Server 3.1.3+ Required for original object parameter
  55. *
  56. * <pre>
  57. * subscription.on('update', (object, original) => {
  58. *
  59. * });</pre></p>
  60. *
  61. * <p>Enter Event - When an existing ParseObject's (original) old value doesn't fulfill the ParseQuery
  62. * but its new value fulfills the ParseQuery, you'll get this event. The object is the
  63. * ParseObject which enters the ParseQuery. Its content is the latest value of the ParseObject.
  64. *
  65. * Parse-Server 3.1.3+ Required for original object parameter
  66. *
  67. * <pre>
  68. * subscription.on('enter', (object, original) => {
  69. *
  70. * });</pre></p>
  71. *
  72. *
  73. * <p>Update Event - When an existing ParseObject's old value fulfills the ParseQuery but its new value
  74. * doesn't fulfill the ParseQuery, you'll get this event. The object is the ParseObject
  75. * which leaves the ParseQuery. Its content is the latest value of the ParseObject.
  76. *
  77. * <pre>
  78. * subscription.on('leave', (object) => {
  79. *
  80. * });</pre></p>
  81. *
  82. *
  83. * <p>Delete Event - When an existing ParseObject which fulfills the ParseQuery is deleted, you'll
  84. * get this event. The object is the ParseObject which is deleted.
  85. *
  86. * <pre>
  87. * subscription.on('delete', (object) => {
  88. *
  89. * });</pre></p>
  90. *
  91. *
  92. * <p>Close Event - When the client loses the WebSocket connection to the LiveQuery
  93. * server and we stop receiving events, you'll get this event.
  94. *
  95. * <pre>
  96. * subscription.on('close', () => {
  97. *
  98. * });</pre></p>
  99. *
  100. * @alias Parse.LiveQuerySubscription
  101. */
  102. var Subscription =
  103. /*#__PURE__*/
  104. function (_EventEmitter) {
  105. (0, _inherits2.default)(Subscription, _EventEmitter);
  106. /*
  107. * @param {string} id - subscription id
  108. * @param {string} query - query to subscribe to
  109. * @param {string} sessionToken - optional session token
  110. */
  111. function Subscription(id, query, sessionToken) {
  112. var _this;
  113. (0, _classCallCheck2.default)(this, Subscription);
  114. _this = (0, _possibleConstructorReturn2.default)(this, (0, _getPrototypeOf2.default)(Subscription).call(this));
  115. _this.id = id;
  116. _this.query = query;
  117. _this.sessionToken = sessionToken; // adding listener so process does not crash
  118. // best practice is for developer to register their own listener
  119. _this.on('error', function () {});
  120. return _this;
  121. }
  122. /**
  123. * Close the subscription
  124. */
  125. (0, _createClass2.default)(Subscription, [{
  126. key: "unsubscribe",
  127. value: function ()
  128. /*: Promise*/
  129. {
  130. var _this2 = this;
  131. return _CoreManager.default.getLiveQueryController().getDefaultLiveQueryClient().then(function (liveQueryClient) {
  132. liveQueryClient.unsubscribe(_this2);
  133. _this2.emit('close');
  134. });
  135. }
  136. }]);
  137. return Subscription;
  138. }(_EventEmitter2.default);
  139. var _default = Subscription;
  140. exports.default = _default;