-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathSourcesProcessor.ts
47 lines (42 loc) · 1.61 KB
/
SourcesProcessor.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
import { COMBINED } from "utils/constants";
import { ICombined, ISources } from "utils/definitions";
import { logger } from "utils/logger";
import { fetch as fetchPolyfill } from "whatwg-fetch";
import { BaseSourcesProcessor } from "./BaseSourcesProcessor";
import { UpdatedSourcesMessage } from "utils/messages/UpdatedSourcesMessage";
const log = logger("mbfc:background:sources");
export class SourcesProcessor extends BaseSourcesProcessor {
retrievingPromise: Promise<ISources> | undefined;
private static instance: SourcesProcessor;
static getInstance(combined?: ICombined): SourcesProcessor {
if (!SourcesProcessor.instance) {
SourcesProcessor.instance = new SourcesProcessor();
if (combined) {
SourcesProcessor.instance.initializeCombined(combined);
}
log("SourcesProcessor initialized");
}
return SourcesProcessor.instance;
}
async getSources(): Promise<ISources> {
if (this.areSourcesLoaded()) return this.sources;
if (!this.retrievingPromise) this.retrievingPromise = this.retrieveRemote();
return this.retrievingPromise;
}
async retrieveRemote(): Promise<ISources> {
try {
const res = await fetchPolyfill(COMBINED);
const combined: ICombined = await res.json();
console.log(
`Loaded combined data ${combined.version} from ${combined.date}`
);
if (!combined) return this.sources;
this.initializeCombined(combined);
const msg = new UpdatedSourcesMessage(this.sources);
await msg.sendMessage(true);
} catch (err) {
console.error(`ERROR Loading sources: `, err);
}
return this.sources;
}
}