Skip to content

Commit 9f2a811

Browse files
Cache data on Folder Progress Bar Worker
Currently, all information needed for calculating the folder progress bar is sent to the worker on every click on a new resource. That is slow and not necessary, as most of the data just changes after saving. Therefore, in this commit, the progress-bar-worker is modified such that it caches information and only reloads the cache after saving. Signed-off-by: Florian Schepers <florianschepers@gmx.de>
1 parent 3697ee8 commit 9f2a811

File tree

3 files changed

+70
-17
lines changed

3 files changed

+70
-17
lines changed

src/Frontend/Components/ProgressBar/FolderProgressBar.tsx

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -72,18 +72,8 @@ export function FolderProgressBar(props: FolderProgressBarProps): ReactElement {
7272
const folderProgressBarWorkerArgs = useMemo(
7373
() => ({
7474
resourceId,
75-
manualAttributions,
76-
externalAttributions,
77-
resourcesToManualAttributions,
78-
resolvedExternalAttributions,
7975
}),
80-
[
81-
resourceId,
82-
manualAttributions,
83-
externalAttributions,
84-
resourcesToManualAttributions,
85-
resolvedExternalAttributions,
86-
],
76+
[resourceId],
8777
);
8878

8979
const folderProgressBarSyncFallbackArgs = useMemo(

src/Frontend/Components/WorkersContextProvider/WorkersContextProvider.tsx

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,20 @@ import { FC, ReactNode, createContext, useMemo } from 'react';
77
import { useAppSelector } from '../../state/hooks';
88
import {
99
getAttributionBreakpoints,
10+
getExternalAttributions,
1011
getExternalAttributionsToHashes,
1112
getExternalData,
1213
getFilesWithChildren,
14+
getManualAttributions,
1315
getManualData,
1416
getResources,
1517
getResourcesToExternalAttributions,
18+
getResourcesToManualAttributions,
1619
} from '../../state/selectors/all-views-resource-selectors';
1720
import { getNewAccordionWorkers } from '../../web-workers/get-new-accordion-workers';
1821
import { getNewProgressBarWorkers } from '../../web-workers/get-new-progress-bar-workers';
1922
import { PanelAttributionData } from '../../util/get-contained-packages';
23+
import { getResolvedExternalAttributions } from '../../state/selectors/audit-view-resource-selectors';
2024

2125
const resourceDetailsTabsWorkers = getNewAccordionWorkers();
2226

@@ -93,9 +97,17 @@ export const ProgressBarWorkersContextProvider: FC<{
9397
children: ReactNode | null;
9498
}> = ({ children }) => {
9599
const resources = useAppSelector(getResources);
100+
const manualAttributions = useAppSelector(getManualAttributions);
101+
const externalAttributions = useAppSelector(getExternalAttributions);
102+
const resourcesToManualAttributions = useAppSelector(
103+
getResourcesToManualAttributions,
104+
);
96105
const resourcesToExternalAttributions = useAppSelector(
97106
getResourcesToExternalAttributions,
98107
);
108+
const resolvedExternalAttributions = useAppSelector(
109+
getResolvedExternalAttributions,
110+
);
99111
const attributionBreakpoints = useAppSelector(getAttributionBreakpoints);
100112
const filesWithChildren = useAppSelector(getFilesWithChildren);
101113

@@ -104,6 +116,7 @@ export const ProgressBarWorkersContextProvider: FC<{
104116
progressBarWorkers.TopProgressBarWorker.postMessage({
105117
isCacheInitializationMessage: true,
106118
resources: null,
119+
externalAttributions: null,
107120
resourcesToExternalAttributions: null,
108121
attributionBreakpoints: null,
109122
filesWithChildren: null,
@@ -112,6 +125,7 @@ export const ProgressBarWorkersContextProvider: FC<{
112125
worker.postMessage({
113126
isCacheInitializationMessage: true,
114127
resources,
128+
externalAttributions,
115129
resourcesToExternalAttributions,
116130
attributionBreakpoints,
117131
filesWithChildren,
@@ -122,11 +136,35 @@ export const ProgressBarWorkersContextProvider: FC<{
122136
}
123137
}, [
124138
resources,
139+
externalAttributions,
125140
resourcesToExternalAttributions,
126141
attributionBreakpoints,
127142
filesWithChildren,
128143
]);
129144

145+
useMemo(() => {
146+
try {
147+
progressBarWorkers.TopProgressBarWorker.postMessage({
148+
manualAttributions: null,
149+
resourcesToManualAttributions: null,
150+
resolvedExternalAttributions: null,
151+
});
152+
Object.values(progressBarWorkers).forEach((worker) => {
153+
worker.postMessage({
154+
manualAttributions,
155+
resourcesToManualAttributions,
156+
resolvedExternalAttributions,
157+
});
158+
});
159+
} catch (error) {
160+
console.info('Web worker error in workers context provider: ', error);
161+
}
162+
}, [
163+
manualAttributions,
164+
resourcesToManualAttributions,
165+
resolvedExternalAttributions,
166+
]);
167+
130168
return (
131169
<ProgressBarWorkersContext.Provider value={progressBarWorkers}>
132170
{children}

src/Frontend/web-workers/progress-bar-worker.ts

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,23 @@
22
//
33
// SPDX-License-Identifier: Apache-2.0
44

5-
import { Resources, ResourcesToAttributions } from '../../shared/shared-types';
5+
import {
6+
Attributions,
7+
Resources,
8+
ResourcesToAttributions,
9+
} from '../../shared/shared-types';
610
import { getUpdatedProgressBarData } from '../state/helpers/progress-bar-data-helpers';
711
import { ProgressBarDataAndResourceId } from '../types/types';
812

913
let cachedResources: Resources | null = null;
14+
let cachedExternalAttributions: Attributions | null = null;
1015
let cachedResourcesToExternalAttributions: ResourcesToAttributions | null =
1116
null;
1217
let cachedAttributionBreakpoints: Set<string> | null = null;
1318
let cachedFilesWithChildren: Set<string> | null = null;
19+
let cachedManualAttributions: Attributions | null = null;
20+
let cachedResourcesToManualAttributions: ResourcesToAttributions | null = null;
21+
let cachedResolvedExternalAttributions: Set<string> | null = null;
1422

1523
self.onmessage = ({
1624
data: {
@@ -26,12 +34,29 @@ self.onmessage = ({
2634
filesWithChildren,
2735
},
2836
}): void => {
37+
if (
38+
manualAttributions !== undefined &&
39+
resourcesToManualAttributions !== undefined &&
40+
resolvedExternalAttributions !== undefined
41+
) {
42+
cachedManualAttributions = manualAttributions;
43+
cachedResourcesToManualAttributions = resourcesToManualAttributions;
44+
cachedResolvedExternalAttributions = resolvedExternalAttributions;
45+
}
46+
2947
if (isCacheInitializationMessage) {
3048
cachedResources = resources;
49+
cachedExternalAttributions = externalAttributions;
3150
cachedResourcesToExternalAttributions = resourcesToExternalAttributions;
3251
cachedAttributionBreakpoints = attributionBreakpoints;
3352
cachedFilesWithChildren = filesWithChildren;
34-
} else if (
53+
}
54+
55+
if (
56+
cachedManualAttributions &&
57+
cachedExternalAttributions &&
58+
cachedResourcesToManualAttributions &&
59+
cachedResolvedExternalAttributions &&
3560
cachedResources &&
3661
cachedResourcesToExternalAttributions &&
3762
cachedAttributionBreakpoints &&
@@ -40,11 +65,11 @@ self.onmessage = ({
4065
const progressBarData = getUpdatedProgressBarData({
4166
resources: cachedResources,
4267
resourceId,
43-
manualAttributions,
44-
externalAttributions,
45-
resourcesToManualAttributions,
68+
manualAttributions: cachedManualAttributions,
69+
externalAttributions: cachedExternalAttributions,
70+
resourcesToManualAttributions: cachedResourcesToManualAttributions,
4671
resourcesToExternalAttributions: cachedResourcesToExternalAttributions,
47-
resolvedExternalAttributions,
72+
resolvedExternalAttributions: cachedResolvedExternalAttributions,
4873
attributionBreakpoints: cachedAttributionBreakpoints,
4974
filesWithChildren: cachedFilesWithChildren,
5075
});

0 commit comments

Comments
 (0)