ParseLiveQuery.js 7.6 KB

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