wip fix idb cache
This commit is contained in:
parent
6538c67789
commit
07b5ae6155
|
|
@ -43,10 +43,14 @@ export const page_cache: SAction["page"]["cache"] = async function (
|
||||||
if (found && !exclude_page_id.includes(found.id)) {
|
if (found && !exclude_page_id.includes(found.id)) {
|
||||||
const row = await db.page.findFirst({
|
const row = await db.page.findFirst({
|
||||||
where: { id: found.id },
|
where: { id: found.id },
|
||||||
select: { content_tree: true },
|
select: { content_tree: true, url: true },
|
||||||
});
|
});
|
||||||
if (row) {
|
if (row) {
|
||||||
result[found.id] = row.content_tree;
|
result[found.id] = {
|
||||||
|
root: row.content_tree,
|
||||||
|
url: row.url,
|
||||||
|
org_url: url,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,16 +1,15 @@
|
||||||
import { NodeModel } from "@minoru/react-dnd-treeview";
|
import { NodeModel } from "@minoru/react-dnd-treeview";
|
||||||
|
import { createId } from "@paralleldrive/cuid2";
|
||||||
import { compress, decompress } from "wasm-gzip";
|
import { compress, decompress } from "wasm-gzip";
|
||||||
import { IItem } from "../../../../utils/types/item";
|
import { IItem } from "../../../../utils/types/item";
|
||||||
import { DComp } from "../../../../utils/types/root";
|
import { DComp } from "../../../../utils/types/root";
|
||||||
import { initLoadComp } from "../../../vi/meta/comp/init-comp-load";
|
import { initLoadComp } from "../../../vi/meta/comp/init-comp-load";
|
||||||
import { genMeta } from "../../../vi/meta/meta";
|
import { genMeta } from "../../../vi/meta/meta";
|
||||||
|
import { isTextEditing } from "../active/is-editing";
|
||||||
import { IMeta, PG } from "../ed-global";
|
import { IMeta, PG } from "../ed-global";
|
||||||
|
import { assignMitem } from "../tree/assign-mitem";
|
||||||
import { treeRebuild } from "../tree/build";
|
import { treeRebuild } from "../tree/build";
|
||||||
import { pushTreeNode } from "../tree/build/push-tree";
|
import { pushTreeNode } from "../tree/build/push-tree";
|
||||||
import { isTextEditing } from "../active/is-editing";
|
|
||||||
import { assignMitem } from "../tree/assign-mitem";
|
|
||||||
import { createId } from "@paralleldrive/cuid2";
|
|
||||||
import { waitUntil } from "web-utils";
|
|
||||||
|
|
||||||
export const loadcomp = {
|
export const loadcomp = {
|
||||||
timeout: 0 as any,
|
timeout: 0 as any,
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,7 @@ import { clientStartSync } from "../../../utils/sync/ws-client";
|
||||||
import { IItem } from "../../../utils/types/item";
|
import { IItem } from "../../../utils/types/item";
|
||||||
import { DCode, DComp, DPage, IRoot } from "../../../utils/types/root";
|
import { DCode, DComp, DPage, IRoot } from "../../../utils/types/root";
|
||||||
import { GenMetaP, IMeta as LogicMeta } from "../../vi/utils/types";
|
import { GenMetaP, IMeta as LogicMeta } from "../../vi/utils/types";
|
||||||
|
import { createRouter } from "radix3";
|
||||||
export type IMeta = LogicMeta;
|
export type IMeta = LogicMeta;
|
||||||
|
|
||||||
export const EmptySite = {
|
export const EmptySite = {
|
||||||
|
|
@ -148,10 +148,11 @@ export const EDGlobal = {
|
||||||
| "ready",
|
| "ready",
|
||||||
preview: {
|
preview: {
|
||||||
url_cache: new Set<string>(),
|
url_cache: new Set<string>(),
|
||||||
page_cache: {} as Record<string, IRoot>,
|
route_cache: createRouter<{ url: string; id: string }>(),
|
||||||
|
page_cache: {} as Record<string, { root: IRoot; url: string }>,
|
||||||
meta_cache: {} as Record<
|
meta_cache: {} as Record<
|
||||||
string,
|
string,
|
||||||
{ entry: string[]; meta: Record<string, IMeta> }
|
{ entry: string[]; meta: Record<string, IMeta>; url: string }
|
||||||
>,
|
>,
|
||||||
show_loading: false,
|
show_loading: false,
|
||||||
},
|
},
|
||||||
|
|
|
||||||
|
|
@ -1,30 +1,74 @@
|
||||||
|
import { get, set } from "idb-keyval";
|
||||||
import { IContent } from "../../../../utils/types/general";
|
import { IContent } from "../../../../utils/types/general";
|
||||||
import { IItem, MItem } from "../../../../utils/types/item";
|
import { IItem, MItem } from "../../../../utils/types/item";
|
||||||
|
import { initLoadComp } from "../../../vi/meta/comp/init-comp-load";
|
||||||
import { genMeta } from "../../../vi/meta/meta";
|
import { genMeta } from "../../../vi/meta/meta";
|
||||||
|
import { nav } from "../../../vi/render/script/extract-nav";
|
||||||
|
import { loadCompSnapshot, loadComponent } from "../comp/load";
|
||||||
import { IMeta, PG, active } from "../ed-global";
|
import { IMeta, PG, active } from "../ed-global";
|
||||||
import { assignMitem } from "./assign-mitem";
|
import { assignMitem } from "./assign-mitem";
|
||||||
import { pushTreeNode } from "./build/push-tree";
|
import { pushTreeNode } from "./build/push-tree";
|
||||||
|
|
||||||
export const treeCacheBuild = async (p: PG, page_id: string) => {
|
export const treeCacheBuild = async (p: PG, page_id: string) => {
|
||||||
const root = p.preview.page_cache[page_id];
|
const page_cache = p.preview.page_cache[page_id];
|
||||||
|
if (page_cache && !p.preview.meta_cache[page_id]) {
|
||||||
|
const meta_cache = {
|
||||||
|
meta: {} as Record<string, IMeta>,
|
||||||
|
entry: [] as string[],
|
||||||
|
url: page_cache.url,
|
||||||
|
};
|
||||||
|
|
||||||
const meta_cache = {
|
await initLoadComp(
|
||||||
meta: {} as Record<string, IMeta>,
|
|
||||||
entry: [] as string[],
|
|
||||||
};
|
|
||||||
for (const item of root.childs) {
|
|
||||||
meta_cache.entry.push(item.id);
|
|
||||||
genMeta(
|
|
||||||
{
|
{
|
||||||
note: "tree-rebuild",
|
|
||||||
comps: p.comp.loaded,
|
comps: p.comp.loaded,
|
||||||
meta: meta_cache.meta,
|
meta: meta_cache.meta,
|
||||||
mode: "page",
|
mode: "page",
|
||||||
},
|
},
|
||||||
{ item: item as IContent }
|
page_cache.root as unknown as IItem,
|
||||||
|
{
|
||||||
|
async load(comp_ids) {
|
||||||
|
const ids = comp_ids.filter((id) => !p.comp.loaded[id]);
|
||||||
|
const comps = await p.sync.comp.load(ids, true);
|
||||||
|
let result = Object.entries(comps);
|
||||||
|
|
||||||
|
for (const [id_comp, comp] of result) {
|
||||||
|
const cached = await get(`comp-${id_comp}`, nav.store);
|
||||||
|
if (cached) {
|
||||||
|
p.comp.loaded[id_comp] = cached;
|
||||||
|
}
|
||||||
|
if (comp && comp.snapshot && !p.comp.list[id_comp]) {
|
||||||
|
if (p.comp.loaded[id_comp]) {
|
||||||
|
loadCompSnapshot(p, id_comp, comp.snapshot).then(() => {
|
||||||
|
if (p.comp.loaded[id_comp]) {
|
||||||
|
set(`comp-${id_comp}`, p.comp.loaded[id_comp], nav.store);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
await loadCompSnapshot(p, id_comp, comp.snapshot);
|
||||||
|
if (p.comp.loaded[id_comp]) {
|
||||||
|
set(`comp-${id_comp}`, p.comp.loaded[id_comp], nav.store);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
);
|
);
|
||||||
|
for (const item of page_cache.root.childs) {
|
||||||
|
meta_cache.entry.push(item.id);
|
||||||
|
genMeta(
|
||||||
|
{
|
||||||
|
note: "cache-rebuild",
|
||||||
|
comps: p.comp.loaded,
|
||||||
|
meta: meta_cache.meta,
|
||||||
|
mode: "page",
|
||||||
|
},
|
||||||
|
{ item: item as IContent }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
p.preview.meta_cache[page_id] = meta_cache;
|
||||||
|
set(`page-${page_id}`, meta_cache, nav.store);
|
||||||
}
|
}
|
||||||
p.preview.meta_cache[page_id] = meta_cache;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export const treeRebuild = async (p: PG, arg?: { note?: string }) => {
|
export const treeRebuild = async (p: PG, arg?: { note?: string }) => {
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,9 @@ import init, { decompress } from "wasm-gzip";
|
||||||
import { w } from "../../utils/types/general";
|
import { w } from "../../utils/types/general";
|
||||||
import { IRoot } from "../../utils/types/root";
|
import { IRoot } from "../../utils/types/root";
|
||||||
import { treeCacheBuild } from "../ed/logic/tree/build";
|
import { treeCacheBuild } from "../ed/logic/tree/build";
|
||||||
|
import { loadComponent } from "../ed/logic/comp/load";
|
||||||
|
import { get } from "idb-keyval";
|
||||||
|
import { nav } from "./render/script/extract-nav";
|
||||||
|
|
||||||
const decoder = new TextDecoder();
|
const decoder = new TextDecoder();
|
||||||
export const ViPreview = (arg: { pathname: string }) => {
|
export const ViPreview = (arg: { pathname: string }) => {
|
||||||
|
|
@ -52,10 +55,11 @@ export const ViPreview = (arg: { pathname: string }) => {
|
||||||
|
|
||||||
const mode = p.mode;
|
const mode = p.mode;
|
||||||
|
|
||||||
if (!w.isEditor) {
|
if (!w.isEditor && !p.preview.meta_cache[params.page_id]) {
|
||||||
p.preview.meta_cache[params.page_id] = {
|
p.preview.meta_cache[params.page_id] = {
|
||||||
meta: p.page.meta,
|
meta: p.page.meta,
|
||||||
entry: p.page.entry,
|
entry: p.page.entry,
|
||||||
|
url: p.page.cur.url,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -113,20 +117,23 @@ export const ViPreview = (arg: { pathname: string }) => {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (load_urls.length > 0) {
|
if (load_urls.length > 0) {
|
||||||
console.log(load_urls);
|
|
||||||
const res = await p.sync.page.cache(
|
const res = await p.sync.page.cache(
|
||||||
p.site.id,
|
p.site.id,
|
||||||
load_urls,
|
load_urls,
|
||||||
Object.keys(p.preview.page_cache)
|
Object.keys(p.preview.page_cache)
|
||||||
);
|
);
|
||||||
|
|
||||||
if (res) {
|
if (res) {
|
||||||
const pages = JSON.parse(
|
const pages = JSON.parse(
|
||||||
decoder.decode(decompress(res.gzip)) || "{}"
|
decoder.decode(decompress(res.gzip)) || "{}"
|
||||||
);
|
) as Record<
|
||||||
|
string,
|
||||||
|
{ root: IRoot; url: string; org_url: string }
|
||||||
|
>;
|
||||||
|
|
||||||
for (const [id, page] of Object.entries(pages)) {
|
for (const [id, page] of Object.entries(pages)) {
|
||||||
p.preview.page_cache[id] = page as IRoot;
|
p.preview.page_cache[id] = page;
|
||||||
treeCacheBuild(p, params.page_id);
|
await treeCacheBuild(p, id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -172,7 +179,17 @@ const viRoute = async (p: PG) => {
|
||||||
p.script.init_local_effect = {};
|
p.script.init_local_effect = {};
|
||||||
|
|
||||||
if (!w.isEditor) {
|
if (!w.isEditor) {
|
||||||
const page_cache = p.preview.meta_cache[params.page_id];
|
let page_cache = p.preview.meta_cache[params.page_id];
|
||||||
|
|
||||||
|
let should_render = false;
|
||||||
|
if (!page_cache) {
|
||||||
|
const idb_cache = await get(`page-${params.page_id}`, nav.store);
|
||||||
|
if (idb_cache) {
|
||||||
|
page_cache = idb_cache;
|
||||||
|
p.preview.meta_cache[params.page_id] = idb_cache;
|
||||||
|
}
|
||||||
|
should_render = true;
|
||||||
|
}
|
||||||
|
|
||||||
if (page_cache) {
|
if (page_cache) {
|
||||||
p.page.meta = page_cache.meta;
|
p.page.meta = page_cache.meta;
|
||||||
|
|
@ -182,7 +199,7 @@ const viRoute = async (p: PG) => {
|
||||||
p.page.cur = { id: params.page_id } as any;
|
p.page.cur = { id: params.page_id } as any;
|
||||||
}
|
}
|
||||||
p.status = "ready";
|
p.status = "ready";
|
||||||
p.render();
|
if (should_render) p.render();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,8 @@
|
||||||
|
import { createStore } from "idb-keyval";
|
||||||
import { VG } from "../global";
|
import { VG } from "../global";
|
||||||
|
|
||||||
const nav = { timeout: null as any };
|
const store = createStore(`prasi-cache`, `prasi-cache-store`);
|
||||||
|
export const nav = { timeout: null as any, store };
|
||||||
|
|
||||||
export const extractNavigate = (
|
export const extractNavigate = (
|
||||||
vi: { page: VG["page"]; on_nav_loaded?: VG["on_nav_loaded"] },
|
vi: { page: VG["page"]; on_nav_loaded?: VG["on_nav_loaded"] },
|
||||||
|
|
|
||||||
|
|
@ -1,12 +1,12 @@
|
||||||
import { FC, Suspense, useEffect, useState } from "react";
|
import { FC, Suspense, useEffect } from "react";
|
||||||
import { useGlobal } from "web-utils";
|
import { useGlobal } from "web-utils";
|
||||||
import { IMeta } from "../ed/logic/ed-global";
|
import { IMeta } from "../ed/logic/ed-global";
|
||||||
import { viLoad } from "./load/load";
|
import { viLoad } from "./load/load";
|
||||||
import { VG, ViGlobal } from "./render/global";
|
import { VG, ViGlobal } from "./render/global";
|
||||||
|
import { render_stat } from "./render/render";
|
||||||
|
import { nav } from "./render/script/extract-nav";
|
||||||
import { ViRoot } from "./root";
|
import { ViRoot } from "./root";
|
||||||
import { ErrorBox } from "./utils/error-box";
|
import { ErrorBox } from "./utils/error-box";
|
||||||
import { render_stat } from "./render/render";
|
|
||||||
import { IRoot } from "../../utils/types/root";
|
|
||||||
|
|
||||||
const w = window as any;
|
const w = window as any;
|
||||||
export const Vi: FC<{
|
export const Vi: FC<{
|
||||||
|
|
@ -46,11 +46,20 @@ export const Vi: FC<{
|
||||||
w.isMobile = mode === "mobile";
|
w.isMobile = mode === "mobile";
|
||||||
w.isDesktop = mode === "desktop";
|
w.isDesktop = mode === "desktop";
|
||||||
w.preload = (urls: string[]) => {
|
w.preload = (urls: string[]) => {
|
||||||
|
if (!vi.page.navs[page_id]) vi.page.navs[page_id] = new Set();
|
||||||
for (const url of urls) {
|
for (const url of urls) {
|
||||||
vi.page.navs[page_id].add(url);
|
vi.page.navs[page_id].add(url);
|
||||||
}
|
}
|
||||||
|
clearTimeout(nav.timeout);
|
||||||
|
nav.timeout = setTimeout(() => {
|
||||||
|
if (vi.on_nav_loaded) {
|
||||||
|
vi.on_nav_loaded({
|
||||||
|
urls: Array.from(vi.page.navs[page_id]),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}, 100);
|
||||||
};
|
};
|
||||||
|
|
||||||
vi.page.cur.id = page_id;
|
vi.page.cur.id = page_id;
|
||||||
vi.on_status_changes = on_status_changed;
|
vi.on_status_changes = on_status_changed;
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue