ParseLiveQuery.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 _defineProperty2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/defineProperty"));
  11. var _LiveQueryClient = _interopRequireDefault(require("./LiveQueryClient"));
  12. var _CoreManager = _interopRequireDefault(require("./CoreManager"));
  13. function getLiveQueryClient() {
  14. return _CoreManager.default.getLiveQueryController().getDefaultLiveQueryClient();
  15. }
  16. /**
  17. * We expose three events to help you monitor the status of the WebSocket connection:
  18. *
  19. * <p>Open - When we establish the WebSocket connection to the LiveQuery server, you'll get this event.
  20. *
  21. * <pre>
  22. * Parse.LiveQuery.on('open', () => {
  23. *
  24. * });</pre></p>
  25. *
  26. * <p>Close - When we lose the WebSocket connection to the LiveQuery server, you'll get this event.
  27. *
  28. * <pre>
  29. * Parse.LiveQuery.on('close', () => {
  30. *
  31. * });</pre></p>
  32. *
  33. * <p>Error - When some network error or LiveQuery server error happens, you'll get this event.
  34. *
  35. * <pre>
  36. * Parse.LiveQuery.on('error', (error) => {
  37. *
  38. * });</pre></p>
  39. *
  40. * @class Parse.LiveQuery
  41. * @static
  42. */
  43. class LiveQuery {
  44. constructor() {
  45. var _this = this;
  46. (0, _defineProperty2.default)(this, "emitter", void 0);
  47. (0, _defineProperty2.default)(this, "on", void 0);
  48. (0, _defineProperty2.default)(this, "emit", void 0);
  49. const EventEmitter = _CoreManager.default.getEventEmitter();
  50. this.emitter = new EventEmitter();
  51. this.on = (eventName, listener) => this.emitter.on(eventName, listener);
  52. this.emit = function (eventName) {
  53. for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
  54. args[_key - 1] = arguments[_key];
  55. }
  56. return _this.emitter.emit(eventName, ...args);
  57. };
  58. // adding listener so process does not crash
  59. // best practice is for developer to register their own listener
  60. this.on('error', () => {});
  61. }
  62. /**
  63. * After open is called, the LiveQuery will try to send a connect request
  64. * to the LiveQuery server.
  65. */
  66. async open() {
  67. const liveQueryClient = await getLiveQueryClient();
  68. liveQueryClient.open();
  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. async close() {
  78. const liveQueryClient = await getLiveQueryClient();
  79. liveQueryClient.close();
  80. }
  81. }
  82. var _default = exports.default = LiveQuery;
  83. let defaultLiveQueryClient;
  84. const DefaultLiveQueryController = {
  85. setDefaultLiveQueryClient(liveQueryClient) {
  86. defaultLiveQueryClient = liveQueryClient;
  87. },
  88. async getDefaultLiveQueryClient() {
  89. if (defaultLiveQueryClient) {
  90. return defaultLiveQueryClient;
  91. }
  92. const [currentUser, installationId] = await _promise.default.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 && (0, _indexOf.default)(liveQueryServerURL).call(liveQueryServerURL, 'ws') !== 0) {
  96. throw new Error('You need to set a proper Parse LiveQuery server url before using LiveQueryClient');
  97. }
  98. // If we can not find Parse.liveQueryServerURL, we try to extract it from Parse.serverURL
  99. if (!liveQueryServerURL) {
  100. const serverURL = _CoreManager.default.get('SERVER_URL');
  101. const protocol = (0, _indexOf.default)(serverURL).call(serverURL, 'https') === 0 ? 'wss://' : 'ws://';
  102. const host = serverURL.replace(/^https?:\/\//, '');
  103. liveQueryServerURL = protocol + host;
  104. _CoreManager.default.set('LIVEQUERY_SERVER_URL', liveQueryServerURL);
  105. }
  106. const applicationId = _CoreManager.default.get('APPLICATION_ID');
  107. const javascriptKey = _CoreManager.default.get('JAVASCRIPT_KEY');
  108. const masterKey = _CoreManager.default.get('MASTER_KEY');
  109. defaultLiveQueryClient = new _LiveQueryClient.default({
  110. applicationId,
  111. serverURL: liveQueryServerURL,
  112. javascriptKey,
  113. masterKey,
  114. sessionToken,
  115. installationId
  116. });
  117. const LiveQuery = _CoreManager.default.getLiveQuery();
  118. defaultLiveQueryClient.on('error', error => {
  119. LiveQuery.emit('error', error);
  120. });
  121. defaultLiveQueryClient.on('open', () => {
  122. LiveQuery.emit('open');
  123. });
  124. defaultLiveQueryClient.on('close', () => {
  125. LiveQuery.emit('close');
  126. });
  127. return defaultLiveQueryClient;
  128. },
  129. _clearCachedDefaultClient() {
  130. defaultLiveQueryClient = null;
  131. }
  132. };
  133. _CoreManager.default.setLiveQueryController(DefaultLiveQueryController);