cursorPoller.tsx 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import {Client, Request} from 'sentry/api';
  2. import {defined} from 'sentry/utils';
  3. import parseLinkHeader from 'sentry/utils/parseLinkHeader';
  4. type Options = {
  5. linkPreviousHref: string;
  6. success: (data: any, headers: {queryCount: number}) => void;
  7. };
  8. const BASE_DELAY = 3000;
  9. const MAX_DELAY = 60000;
  10. class CursorPoller {
  11. constructor(options: Options) {
  12. this.options = options;
  13. this.setEndpoint(options.linkPreviousHref);
  14. }
  15. api = new Client();
  16. options: Options;
  17. pollingEndpoint: string = '';
  18. timeoutId: number | null = null;
  19. lastRequest: Request | null = null;
  20. active: boolean = true;
  21. reqsWithoutData = 0;
  22. getDelay() {
  23. const delay = BASE_DELAY * (this.reqsWithoutData + 1);
  24. return Math.min(delay, MAX_DELAY);
  25. }
  26. setEndpoint(linkPreviousHref: string) {
  27. if (!linkPreviousHref) {
  28. this.pollingEndpoint = '';
  29. return;
  30. }
  31. const issueEndpoint = new URL(linkPreviousHref, window.location.origin);
  32. // Remove collapse stats
  33. issueEndpoint.searchParams.delete('collapse');
  34. this.pollingEndpoint = decodeURIComponent(
  35. issueEndpoint.pathname + issueEndpoint.search
  36. );
  37. }
  38. enable() {
  39. this.active = true;
  40. // Proactively clear timeout and last request
  41. if (this.timeoutId) {
  42. window.clearTimeout(this.timeoutId);
  43. }
  44. if (this.lastRequest) {
  45. this.lastRequest.cancel();
  46. }
  47. this.timeoutId = window.setTimeout(this.poll.bind(this), this.getDelay());
  48. }
  49. disable() {
  50. this.active = false;
  51. if (this.timeoutId) {
  52. window.clearTimeout(this.timeoutId);
  53. this.timeoutId = null;
  54. }
  55. if (this.lastRequest) {
  56. this.lastRequest.cancel();
  57. }
  58. }
  59. poll() {
  60. this.lastRequest = this.api.request(this.pollingEndpoint, {
  61. success: (data, _, resp) => {
  62. // cancel in progress operation if disabled
  63. if (!this.active) {
  64. return;
  65. }
  66. // if theres no data, nothing changes
  67. if (!data || !data.length) {
  68. this.reqsWithoutData += 1;
  69. return;
  70. }
  71. if (this.reqsWithoutData > 0) {
  72. this.reqsWithoutData -= 1;
  73. }
  74. const linksHeader = resp?.getResponseHeader('Link') ?? null;
  75. const hitsHeader = resp?.getResponseHeader('X-Hits') ?? null;
  76. const queryCount = defined(hitsHeader) ? parseInt(hitsHeader, 10) || 0 : 0;
  77. const links = parseLinkHeader(linksHeader);
  78. this.setEndpoint(links.previous.href);
  79. this.options.success(data, {queryCount});
  80. },
  81. error: resp => {
  82. if (!resp) {
  83. return;
  84. }
  85. // If user does not have access to the endpoint, we should halt polling
  86. // These errors could mean:
  87. // * the user lost access to a project
  88. // * project was renamed
  89. // * user needs to reauth
  90. if (resp.status === 404 || resp.status === 403 || resp.status === 401) {
  91. this.disable();
  92. }
  93. },
  94. complete: () => {
  95. this.lastRequest = null;
  96. if (this.active) {
  97. this.timeoutId = window.setTimeout(this.poll.bind(this), this.getDelay());
  98. }
  99. },
  100. });
  101. }
  102. }
  103. export default CursorPoller;