ParseLiveQuery.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /**
  2. * Copyright (c) 2015-present, Parse, LLC.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under the BSD-style license found in the
  6. * LICENSE file in the root directory of this source tree. An additional grant
  7. * of patent rights can be found in the PATENTS file in the same directory.
  8. *
  9. * @flow
  10. */
  11. import EventEmitter from './EventEmitter';
  12. import LiveQueryClient from './LiveQueryClient';
  13. import CoreManager from './CoreManager';
  14. function getLiveQueryClient()
  15. /*: LiveQueryClient*/
  16. {
  17. return CoreManager.getLiveQueryController().getDefaultLiveQueryClient();
  18. }
  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. */
  48. const LiveQuery = new EventEmitter();
  49. /**
  50. * After open is called, the LiveQuery will try to send a connect request
  51. * to the LiveQuery server.
  52. */
  53. LiveQuery.open = async () => {
  54. const liveQueryClient = await getLiveQueryClient();
  55. return liveQueryClient.open();
  56. };
  57. /**
  58. * When you're done using LiveQuery, you can call Parse.LiveQuery.close().
  59. * This function will close the WebSocket connection to the LiveQuery server,
  60. * cancel the auto reconnect, and unsubscribe all subscriptions based on it.
  61. * If you call query.subscribe() after this, we'll create a new WebSocket
  62. * connection to the LiveQuery server.
  63. */
  64. LiveQuery.close = async () => {
  65. const liveQueryClient = await getLiveQueryClient();
  66. return liveQueryClient.close();
  67. }; // Register a default onError callback to make sure we do not crash on error
  68. LiveQuery.on('error', () => {});
  69. export default LiveQuery;
  70. let defaultLiveQueryClient;
  71. const DefaultLiveQueryController = {
  72. setDefaultLiveQueryClient(liveQueryClient
  73. /*: LiveQueryClient*/
  74. ) {
  75. defaultLiveQueryClient = liveQueryClient;
  76. },
  77. async getDefaultLiveQueryClient()
  78. /*: Promise<LiveQueryClient>*/
  79. {
  80. if (defaultLiveQueryClient) {
  81. return defaultLiveQueryClient;
  82. }
  83. const currentUser = await CoreManager.getUserController().currentUserAsync();
  84. const sessionToken = currentUser ? currentUser.getSessionToken() : undefined;
  85. let liveQueryServerURL = CoreManager.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. } // If we can not find Parse.liveQueryServerURL, we try to extract it from Parse.serverURL
  89. if (!liveQueryServerURL) {
  90. const serverURL = CoreManager.get('SERVER_URL');
  91. const protocol = serverURL.indexOf('https') === 0 ? 'wss://' : 'ws://';
  92. const host = serverURL.replace(/^https?:\/\//, '');
  93. liveQueryServerURL = protocol + host;
  94. CoreManager.set('LIVEQUERY_SERVER_URL', liveQueryServerURL);
  95. }
  96. const applicationId = CoreManager.get('APPLICATION_ID');
  97. const javascriptKey = CoreManager.get('JAVASCRIPT_KEY');
  98. const masterKey = CoreManager.get('MASTER_KEY');
  99. defaultLiveQueryClient = new LiveQueryClient({
  100. applicationId,
  101. serverURL: liveQueryServerURL,
  102. javascriptKey,
  103. masterKey,
  104. sessionToken
  105. });
  106. defaultLiveQueryClient.on('error', error => {
  107. LiveQuery.emit('error', error);
  108. });
  109. defaultLiveQueryClient.on('open', () => {
  110. LiveQuery.emit('open');
  111. });
  112. defaultLiveQueryClient.on('close', () => {
  113. LiveQuery.emit('close');
  114. });
  115. return defaultLiveQueryClient;
  116. },
  117. _clearCachedDefaultClient() {
  118. defaultLiveQueryClient = null;
  119. }
  120. };
  121. CoreManager.setLiveQueryController(DefaultLiveQueryController);