ParseLiveQuery.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 _LiveQueryClient = _interopRequireDefault(require("./LiveQueryClient"));
  8. var _CoreManager = _interopRequireDefault(require("./CoreManager"));
  9. function _interopRequireDefault(obj) {
  10. return obj && obj.__esModule ? obj : {
  11. default: obj
  12. };
  13. }
  14. /**
  15. * @flow
  16. */
  17. function getLiveQueryClient() /*: LiveQueryClient*/{
  18. return _CoreManager.default.getLiveQueryController().getDefaultLiveQueryClient();
  19. }
  20. /**
  21. * We expose three events to help you monitor the status of the WebSocket connection:
  22. *
  23. * <p>Open - When we establish the WebSocket connection to the LiveQuery server, you'll get this event.
  24. *
  25. * <pre>
  26. * Parse.LiveQuery.on('open', () => {
  27. *
  28. * });</pre></p>
  29. *
  30. * <p>Close - When we lose the WebSocket connection to the LiveQuery server, you'll get this event.
  31. *
  32. * <pre>
  33. * Parse.LiveQuery.on('close', () => {
  34. *
  35. * });</pre></p>
  36. *
  37. * <p>Error - When some network error or LiveQuery server error happens, you'll get this event.
  38. *
  39. * <pre>
  40. * Parse.LiveQuery.on('error', (error) => {
  41. *
  42. * });</pre></p>
  43. *
  44. * @class Parse.LiveQuery
  45. * @static
  46. */
  47. const LiveQuery = new _EventEmitter.default();
  48. /**
  49. * After open is called, the LiveQuery will try to send a connect request
  50. * to the LiveQuery server.
  51. */
  52. LiveQuery.open = async () => {
  53. const liveQueryClient = await getLiveQueryClient();
  54. liveQueryClient.open();
  55. };
  56. /**
  57. * When you're done using LiveQuery, you can call Parse.LiveQuery.close().
  58. * This function will close the WebSocket connection to the LiveQuery server,
  59. * cancel the auto reconnect, and unsubscribe all subscriptions based on it.
  60. * If you call query.subscribe() after this, we'll create a new WebSocket
  61. * connection to the LiveQuery server.
  62. */
  63. LiveQuery.close = async () => {
  64. const liveQueryClient = await getLiveQueryClient();
  65. liveQueryClient.close();
  66. };
  67. // Register a default onError callback to make sure we do not crash on error
  68. LiveQuery.on('error', () => {});
  69. var _default = LiveQuery;
  70. exports.default = _default;
  71. let defaultLiveQueryClient;
  72. const DefaultLiveQueryController = {
  73. setDefaultLiveQueryClient(liveQueryClient /*: LiveQueryClient*/) {
  74. defaultLiveQueryClient = liveQueryClient;
  75. },
  76. async getDefaultLiveQueryClient() /*: Promise<LiveQueryClient>*/{
  77. if (defaultLiveQueryClient) {
  78. return defaultLiveQueryClient;
  79. }
  80. const [currentUser, installationId] = await Promise.all([_CoreManager.default.getUserController().currentUserAsync(), _CoreManager.default.getInstallationController().currentInstallationId()]);
  81. const sessionToken = currentUser ? currentUser.getSessionToken() : undefined;
  82. let liveQueryServerURL = _CoreManager.default.get('LIVEQUERY_SERVER_URL');
  83. if (liveQueryServerURL && liveQueryServerURL.indexOf('ws') !== 0) {
  84. throw new Error('You need to set a proper Parse LiveQuery server url before using LiveQueryClient');
  85. }
  86. // If we can not find Parse.liveQueryServerURL, we try to extract it from Parse.serverURL
  87. if (!liveQueryServerURL) {
  88. const serverURL = _CoreManager.default.get('SERVER_URL');
  89. const protocol = serverURL.indexOf('https') === 0 ? 'wss://' : 'ws://';
  90. const host = serverURL.replace(/^https?:\/\//, '');
  91. liveQueryServerURL = protocol + host;
  92. _CoreManager.default.set('LIVEQUERY_SERVER_URL', liveQueryServerURL);
  93. }
  94. const applicationId = _CoreManager.default.get('APPLICATION_ID');
  95. const javascriptKey = _CoreManager.default.get('JAVASCRIPT_KEY');
  96. const masterKey = _CoreManager.default.get('MASTER_KEY');
  97. defaultLiveQueryClient = new _LiveQueryClient.default({
  98. applicationId,
  99. serverURL: liveQueryServerURL,
  100. javascriptKey,
  101. masterKey,
  102. sessionToken,
  103. installationId
  104. });
  105. defaultLiveQueryClient.on('error', error => {
  106. LiveQuery.emit('error', error);
  107. });
  108. defaultLiveQueryClient.on('open', () => {
  109. LiveQuery.emit('open');
  110. });
  111. defaultLiveQueryClient.on('close', () => {
  112. LiveQuery.emit('close');
  113. });
  114. return defaultLiveQueryClient;
  115. },
  116. _clearCachedDefaultClient() {
  117. defaultLiveQueryClient = null;
  118. }
  119. };
  120. _CoreManager.default.setLiveQueryController(DefaultLiveQueryController);