wip fix cache

This commit is contained in:
Rizky 2024-02-09 21:04:11 +07:00
parent d28fa4c141
commit e40db345ec
3 changed files with 61 additions and 13 deletions

View File

@ -1,8 +1,10 @@
import { get, set } from "idb-keyval";
import { IContent } from "../../../utils/types/general";
import { IItem } from "../../../utils/types/item";
import { ISection } from "../../../utils/types/section";
import { base } from "./base";
import { decompressBlob } from "./util";
import { prodCache } from "./cache";
export const scanComponent = async (items: IContent[]) => {
const comp = base.comp;
@ -13,6 +15,29 @@ export const scanComponent = async (items: IContent[]) => {
}
}
console.log(comp.pending);
if (comp.pending.size > 0) {
let all_found = true;
const founds: any = [];
for (const id of [...comp.pending]) {
const item = await get(`comp-${id}`, prodCache);
if (!item) {
all_found = false;
}
comp.list[id] = item;
founds.push(item);
}
if (all_found) {
for (const id of [...comp.pending]) {
comp.pending.delete(id);
}
await scanComponent(founds);
return;
}
}
if (comp.pending.size > 0) {
try {
const raw = await (
@ -27,6 +52,8 @@ export const scanComponent = async (items: IContent[]) => {
for (const [id, item] of Object.entries(res)) {
comp.pending.delete(id);
comp.list[id] = item;
await set(`comp-${id}`, item, prodCache);
}
await scanComponent(Object.values(res));
} catch (e) {}

View File

@ -43,16 +43,17 @@ export const loadPages = (page_ids: string[]) => {
>(async (done) => {
const result: any = {};
const ids = [...new Set(page_ids)];
let is_complete = true;
let is_done = true;
for (const id of ids) {
const page = await get(`page-${id}`, prodCache);
if (page) {
result[id] = page;
} else {
is_complete = false;
is_done = false;
break;
}
}
if (is_complete) {
if (is_done) {
done(result);
}
@ -72,7 +73,7 @@ export const loadPages = (page_ids: string[]) => {
set(`page-${k}`, v, prodCache);
}
if (!is_complete) {
if (!is_done) {
done(res);
}
});

View File

@ -6,19 +6,39 @@ import { genMeta } from "../../vi/meta/meta";
import { IMeta } from "../../vi/utils/types";
import { base } from "./base";
import { decompressBlob } from "./util";
import { prodCache } from "./cache";
import { get, set } from "idb-keyval";
const getRoute = () => {
return new Promise<{
site: any;
urls: {
id: string;
url: string;
}[];
layout: any;
}>(async (done) => {
let is_done = false;
const route_cache = await get("route", prodCache);
if (route_cache) {
done(route_cache);
is_done = true;
}
let raw = await (await fetch(base.url`_prasi/route`)).blob();
const res = JSON.parse(await (await decompressBlob(raw)).text());
await set("route", res, prodCache);
if (!is_done) {
done(res);
}
});
};
export const initBaseRoute = async () => {
const raw = await (await fetch(base.url`_prasi/route`)).blob();
const router = createRouter<{ id: string; url: string }>();
try {
const res = JSON.parse(await (await decompressBlob(raw)).text()) as {
site: any;
urls: {
id: string;
url: string;
}[];
layout: any;
};
const res = await getRoute();
if (res && res.site && res.urls) {
if (res.layout) {