/
AnalysisSubAnalysesManager.ts
51 lines (42 loc) · 1.45 KB
/
AnalysisSubAnalysesManager.ts
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
import { AutoFetchLevels, FetchOptions, GetOptions } from '..';
import { Analysis, SubAnalysis, CachedManager } from '.';
/**
* This Manager is responsible for fetching and caching the SubAnalyses of an Analysis.
*/
export class AnalysisSubAnalysesManager extends CachedManager<string, SubAnalysis> {
/**
* The Analysis this Manager relates to.
*/
analysis: Analysis;
/**
* Fetch all the SubAnalyses from the API and store it in the cache.
*/
async fetchAll(options?: FetchOptions): Promise<SubAnalysis[]> {
const results = await this.client.raw.getSubAnalyses(this.analysis.id);
const subAnalyses = results.map((v) => new SubAnalysis(this.analysis, v));
const autoFetches = [];
for (const sub of subAnalyses) {
if (options?.shouldCache ?? this.client.options.enableCache) this.cache.set(sub.id, sub);
if (
options?.autoFetch
? options?.autoFetch.includes(AutoFetchLevels.Metadata)
: this.client.options.autoFetch?.includes(AutoFetchLevels.Metadata)
)
autoFetches.push(sub.fetchMetadata(options));
}
await Promise.all(autoFetches);
return subAnalyses;
}
/**
* Fetch all the SubAnalyses from either the cache or by fetching it.
*/
async getAll(options?: GetOptions): Promise<SubAnalysis[]> {
return !options?.skipCache && this.cache.size > 0
? Array.from(this.cache.values())
: await this.fetchAll(options);
}
constructor(analysis: Analysis) {
super(analysis.client);
this.analysis = analysis;
}
}