LiveQuerySubscription.js 4.1 KB

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