您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

functions.js 1.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import _ from 'lodash';
  2. import XHRInterceptor from 'react-native/Libraries/Network/XHRInterceptor';
  3. import { UPDATE_NETWORK_REQUESTS } from './actionTypes';
  4. /**
  5. * Global index for keeping track of XHR requests.
  6. * @type {number}
  7. */
  8. let reqIndex = 0;
  9. /**
  10. * Starts intercepting network requests. Only XHR requests are supported at the
  11. * moment.
  12. *
  13. * Ongoing request information is kept in redux, and it's removed as soon as a
  14. * given request is complete.
  15. *
  16. * @param {Object} store - The redux store.
  17. * @returns {void}
  18. */
  19. export function startNetInterception({ dispatch, getState }) {
  20. XHRInterceptor.setOpenCallback((method, url, xhr) => {
  21. xhr._reqIndex = reqIndex++;
  22. const requests = getState()['features/net-interceptor'].requests || {};
  23. const newRequests = _.cloneDeep(requests);
  24. const request = {
  25. method,
  26. url
  27. };
  28. newRequests[xhr._reqIndex] = request;
  29. dispatch({
  30. type: UPDATE_NETWORK_REQUESTS,
  31. requests: newRequests
  32. });
  33. });
  34. XHRInterceptor.setResponseCallback((...args) => {
  35. const xhr = args.slice(-1)[0];
  36. if (typeof xhr._reqIndex === 'undefined') {
  37. return;
  38. }
  39. const requests = getState()['features/net-interceptor'].requests || {};
  40. const newRequests = _.cloneDeep(requests);
  41. delete newRequests[xhr._reqIndex];
  42. dispatch({
  43. type: UPDATE_NETWORK_REQUESTS,
  44. requests: newRequests
  45. });
  46. });
  47. XHRInterceptor.enableInterception();
  48. }
  49. /**
  50. * Stops intercepting network requests.
  51. *
  52. * @param {Object} store - The redux store.
  53. * @returns {void}
  54. */
  55. export function stopNetInterception({ dispatch }) {
  56. XHRInterceptor.disableInterception();
  57. dispatch({
  58. type: UPDATE_NETWORK_REQUESTS,
  59. requests: {}
  60. });
  61. }