ParseLiveQuery.js 4.4 KB

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