feat: load news and calendars from cache on startup
Also check calendars for cache before trying to load
This commit is contained in:
parent
5b221fb72d
commit
1cc7e12a57
2 changed files with 35 additions and 4 deletions
|
|
@ -134,6 +134,7 @@ export default class Calendar extends GObject.Object {
|
||||||
icalStr = cal.value;
|
icalStr = cal.value;
|
||||||
} else {
|
} else {
|
||||||
console.error(`Failed to get calendar from ${config.webcals.get()[i]}:\n${cal.reason}`);
|
console.error(`Failed to get calendar from ${config.webcals.get()[i]}:\n${cal.reason}`);
|
||||||
|
if (GLib.file_test(`${this.#cacheDir}/${webcal}`, GLib.FileTest.EXISTS))
|
||||||
icalStr = await readFileAsync(`${this.#cacheDir}/${webcal}`);
|
icalStr = await readFileAsync(`${this.#cacheDir}/${webcal}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -202,6 +203,23 @@ export default class Calendar extends GObject.Object {
|
||||||
|
|
||||||
GLib.mkdir_with_parents(this.#cacheDir, 0o755);
|
GLib.mkdir_with_parents(this.#cacheDir, 0o755);
|
||||||
|
|
||||||
|
const cals = config.webcals.get().map(async c => {
|
||||||
|
const webcal = pathToFileName(c);
|
||||||
|
|
||||||
|
if (GLib.file_test(`${this.#cacheDir}/${webcal}`, GLib.FileTest.EXISTS)) {
|
||||||
|
const data = await readFileAsync(`${this.#cacheDir}/${webcal}`);
|
||||||
|
const comp = new ical.Component(ical.parse(data));
|
||||||
|
const name = (comp.getFirstPropertyValue("x-wr-calname") ?? `Calendar ${this.#calCount++}`) as string;
|
||||||
|
this.#calendars[name] = comp;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
Promise.allSettled(cals).then(() => {
|
||||||
|
this.#cachedEvents = {};
|
||||||
|
this.#cachedMonths.clear();
|
||||||
|
this.notify("calendars");
|
||||||
|
this.updateUpcoming();
|
||||||
|
});
|
||||||
|
|
||||||
this.updateCalendars().catch(console.error);
|
this.updateCalendars().catch(console.error);
|
||||||
config.webcals.subscribe(() => this.updateCalendars().catch(console.error));
|
config.webcals.subscribe(() => this.updateCalendars().catch(console.error));
|
||||||
config.upcomingDays.subscribe(() => this.updateUpcoming());
|
config.upcomingDays.subscribe(() => this.updateUpcoming());
|
||||||
|
|
|
||||||
|
|
@ -106,6 +106,13 @@ export default class News extends GObject.Object {
|
||||||
}
|
}
|
||||||
this.notify("articles");
|
this.notify("articles");
|
||||||
|
|
||||||
|
this.updateCategories();
|
||||||
|
|
||||||
|
this.#loading = false;
|
||||||
|
this.notify("loading");
|
||||||
|
}
|
||||||
|
|
||||||
|
updateCategories() {
|
||||||
this.#categories = {};
|
this.#categories = {};
|
||||||
for (const article of this.#articles) {
|
for (const article of this.#articles) {
|
||||||
for (const category of article.category) {
|
for (const category of article.category) {
|
||||||
|
|
@ -114,14 +121,20 @@ export default class News extends GObject.Object {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
this.notify("categories");
|
this.notify("categories");
|
||||||
|
|
||||||
this.#loading = false;
|
|
||||||
this.notify("loading");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
super();
|
super();
|
||||||
|
|
||||||
|
if (GLib.file_test(this.#cachePath, GLib.FileTest.EXISTS))
|
||||||
|
readFileAsync(this.#cachePath)
|
||||||
|
.then(data => {
|
||||||
|
this.#articles = JSON.parse(data);
|
||||||
|
this.notify("articles");
|
||||||
|
this.updateCategories();
|
||||||
|
})
|
||||||
|
.catch(console.error);
|
||||||
|
|
||||||
this.getNews().catch(console.error);
|
this.getNews().catch(console.error);
|
||||||
config.apiKey.subscribe(() => this.getNews().catch(console.error));
|
config.apiKey.subscribe(() => this.getNews().catch(console.error));
|
||||||
config.countries.subscribe(() => this.getNews().catch(console.error));
|
config.countries.subscribe(() => this.getNews().catch(console.error));
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue