ParseLiveQuery.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. * @flow
  18. */
  19. function getLiveQueryClient() /*: LiveQueryClient*/{
  20. return _CoreManager.default.getLiveQueryController().getDefaultLiveQueryClient();
  21. }
  22. /**
  23. * We expose three events to help you monitor the status of the WebSocket connection:
  24. *
  25. * <p>Open - When we establish the WebSocket connection to the LiveQuery server, you'll get this event.
  26. *
  27. * <pre>
  28. * Parse.LiveQuery.on('open', () => {
  29. *
  30. * });</pre></p>
  31. *
  32. * <p>Close - When we lose the WebSocket connection to the LiveQuery server, you'll get this event.
  33. *
  34. * <pre>
  35. * Parse.LiveQuery.on('close', () => {
  36. *
  37. * });</pre></p>
  38. *
  39. * <p>Error - When some network error or LiveQuery server error happens, you'll get this event.
  40. *
  41. * <pre>
  42. * Parse.LiveQuery.on('error', (error) => {
  43. *
  44. * });</pre></p>
  45. *
  46. * @class Parse.LiveQuery
  47. * @static
  48. */
  49. var LiveQuery = new _EventEmitter.default();
  50. /**
  51. * After open is called, the LiveQuery will try to send a connect request
  52. * to the LiveQuery server.
  53. */
  54. LiveQuery.open = /*#__PURE__*/(0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee() {
  55. var liveQueryClient;
  56. return _regenerator.default.wrap(function (_context) {
  57. while (1) switch (_context.prev = _context.next) {
  58. case 0:
  59. _context.next = 2;
  60. return getLiveQueryClient();
  61. case 2:
  62. liveQueryClient = _context.sent;
  63. liveQueryClient.open();
  64. case 4:
  65. case "end":
  66. return _context.stop();
  67. }
  68. }, _callee);
  69. }));
  70. /**
  71. * When you're done using LiveQuery, you can call Parse.LiveQuery.close().
  72. * This function will close the WebSocket connection to the LiveQuery server,
  73. * cancel the auto reconnect, and unsubscribe all subscriptions based on it.
  74. * If you call query.subscribe() after this, we'll create a new WebSocket
  75. * connection to the LiveQuery server.
  76. */
  77. LiveQuery.close = /*#__PURE__*/(0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee2() {
  78. var liveQueryClient;
  79. return _regenerator.default.wrap(function (_context2) {
  80. while (1) switch (_context2.prev = _context2.next) {
  81. case 0:
  82. _context2.next = 2;
  83. return getLiveQueryClient();
  84. case 2:
  85. liveQueryClient = _context2.sent;
  86. liveQueryClient.close();
  87. case 4:
  88. case "end":
  89. return _context2.stop();
  90. }
  91. }, _callee2);
  92. }));
  93. // Register a default onError callback to make sure we do not crash on error
  94. LiveQuery.on('error', function () {});
  95. var _default = LiveQuery;
  96. exports.default = _default;
  97. var defaultLiveQueryClient;
  98. var DefaultLiveQueryController = {
  99. setDefaultLiveQueryClient: function (liveQueryClient /*: LiveQueryClient*/) {
  100. defaultLiveQueryClient = liveQueryClient;
  101. },
  102. getDefaultLiveQueryClient: function () /*: Promise<LiveQueryClient>*/{
  103. return (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee3() {
  104. var _yield$Promise$all, _yield$Promise$all2, currentUser, installationId, sessionToken, liveQueryServerURL, serverURL, protocol, host, applicationId, javascriptKey, masterKey;
  105. return _regenerator.default.wrap(function (_context3) {
  106. while (1) switch (_context3.prev = _context3.next) {
  107. case 0:
  108. if (!defaultLiveQueryClient) {
  109. _context3.next = 2;
  110. break;
  111. }
  112. return _context3.abrupt("return", defaultLiveQueryClient);
  113. case 2:
  114. _context3.next = 4;
  115. return _promise.default.all([_CoreManager.default.getUserController().currentUserAsync(), _CoreManager.default.getInstallationController().currentInstallationId()]);
  116. case 4:
  117. _yield$Promise$all = _context3.sent;
  118. _yield$Promise$all2 = (0, _slicedToArray2.default)(_yield$Promise$all, 2);
  119. currentUser = _yield$Promise$all2[0];
  120. installationId = _yield$Promise$all2[1];
  121. sessionToken = currentUser ? currentUser.getSessionToken() : undefined;
  122. liveQueryServerURL = _CoreManager.default.get('LIVEQUERY_SERVER_URL');
  123. if (!(liveQueryServerURL && (0, _indexOf.default)(liveQueryServerURL).call(liveQueryServerURL, 'ws') !== 0)) {
  124. _context3.next = 12;
  125. break;
  126. }
  127. throw new Error('You need to set a proper Parse LiveQuery server url before using LiveQueryClient');
  128. case 12:
  129. // If we can not find Parse.liveQueryServerURL, we try to extract it from Parse.serverURL
  130. if (!liveQueryServerURL) {
  131. serverURL = _CoreManager.default.get('SERVER_URL');
  132. protocol = (0, _indexOf.default)(serverURL).call(serverURL, 'https') === 0 ? 'wss://' : 'ws://';
  133. host = serverURL.replace(/^https?:\/\//, '');
  134. liveQueryServerURL = protocol + host;
  135. _CoreManager.default.set('LIVEQUERY_SERVER_URL', liveQueryServerURL);
  136. }
  137. applicationId = _CoreManager.default.get('APPLICATION_ID');
  138. javascriptKey = _CoreManager.default.get('JAVASCRIPT_KEY');
  139. masterKey = _CoreManager.default.get('MASTER_KEY');
  140. defaultLiveQueryClient = new _LiveQueryClient.default({
  141. applicationId: applicationId,
  142. serverURL: liveQueryServerURL,
  143. javascriptKey: javascriptKey,
  144. masterKey: masterKey,
  145. sessionToken: sessionToken,
  146. installationId: installationId
  147. });
  148. defaultLiveQueryClient.on('error', function (error) {
  149. LiveQuery.emit('error', error);
  150. });
  151. defaultLiveQueryClient.on('open', function () {
  152. LiveQuery.emit('open');
  153. });
  154. defaultLiveQueryClient.on('close', function () {
  155. LiveQuery.emit('close');
  156. });
  157. return _context3.abrupt("return", defaultLiveQueryClient);
  158. case 21:
  159. case "end":
  160. return _context3.stop();
  161. }
  162. }, _callee3);
  163. }))();
  164. },
  165. _clearCachedDefaultClient: function () {
  166. defaultLiveQueryClient = null;
  167. }
  168. };
  169. _CoreManager.default.setLiveQueryController(DefaultLiveQueryController);