cursorPoller.tsx 3.1 KB

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