added caching for projects
This commit is contained in:
+77
-34
@@ -610,18 +610,32 @@ export class UniTrendParentComponent implements OnInit {
|
||||
const resolved = data['trendData'];
|
||||
const res = resolved.trendResponse ?? resolved;
|
||||
this.rawTrendResponse.set(res);
|
||||
if (resolved.searchType) {
|
||||
this.selectedSearchType.set(resolved.searchType);
|
||||
}
|
||||
|
||||
const type = resolved.searchType || this.selectedSearchType();
|
||||
this.selectedSearchType.set(type);
|
||||
this.fetchedSearchType.set(type);
|
||||
|
||||
const fuzzy = this.state?.fuzzySearch() ?? true;
|
||||
this.fetchedFuzzy.set(fuzzy);
|
||||
|
||||
if (resolved.topics && resolved.topics.length > 0) {
|
||||
this.selectedTrendTopics.set(resolved.topics);
|
||||
this.fetchedTopics.set([...resolved.topics]);
|
||||
if (
|
||||
!this.activeChartTopic() ||
|
||||
!resolved.topics.includes(this.activeChartTopic())
|
||||
) {
|
||||
this.activeChartTopic.set(resolved.topics[0]);
|
||||
}
|
||||
} else {
|
||||
this.fetchedTopics.set([...this.selectedTrendTopics()]);
|
||||
}
|
||||
|
||||
this.fetchedInstitutions.set([...this.comparisonInstitutions()]);
|
||||
this.fetchedCountries.set([...this.comparisonCountries()]);
|
||||
this.fetchedRegions.set([...this.comparisonRegions()]);
|
||||
this.fetchedAuthors.set([...this.comparisonAuthors()]);
|
||||
|
||||
this.isTrendLoading.set(false);
|
||||
}
|
||||
});
|
||||
@@ -692,7 +706,7 @@ export class UniTrendParentComponent implements OnInit {
|
||||
});
|
||||
});
|
||||
|
||||
// Effect: automatically refetch trend data whenever topics or comparisons change (fully reactive with subset check)
|
||||
// Effect: automatically refetch trend data whenever topics or comparisons change (fully reactive with cached rawTrendResponse check)
|
||||
effect(() => {
|
||||
const topics = this.selectedTrendTopics();
|
||||
const insts = this.comparisonInstitutions();
|
||||
@@ -707,55 +721,59 @@ export class UniTrendParentComponent implements OnInit {
|
||||
const fetchedT = new Set(untracked(() => this.fetchedTopics()));
|
||||
const fetchedType = untracked(() => this.fetchedSearchType());
|
||||
const fetchedF = untracked(() => this.fetchedFuzzy());
|
||||
const rawRes = untracked(() => this.rawTrendResponse());
|
||||
|
||||
const allTopicsFetched = topics.every((t) => fetchedT.has(t));
|
||||
const sameType = searchType === fetchedType;
|
||||
const sameFuzzy = fuzzy === fetchedF;
|
||||
|
||||
let allEntitiesFetched = false;
|
||||
if (sameType) {
|
||||
if (sameType && rawRes && rawRes.series) {
|
||||
const rawSeriesCleanIds = new Set(
|
||||
rawRes.series.map((s) => {
|
||||
const id = s.entity_id;
|
||||
return id.includes('/')
|
||||
? id.split('/').pop()!.toLowerCase()
|
||||
: id.toLowerCase();
|
||||
}),
|
||||
);
|
||||
|
||||
if (searchType === 'authors') {
|
||||
const fetchedA = new Set(
|
||||
untracked(() => this.fetchedAuthors()).map((a) =>
|
||||
a.id.toLowerCase(),
|
||||
),
|
||||
);
|
||||
allEntitiesFetched = authors.every((a) =>
|
||||
fetchedA.has(a.id.toLowerCase()),
|
||||
);
|
||||
allEntitiesFetched =
|
||||
authors.length === 0 ||
|
||||
authors.every((a) => {
|
||||
const cleanA = a.id.includes('/')
|
||||
? a.id.split('/').pop()!.toLowerCase()
|
||||
: a.id.toLowerCase();
|
||||
return rawSeriesCleanIds.has(cleanA);
|
||||
});
|
||||
} else if (searchType === 'news' || searchType === 'all') {
|
||||
allEntitiesFetched = true;
|
||||
} else {
|
||||
// publications, patents, projects
|
||||
const fetchedI = new Set(
|
||||
untracked(() => this.fetchedInstitutions()).map((i) =>
|
||||
i.id.toLowerCase(),
|
||||
),
|
||||
);
|
||||
const fetchedC = new Set(
|
||||
untracked(() => this.fetchedCountries()).map((c) =>
|
||||
c.id.toLowerCase(),
|
||||
),
|
||||
);
|
||||
const fetchedR = new Set(
|
||||
untracked(() => this.fetchedRegions()).map((r) =>
|
||||
r.id.toLowerCase(),
|
||||
),
|
||||
);
|
||||
allEntitiesFetched =
|
||||
insts.every((i) => fetchedI.has(i.id.toLowerCase())) &&
|
||||
countries.every((c) => fetchedC.has(c.id.toLowerCase())) &&
|
||||
regions.every((r) => fetchedR.has(r.id.toLowerCase()));
|
||||
(insts.length === 0 ||
|
||||
insts.every((i) => {
|
||||
const cleanI = i.id.includes('/')
|
||||
? i.id.split('/').pop()!.toLowerCase()
|
||||
: i.id.toLowerCase();
|
||||
return rawSeriesCleanIds.has(cleanI);
|
||||
})) &&
|
||||
(countries.length === 0 ||
|
||||
countries.every((c) =>
|
||||
rawSeriesCleanIds.has(c.id.toLowerCase()),
|
||||
)) &&
|
||||
(regions.length === 0 ||
|
||||
regions.every((r) => rawSeriesCleanIds.has(r.id.toLowerCase())));
|
||||
}
|
||||
}
|
||||
|
||||
const hasResponse = !!untracked(() => this.rawTrendResponse());
|
||||
if (
|
||||
allTopicsFetched &&
|
||||
sameType &&
|
||||
sameFuzzy &&
|
||||
allEntitiesFetched &&
|
||||
hasResponse
|
||||
!!rawRes
|
||||
) {
|
||||
// All requested data is already present in rawTrendResponse, so skip backend call!
|
||||
return;
|
||||
@@ -1249,7 +1267,32 @@ export class UniTrendParentComponent implements OnInit {
|
||||
)
|
||||
.subscribe({
|
||||
next: (response: TrendResponse) => {
|
||||
this.rawTrendResponse.set(response);
|
||||
const currentRaw = untracked(() => this.rawTrendResponse());
|
||||
if (!currentRaw || !currentRaw.series) {
|
||||
this.rawTrendResponse.set(response);
|
||||
} else {
|
||||
const seriesMap = new Map<string, TrendSeries>();
|
||||
currentRaw.series.forEach((s) => {
|
||||
seriesMap.set(`${s.entity_type}_${s.entity_id}`, s);
|
||||
});
|
||||
response.series.forEach((s) => {
|
||||
const key = `${s.entity_type}_${s.entity_id}`;
|
||||
const prev = seriesMap.get(key);
|
||||
if (prev && prev.topic_data && s.topic_data) {
|
||||
seriesMap.set(key, {
|
||||
...prev,
|
||||
...s,
|
||||
topic_data: { ...prev.topic_data, ...s.topic_data },
|
||||
});
|
||||
} else {
|
||||
seriesMap.set(key, s);
|
||||
}
|
||||
});
|
||||
this.rawTrendResponse.set({
|
||||
...response,
|
||||
series: Array.from(seriesMap.values()),
|
||||
});
|
||||
}
|
||||
this.isTrendLoading.set(false);
|
||||
this.updateUrlParams();
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user