summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/blame/streaming/index.js
blob: a74e01b6423d61f4af532ab4f196030266eac673 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import { renderHtmlStreams } from '~/streaming/render_html_streams';
import { handleStreamedAnchorLink } from '~/streaming/handle_streamed_anchor_link';
import { createAlert } from '~/flash';
import { __ } from '~/locale';
import { rateLimitStreamRequests } from '~/streaming/rate_limit_stream_requests';
import { toPolyfillReadable } from '~/streaming/polyfills';

export async function renderBlamePageStreams(firstStreamPromise) {
  const element = document.querySelector('#blame-stream-container');

  if (!element || !firstStreamPromise) return;

  const stopAnchorObserver = handleStreamedAnchorLink(element);
  const { dataset } = document.querySelector('#blob-content-holder');
  const totalExtraPages = parseInt(dataset.totalExtraPages, 10);
  const { pagesUrl } = dataset;

  const remainingStreams = rateLimitStreamRequests({
    factory: (index) => {
      const url = new URL(pagesUrl);
      // page numbers start with 1
      // the first page is already rendered in the document
      // the second page is passed with the 'firstStreamPromise'
      url.searchParams.set('page', index + 3);
      return fetch(url).then((response) => toPolyfillReadable(response.body));
    },
    // we don't want to overload gitaly with concurrent requests
    // https://gitlab.com/gitlab-org/gitlab/-/issues/391842#note_1281695095
    // using 5 as a good starting point
    maxConcurrentRequests: 5,
    total: totalExtraPages,
  });

  try {
    await renderHtmlStreams(
      [firstStreamPromise.then(toPolyfillReadable), ...remainingStreams],
      element,
    );
  } catch (error) {
    createAlert({
      message: __('Blame could not be loaded as a single page.'),
      primaryButton: {
        text: __('View blame as separate pages'),
        clickHandler() {
          const newUrl = new URL(window.location);
          newUrl.searchParams.delete('streaming');
          window.location.href = newUrl;
        },
      },
    });
    throw error;
  } finally {
    stopAnchorObserver();
    document.querySelector('#blame-stream-loading').remove();
  }
}