ParseLiveQuery.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _LiveQueryClient = _interopRequireDefault(require("./LiveQueryClient"));
  7. var _CoreManager = _interopRequireDefault(require("./CoreManager"));
  8. function _interopRequireDefault(e) {
  9. return e && e.__esModule ? e : {
  10. default: e
  11. };
  12. }
  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. const EventEmitter = _CoreManager.default.getEventEmitter();
  46. this.emitter = new EventEmitter();
  47. this.on = (eventName, listener) => this.emitter.on(eventName, listener);
  48. this.emit = (eventName, ...args) => this.emitter.emit(eventName, ...args);
  49. // adding listener so process does not crash
  50. // best practice is for developer to register their own listener
  51. this.on('error', () => {});
  52. }
  53. /**
  54. * After open is called, the LiveQuery will try to send a connect request
  55. * to the LiveQuery server.
  56. */
  57. async open() {
  58. const liveQueryClient = await getLiveQueryClient();
  59. liveQueryClient.open();
  60. }
  61. /**
  62. * When you're done using LiveQuery, you can call Parse.LiveQuery.close().
  63. * This function will close the WebSocket connection to the LiveQuery server,
  64. * cancel the auto reconnect, and unsubscribe all subscriptions based on it.
  65. * If you call query.subscribe() after this, we'll create a new WebSocket
  66. * connection to the LiveQuery server.
  67. */
  68. async close() {
  69. const liveQueryClient = await getLiveQueryClient();
  70. liveQueryClient.close();
  71. }
  72. }
  73. var _default = exports.default = LiveQuery;
  74. let defaultLiveQueryClient;
  75. const DefaultLiveQueryController = {
  76. setDefaultLiveQueryClient(liveQueryClient) {
  77. defaultLiveQueryClient = liveQueryClient;
  78. },
  79. async getDefaultLiveQueryClient() {
  80. if (defaultLiveQueryClient) {
  81. return defaultLiveQueryClient;
  82. }
  83. const [currentUser, installationId] = await Promise.all([_CoreManager.default.getUserController().currentUserAsync(), _CoreManager.default.getInstallationController().currentInstallationId()]);
  84. const sessionToken = currentUser ? currentUser.getSessionToken() : undefined;
  85. let liveQueryServerURL = _CoreManager.default.get('LIVEQUERY_SERVER_URL');
  86. if (liveQueryServerURL && liveQueryServerURL.indexOf('ws') !== 0) {
  87. throw new Error('You need to set a proper Parse LiveQuery server url before using LiveQueryClient');
  88. }
  89. // If we can not find Parse.liveQueryServerURL, we try to extract it from Parse.serverURL
  90. if (!liveQueryServerURL) {
  91. const serverURL = _CoreManager.default.get('SERVER_URL');
  92. const protocol = serverURL.indexOf('https') === 0 ? 'wss://' : 'ws://';
  93. const host = serverURL.replace(/^https?:\/\//, '');
  94. liveQueryServerURL = protocol + host;
  95. _CoreManager.default.set('LIVEQUERY_SERVER_URL', liveQueryServerURL);
  96. }
  97. const applicationId = _CoreManager.default.get('APPLICATION_ID');
  98. const javascriptKey = _CoreManager.default.get('JAVASCRIPT_KEY');
  99. const masterKey = _CoreManager.default.get('MASTER_KEY');
  100. defaultLiveQueryClient = new _LiveQueryClient.default({
  101. applicationId,
  102. serverURL: liveQueryServerURL,
  103. javascriptKey,
  104. masterKey,
  105. sessionToken,
  106. installationId
  107. });
  108. const LiveQuery = _CoreManager.default.getLiveQuery();
  109. defaultLiveQueryClient.on('error', error => {
  110. LiveQuery.emit('error', error);
  111. });
  112. defaultLiveQueryClient.on('open', () => {
  113. LiveQuery.emit('open');
  114. });
  115. defaultLiveQueryClient.on('close', () => {
  116. LiveQuery.emit('close');
  117. });
  118. return defaultLiveQueryClient;
  119. },
  120. _clearCachedDefaultClient() {
  121. defaultLiveQueryClient = null;
  122. }
  123. };
  124. _CoreManager.default.setLiveQueryController(DefaultLiveQueryController);