ParseLiveQuery.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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 _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
  8. var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
  9. var _EventEmitter = _interopRequireDefault(require("./EventEmitter"));
  10. var _LiveQueryClient = _interopRequireDefault(require("./LiveQueryClient"));
  11. var _CoreManager = _interopRequireDefault(require("./CoreManager"));
  12. /**
  13. * Copyright (c) 2015-present, Parse, LLC.
  14. * All rights reserved.
  15. *
  16. * This source code is licensed under the BSD-style license found in the
  17. * LICENSE file in the root directory of this source tree. An additional grant
  18. * of patent rights can be found in the PATENTS file in the same directory.
  19. *
  20. * @flow
  21. */
  22. function getLiveQueryClient()
  23. /*: LiveQueryClient*/
  24. {
  25. return _CoreManager.default.getLiveQueryController().getDefaultLiveQueryClient();
  26. }
  27. /**
  28. *
  29. * We expose three events to help you monitor the status of the WebSocket connection:
  30. *
  31. * <p>Open - When we establish the WebSocket connection to the LiveQuery server, you'll get this event.
  32. *
  33. * <pre>
  34. * Parse.LiveQuery.on('open', () => {
  35. *
  36. * });</pre></p>
  37. *
  38. * <p>Close - When we lose the WebSocket connection to the LiveQuery server, you'll get this event.
  39. *
  40. * <pre>
  41. * Parse.LiveQuery.on('close', () => {
  42. *
  43. * });</pre></p>
  44. *
  45. * <p>Error - When some network error or LiveQuery server error happens, you'll get this event.
  46. *
  47. * <pre>
  48. * Parse.LiveQuery.on('error', (error) => {
  49. *
  50. * });</pre></p>
  51. *
  52. * @class Parse.LiveQuery
  53. * @static
  54. *
  55. */
  56. var LiveQuery = new _EventEmitter.default();
  57. /**
  58. * After open is called, the LiveQuery will try to send a connect request
  59. * to the LiveQuery server.
  60. */
  61. LiveQuery.open =
  62. /*#__PURE__*/
  63. (0, _asyncToGenerator2.default)(
  64. /*#__PURE__*/
  65. _regenerator.default.mark(function _callee() {
  66. var liveQueryClient;
  67. return _regenerator.default.wrap(function (_context) {
  68. while (1) {
  69. switch (_context.prev = _context.next) {
  70. case 0:
  71. _context.next = 2;
  72. return getLiveQueryClient();
  73. case 2:
  74. liveQueryClient = _context.sent;
  75. return _context.abrupt("return", liveQueryClient.open());
  76. case 4:
  77. case "end":
  78. return _context.stop();
  79. }
  80. }
  81. }, _callee);
  82. }));
  83. /**
  84. * When you're done using LiveQuery, you can call Parse.LiveQuery.close().
  85. * This function will close the WebSocket connection to the LiveQuery server,
  86. * cancel the auto reconnect, and unsubscribe all subscriptions based on it.
  87. * If you call query.subscribe() after this, we'll create a new WebSocket
  88. * connection to the LiveQuery server.
  89. */
  90. LiveQuery.close =
  91. /*#__PURE__*/
  92. (0, _asyncToGenerator2.default)(
  93. /*#__PURE__*/
  94. _regenerator.default.mark(function _callee2() {
  95. var liveQueryClient;
  96. return _regenerator.default.wrap(function (_context2) {
  97. while (1) {
  98. switch (_context2.prev = _context2.next) {
  99. case 0:
  100. _context2.next = 2;
  101. return getLiveQueryClient();
  102. case 2:
  103. liveQueryClient = _context2.sent;
  104. return _context2.abrupt("return", liveQueryClient.close());
  105. case 4:
  106. case "end":
  107. return _context2.stop();
  108. }
  109. }
  110. }, _callee2);
  111. })); // Register a default onError callback to make sure we do not crash on error
  112. LiveQuery.on('error', function () {});
  113. var _default = LiveQuery;
  114. exports.default = _default;
  115. var defaultLiveQueryClient;
  116. var DefaultLiveQueryController = {
  117. setDefaultLiveQueryClient: function (liveQueryClient
  118. /*: LiveQueryClient*/
  119. ) {
  120. defaultLiveQueryClient = liveQueryClient;
  121. },
  122. getDefaultLiveQueryClient: function () {
  123. var _getDefaultLiveQueryClient = (0, _asyncToGenerator2.default)(
  124. /*#__PURE__*/
  125. _regenerator.default.mark(function _callee3() {
  126. var currentUser, sessionToken, liveQueryServerURL, serverURL, protocol, host, applicationId, javascriptKey, masterKey;
  127. return _regenerator.default.wrap(function (_context3) {
  128. while (1) {
  129. switch (_context3.prev = _context3.next) {
  130. case 0:
  131. if (!defaultLiveQueryClient) {
  132. _context3.next = 2;
  133. break;
  134. }
  135. return _context3.abrupt("return", defaultLiveQueryClient);
  136. case 2:
  137. _context3.next = 4;
  138. return _CoreManager.default.getUserController().currentUserAsync();
  139. case 4:
  140. currentUser = _context3.sent;
  141. sessionToken = currentUser ? currentUser.getSessionToken() : undefined;
  142. liveQueryServerURL = _CoreManager.default.get('LIVEQUERY_SERVER_URL');
  143. if (!(liveQueryServerURL && liveQueryServerURL.indexOf('ws') !== 0)) {
  144. _context3.next = 9;
  145. break;
  146. }
  147. throw new Error('You need to set a proper Parse LiveQuery server url before using LiveQueryClient');
  148. case 9:
  149. // If we can not find Parse.liveQueryServerURL, we try to extract it from Parse.serverURL
  150. if (!liveQueryServerURL) {
  151. serverURL = _CoreManager.default.get('SERVER_URL');
  152. protocol = serverURL.indexOf('https') === 0 ? 'wss://' : 'ws://';
  153. host = serverURL.replace(/^https?:\/\//, '');
  154. liveQueryServerURL = protocol + host;
  155. _CoreManager.default.set('LIVEQUERY_SERVER_URL', liveQueryServerURL);
  156. }
  157. applicationId = _CoreManager.default.get('APPLICATION_ID');
  158. javascriptKey = _CoreManager.default.get('JAVASCRIPT_KEY');
  159. masterKey = _CoreManager.default.get('MASTER_KEY');
  160. defaultLiveQueryClient = new _LiveQueryClient.default({
  161. applicationId: applicationId,
  162. serverURL: liveQueryServerURL,
  163. javascriptKey: javascriptKey,
  164. masterKey: masterKey,
  165. sessionToken: sessionToken
  166. });
  167. defaultLiveQueryClient.on('error', function (error) {
  168. LiveQuery.emit('error', error);
  169. });
  170. defaultLiveQueryClient.on('open', function () {
  171. LiveQuery.emit('open');
  172. });
  173. defaultLiveQueryClient.on('close', function () {
  174. LiveQuery.emit('close');
  175. });
  176. return _context3.abrupt("return", defaultLiveQueryClient);
  177. case 18:
  178. case "end":
  179. return _context3.stop();
  180. }
  181. }
  182. }, _callee3);
  183. }));
  184. return function () {
  185. return _getDefaultLiveQueryClient.apply(this, arguments);
  186. };
  187. }(),
  188. _clearCachedDefaultClient: function () {
  189. defaultLiveQueryClient = null;
  190. }
  191. };
  192. _CoreManager.default.setLiveQueryController(DefaultLiveQueryController);