From 2270c7c071dc5f67fa3b6f4d9e65b6c4da8262c5 Mon Sep 17 00:00:00 2001 From: Rizky Date: Tue, 24 Oct 2023 17:49:49 +0700 Subject: [PATCH] fix --- app/srv/ws/sync/actions/yjs_diff_local.ts | 1 + app/srv/ws/sync/actions/yjs_sv_local.ts | 1 + app/srv/ws/sync/actions/yjs_sv_remote.ts | 1 + app/web/src/render/ed/logic/ed-global.ts | 1 + app/web/src/render/ed/logic/ed-route.ts | 7 ++++++- app/web/src/render/ed/logic/ed-sync.tsx | 21 +++++++++++++++++---- app/web/src/utils/sync/ws-client.ts | 6 ++++-- app/web/src/utils/types/root.ts | 8 ++++++-- 8 files changed, 37 insertions(+), 9 deletions(-) diff --git a/app/srv/ws/sync/actions/yjs_diff_local.ts b/app/srv/ws/sync/actions/yjs_diff_local.ts index 052fbbfe..69895987 100644 --- a/app/srv/ws/sync/actions/yjs_diff_local.ts +++ b/app/srv/ws/sync/actions/yjs_diff_local.ts @@ -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; diff --git a/app/srv/ws/sync/actions/yjs_sv_local.ts b/app/srv/ws/sync/actions/yjs_sv_local.ts index 3b682314..0d952358 100644 --- a/app/srv/ws/sync/actions/yjs_sv_local.ts +++ b/app/srv/ws/sync/actions/yjs_sv_local.ts @@ -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; diff --git a/app/srv/ws/sync/actions/yjs_sv_remote.ts b/app/srv/ws/sync/actions/yjs_sv_remote.ts index eb9b90df..5246d37a 100644 --- a/app/srv/ws/sync/actions/yjs_sv_remote.ts +++ b/app/srv/ws/sync/actions/yjs_sv_remote.ts @@ -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; diff --git a/app/web/src/render/ed/logic/ed-global.ts b/app/web/src/render/ed/logic/ed-global.ts index 53bb7cd0..aa98c795 100644 --- a/app/web/src/render/ed/logic/ed-global.ts +++ b/app/web/src/render/ed/logic/ed-global.ts @@ -87,6 +87,7 @@ export const EDGlobal = { group: {} as Record>>, }, ui: { + syncing: false, tree: { item_loading: [] as string[], search: "", diff --git a/app/web/src/render/ed/logic/ed-route.ts b/app/web/src/render/ed/logic/ed-route.ts index 9ae3a73b..6e4bf3b2 100644 --- a/app/web/src/render/ed/logic/ed-route.ts +++ b/app/web/src/render/ed/logic/ed-route.ts @@ -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(); } }); diff --git a/app/web/src/render/ed/logic/ed-sync.tsx b/app/web/src/render/ed/logic/ed-sync.tsx index aa9c8c5e..dad9bf9b 100644 --- a/app/web/src/render/ed/logic/ed-sync.tsx +++ b/app/web/src/render/ed/logic/ed-sync.tsx @@ -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(); } - 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; diff --git a/app/web/src/utils/sync/ws-client.ts b/app/web/src/utils/sync/ws-client.ts index 4b22ce03..0d12e387 100644 --- a/app/web/src/utils/sync/ws-client.ts +++ b/app/web/src/utils/sync/ws-client.ts @@ -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(); } diff --git a/app/web/src/utils/types/root.ts b/app/web/src/utils/types/root.ts index ae38e22b..33d76bbe 100644 --- a/app/web/src/utils/types/root.ts +++ b/app/web/src/utils/types/root.ts @@ -14,5 +14,9 @@ export type MRoot = TypedMap<{ childs: TypedArray; }>; -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 }>; +}>;