This commit is contained in:
Rizky 2023-10-24 17:49:49 +07:00
parent db347a728b
commit 2270c7c071
8 changed files with 37 additions and 9 deletions

View File

@ -10,6 +10,7 @@ export const yjs_diff_local: SAction["yjs"]["diff_local"] = async function (
bin
) {
if (!docs[mode][id]) {
console.log(`diff_local not found`, mode, id);
return;
}
const doc = docs[mode][id].doc as Y.Doc;

View File

@ -10,6 +10,7 @@ export const yjs_sv_local: SAction["yjs"]["sv_local"] = async function (
bin
) {
if (!docs[mode][id]) {
console.log(`sv_local not found`, mode, id);
return;
}
const doc = docs[mode][id].doc as Y.Doc;

View File

@ -11,6 +11,7 @@ export const yjs_sv_remote: SAction["yjs"]["sv_remote"] = async function (
diff
) {
if (!docs[mode][id]) {
console.log(`sv_remote not found`, mode, id);
return;
}
const doc = docs[mode][id].doc;

View File

@ -87,6 +87,7 @@ export const EDGlobal = {
group: {} as Record<string, Awaited<ReturnType<SAction["comp"]["group"]>>>,
},
ui: {
syncing: false,
tree: {
item_loading: [] as string[],
search: "",

View File

@ -31,8 +31,9 @@ export const edRoute = async (p: PG) => {
const doc = new Y.Doc();
Y.applyUpdate(doc, decompress(page.snapshot));
doc.on("update", async (bin: Uint8Array, origin: any) => {
if (origin === "sv_remote") return;
if (origin === "sv_remote" || origin === "local") return;
console.log(origin);
const res = await p.sync.yjs.sv_local(
"page",
p.page.cur.id,
@ -45,11 +46,15 @@ export const edRoute = async (p: PG) => {
decompress(res.sv)
);
Y.applyUpdate(doc as any, decompress(res.diff), "local");
await treeRebuild(p);
await p.sync.yjs.diff_local(
"page",
p.page.cur.id,
Buffer.from(compress(diff_local))
);
p.ui.syncing = false;
p.render();
}
});

View File

@ -21,12 +21,16 @@ export const edInitSync = (p: PG) => {
site_id: params.site_id,
page_id: params.page_id,
events: {
connected() {
async connected() {
if (w.offline) {
console.log("reconnected, syncing...");
}
w.offline = false;
p.ui.syncing = true;
p.render();
} else {
w.offline = false;
p.render();
}
},
disconnected() {
console.log("offline, reconnecting...");
@ -36,7 +40,16 @@ export const edInitSync = (p: PG) => {
reconnect: true,
};
},
editor_start(e) {
async editor_start(e) {
if (p.ui.syncing) {
await p.sync.page.load(params.page_id);
if (p.page.doc) {
p.page.doc.transact(() => {
p.page.doc?.getMap("map").set("ts", Date.now());
}, `sync`);
}
}
if (params.site_id !== e.site_id || params.page_id !== e.page_id) {
p.site.id = e.site_id;
p.page.cur.id = e.page_id;

View File

@ -15,7 +15,9 @@ import { w } from "../types/general";
import { initIDB } from "./idb";
const packr = new Packr({ structuredClone: true });
const WS_DEBUG = true;
/** CONSTANT */
const WS_DEBUG = false;
const RECONNECT_TIMEOUT = 1000;
const conf = {
ws: null as null | WebSocket,
@ -134,7 +136,7 @@ const connect = (
setTimeout(async () => {
reconnect++;
retry();
}, reconnect * 5000);
}, reconnect * RECONNECT_TIMEOUT);
} else {
reject();
}

View File

@ -14,5 +14,9 @@ export type MRoot = TypedMap<{
childs: TypedArray<ISection>;
}>;
export type DPage = TypedDoc<{ map: TypedMap<{ id: string; root: MRoot }> }>;
export type DComp = TypedDoc<{ map: TypedMap<{ id: string; root: MItem }> }>;
export type DPage = TypedDoc<{
map: TypedMap<{ id: string; root: MRoot; ts?: number }>;
}>;
export type DComp = TypedDoc<{
map: TypedMap<{ id: string; root: MItem; ts?: number }>;
}>;