ParseLiveQuery.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. * Copyright (c) 2015-present, Parse, LLC.
  16. * All rights reserved.
  17. *
  18. * This source code is licensed under the BSD-style license found in the
  19. * LICENSE file in the root directory of this source tree. An additional grant
  20. * of patent rights can be found in the PATENTS file in the same directory.
  21. *
  22. * @flow
  23. */
  24. function getLiveQueryClient()
  25. /*: LiveQueryClient*/
  26. {
  27. return _CoreManager.default.getLiveQueryController().getDefaultLiveQueryClient();
  28. }
  29. /**
  30. * We expose three events to help you monitor the status of the WebSocket connection:
  31. *
  32. * <p>Open - When we establish the WebSocket connection to the LiveQuery server, you'll get this event.
  33. *
  34. * <pre>
  35. * Parse.LiveQuery.on('open', () => {
  36. *
  37. * });</pre></p>
  38. *
  39. * <p>Close - When we lose the WebSocket connection to the LiveQuery server, you'll get this event.
  40. *
  41. * <pre>
  42. * Parse.LiveQuery.on('close', () => {
  43. *
  44. * });</pre></p>
  45. *
  46. * <p>Error - When some network error or LiveQuery server error happens, you'll get this event.
  47. *
  48. * <pre>
  49. * Parse.LiveQuery.on('error', (error) => {
  50. *
  51. * });</pre></p>
  52. *
  53. * @class Parse.LiveQuery
  54. * @static
  55. */
  56. const 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 = async () => {
  62. const liveQueryClient = await getLiveQueryClient();
  63. liveQueryClient.open();
  64. };
  65. /**
  66. * When you're done using LiveQuery, you can call Parse.LiveQuery.close().
  67. * This function will close the WebSocket connection to the LiveQuery server,
  68. * cancel the auto reconnect, and unsubscribe all subscriptions based on it.
  69. * If you call query.subscribe() after this, we'll create a new WebSocket
  70. * connection to the LiveQuery server.
  71. */
  72. LiveQuery.close = async () => {
  73. const liveQueryClient = await getLiveQueryClient();
  74. liveQueryClient.close();
  75. }; // Register a default onError callback to make sure we do not crash on error
  76. LiveQuery.on('error', () => {});
  77. var _default = LiveQuery;
  78. exports.default = _default;
  79. let defaultLiveQueryClient;
  80. const DefaultLiveQueryController = {
  81. setDefaultLiveQueryClient(liveQueryClient
  82. /*: LiveQueryClient*/
  83. ) {
  84. defaultLiveQueryClient = liveQueryClient;
  85. },
  86. async getDefaultLiveQueryClient()
  87. /*: Promise<LiveQueryClient>*/
  88. {
  89. if (defaultLiveQueryClient) {
  90. return defaultLiveQueryClient;
  91. }
  92. const [currentUser, installationId] = await Promise.all([_CoreManager.default.getUserController().currentUserAsync(), _CoreManager.default.getInstallationController().currentInstallationId()]);
  93. const sessionToken = currentUser ? currentUser.getSessionToken() : undefined;
  94. let liveQueryServerURL = _CoreManager.default.get('LIVEQUERY_SERVER_URL');
  95. if (liveQueryServerURL && liveQueryServerURL.indexOf('ws') !== 0) {
  96. throw new Error('You need to set a proper Parse LiveQuery server url before using LiveQueryClient');
  97. } // If we can not find Parse.liveQueryServerURL, we try to extract it from Parse.serverURL
  98. if (!liveQueryServerURL) {
  99. const serverURL = _CoreManager.default.get('SERVER_URL');
  100. const protocol = serverURL.indexOf('https') === 0 ? 'wss://' : 'ws://';
  101. const host = serverURL.replace(/^https?:\/\//, '');
  102. liveQueryServerURL = protocol + host;
  103. _CoreManager.default.set('LIVEQUERY_SERVER_URL', liveQueryServerURL);
  104. }
  105. const applicationId = _CoreManager.default.get('APPLICATION_ID');
  106. const javascriptKey = _CoreManager.default.get('JAVASCRIPT_KEY');
  107. const masterKey = _CoreManager.default.get('MASTER_KEY');
  108. defaultLiveQueryClient = new _LiveQueryClient.default({
  109. applicationId,
  110. serverURL: liveQueryServerURL,
  111. javascriptKey,
  112. masterKey,
  113. sessionToken,
  114. installationId
  115. });
  116. defaultLiveQueryClient.on('error', error => {
  117. LiveQuery.emit('error', error);
  118. });
  119. defaultLiveQueryClient.on('open', () => {
  120. LiveQuery.emit('open');
  121. });
  122. defaultLiveQueryClient.on('close', () => {
  123. LiveQuery.emit('close');
  124. });
  125. return defaultLiveQueryClient;
  126. },
  127. _clearCachedDefaultClient() {
  128. defaultLiveQueryClient = null;
  129. }
  130. };
  131. _CoreManager.default.setLiveQueryController(DefaultLiveQueryController);