Commit 4829a69f authored by jerasmus's avatar jerasmus

Prevent refetching commit data

Prevent refetching commit date after clicking Show more.

Changelog: performance
parent f3841248
...@@ -8,6 +8,12 @@ import refQuery from './queries/ref.query.graphql'; ...@@ -8,6 +8,12 @@ import refQuery from './queries/ref.query.graphql';
const fetchpromises = {}; const fetchpromises = {};
const resolvers = {}; const resolvers = {};
let maxOffset; let maxOffset;
let nextOffset;
let currentPath;
function setNextOffset(offset) {
nextOffset = offset || null;
}
export function resolveCommit(commits, path, { resolve, entry }) { export function resolveCommit(commits, path, { resolve, entry }) {
const commit = commits.find( const commit = commits.find(
...@@ -24,7 +30,17 @@ export function fetchLogsTree(client, path, offset, resolver = null, _maxOffset ...@@ -24,7 +30,17 @@ export function fetchLogsTree(client, path, offset, resolver = null, _maxOffset
maxOffset = _maxOffset; maxOffset = _maxOffset;
} }
if (Number(offset) > maxOffset) { if (!currentPath || currentPath !== path) {
// ensures the nextOffset is reset if the user changed directories
setNextOffset(null);
}
currentPath = path;
const offsetNumber = Number(offset);
if (!nextOffset && offsetNumber > maxOffset) {
setNextOffset(offsetNumber - 25); // ensures commit data is fetched for newly added rows that need data from the previous request (requests are made in batches of 25).
return Promise.resolve(); return Promise.resolve();
} }
...@@ -47,7 +63,7 @@ export function fetchLogsTree(client, path, offset, resolver = null, _maxOffset ...@@ -47,7 +63,7 @@ export function fetchLogsTree(client, path, offset, resolver = null, _maxOffset
path.replace(/^\//, ''), path.replace(/^\//, ''),
)}`, )}`,
{ {
params: { format: 'json', offset }, params: { format: 'json', offset: nextOffset || offset },
}, },
) )
.then(({ data: newData, headers }) => { .then(({ data: newData, headers }) => {
...@@ -66,10 +82,12 @@ export function fetchLogsTree(client, path, offset, resolver = null, _maxOffset ...@@ -66,10 +82,12 @@ export function fetchLogsTree(client, path, offset, resolver = null, _maxOffset
delete fetchpromises[path]; delete fetchpromises[path];
if (headerLogsOffset) { if (headerLogsOffset) {
setNextOffset(null);
fetchLogsTree(client, path, headerLogsOffset); fetchLogsTree(client, path, headerLogsOffset);
} else { } else {
delete resolvers[path]; delete resolvers[path];
maxOffset = null; maxOffset = null;
setNextOffset(null);
} }
}); });
......
...@@ -69,6 +69,16 @@ describe('fetchLogsTree', () => { ...@@ -69,6 +69,16 @@ describe('fetchLogsTree', () => {
mock.restore(); mock.restore();
}); });
it('persists the offset for a given page if offset is larger than maximum offset', async () => {
await fetchLogsTree(client, 'path', '1000', resolver, 900).then(() => {});
await fetchLogsTree(client, 'path', '1100', resolver, 1200).then(() => {
expect(axios.get).toHaveBeenCalledWith('/gitlab-org/gitlab-foss/-/refs/main/logs_tree/path', {
params: { format: 'json', offset: 975 },
});
});
});
it('does not call axios get if offset is larger than the maximum offset', () => it('does not call axios get if offset is larger than the maximum offset', () =>
fetchLogsTree(client, '', '1000', resolver, 900).then(() => { fetchLogsTree(client, '', '1000', resolver, 900).then(() => {
expect(axios.get).not.toHaveBeenCalled(); expect(axios.get).not.toHaveBeenCalled();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment