This commit is contained in:
Rizky 2024-02-05 16:16:50 +07:00
parent a9576969fa
commit fa46235a3d
4 changed files with 40 additions and 17 deletions

View File

@ -26,16 +26,20 @@ export const loadComponent = async (p: PG, id_comp: string, sync?: boolean) => {
loadcomp.pending.add(id_comp);
clearTimeout(loadcomp.timeout);
loadcomp.timeout = setTimeout(async () => {
const comps = await p.sync.comp.load([...loadcomp.pending], sync);
let result = Object.entries(comps);
const comps = await p.sync?.comp.load([...loadcomp.pending], sync);
if (comps) {
let result = Object.entries(comps);
for (const [id_comp, comp] of result) {
if (comp && comp.snapshot) {
await loadCompSnapshot(p, id_comp, comp.snapshot);
for (const [id_comp, comp] of result) {
if (comp && comp.snapshot) {
await loadCompSnapshot(p, id_comp, comp.snapshot);
}
}
loadcomp.pending.clear();
resolve(result.length > 0);
} else {
resolve(false);
}
loadcomp.pending.clear();
resolve(result.length > 0);
}, 150);
});
};
@ -73,7 +77,7 @@ export const loadCompSnapshot = async (
return;
}
const res = await p.sync.yjs.sv_local(
const res = await p.sync?.yjs.sv_local(
"comp",
comp_id,
Buffer.from(compress(bin))
@ -86,7 +90,7 @@ export const loadCompSnapshot = async (
);
Y.applyUpdate(doc as any, decompress(res.diff), "local");
await p.sync.yjs.diff_local(
await p.sync?.yjs.diff_local(
"comp",
comp_id,
Buffer.from(compress(diff_local))
@ -144,12 +148,14 @@ export const updateComponentMeta = async (
{
load: async (comp_ids: string[]) => {
const ids = comp_ids.filter((id) => !p.comp.loaded[id]);
const comps = await p.sync.comp.load(ids, true);
let result = Object.entries(comps);
const comps = await p.sync?.comp.load(ids, true);
if (comps) {
let result = Object.entries(comps);
for (const [id_comp, comp] of result) {
if (comp && comp.snapshot && !p.comp.list[id_comp]) {
await loadCompSnapshot(p, id_comp, comp.snapshot);
for (const [id_comp, comp] of result) {
if (comp && comp.snapshot && !p.comp.list[id_comp]) {
await loadCompSnapshot(p, id_comp, comp.snapshot);
}
}
}
},

View File

@ -172,6 +172,12 @@ export const reloadPage = async (
page.on_update = async (bin: Uint8Array, origin: any) => {
if (origin === "local" || !p.sync) return;
if (page.page.id !== remotePage.id) {
alert("Page ID Mismatch!\n Refreshing to preventing data loss...");
location.reload();
return;
}
const res = await p.sync.yjs.sv_local(
"page",
p.page.cur.id,

View File

@ -4,8 +4,6 @@ import { PG } from "../../ed/logic/ed-global";
import { evalCJS } from "../../ed/logic/ed-sync";
import { treeRebuild } from "../../ed/logic/tree/build";
const decoder = new TextDecoder();
export const viLoadSnapshot = async (p: PG) => {
let api_url = p.site.config.api_url;

View File

@ -210,6 +210,20 @@ const viRoute = async (p: PG) => {
if (preview.first_render) {
preview.first_render = false;
} else {
if (p.page.doc) {
let page = p.page.list[params.page_id];
if (!page) {
p.page.list[params.page_id] = {} as any;
page = p.page.list[params.page_id];
}
if (page && page.on_update) {
page.doc.off("update", page.on_update);
page.doc.destroy();
delete p.page.list[params.page_id];
}
}
let page_cache = p.preview.meta_cache[params.page_id];
let should_render = false;
@ -231,7 +245,6 @@ const viRoute = async (p: PG) => {
}
p.status = "ready";
p.sync.page.load(params.page_id);
if (should_render) p.render();
return;
}