From 5522b5fa3380e08b51bfb57aebf0bfef50b57647 Mon Sep 17 00:00:00 2001 From: Rizky Date: Thu, 11 Jan 2024 13:24:56 +0700 Subject: [PATCH] wip fix --- app/srv/ws/sync/actions.ts | 2 +- app/srv/ws/sync/actions/code_load.ts | 31 ++------- app/srv/ws/sync/actions/yjs_diff_local.ts | 24 +++---- app/srv/ws/sync/editor/code/prep-code.ts | 80 +++++++++++++++++++++++ app/srv/ws/sync/entity/snapshot.ts | 22 ++++--- 5 files changed, 111 insertions(+), 48 deletions(-) diff --git a/app/srv/ws/sync/actions.ts b/app/srv/ws/sync/actions.ts index a35e77fd..1a542b6b 100644 --- a/app/srv/ws/sync/actions.ts +++ b/app/srv/ws/sync/actions.ts @@ -95,7 +95,7 @@ export const SyncActions = { ({}) as Record, }, code: { - load: async (id: string, type: "src" | "built") => + load: async (id: string, type: "src" | "build") => ({}) as { id: string; snapshot: null | Uint8Array; diff --git a/app/srv/ws/sync/actions/code_load.ts b/app/srv/ws/sync/actions/code_load.ts index b698df40..5e2f1035 100644 --- a/app/srv/ws/sync/actions/code_load.ts +++ b/app/srv/ws/sync/actions/code_load.ts @@ -1,41 +1,20 @@ -import { dir } from "dir"; -import { g } from "utils/global"; -import { DCode } from "../../../../web/src/utils/types/root"; -import { readDirectoryRecursively } from "../../../api/site-export"; import { SAction } from "../actions"; -import { docs } from "../entity/docs"; +import { getCode, prepDCode } from "../editor/code/prep-code"; import { SyncConnection } from "../type"; -import { getCode } from "../editor/code/prep-code"; + export const code_load: SAction["code"]["load"] = async function ( this: SyncConnection, site_id, type ) { - let result = null as unknown as Awaited>; - const code = await getCode(site_id, "site"); if (code) { - if (!docs.code[site_id]) { - docs.code[site_id] = { - id: site_id, - src: loadFolderAsDCode(dir.path(`${g.datadir}/site/code/${code.id}`)), - build: loadFolderAsDCode( - dir.path(`${g.datadir}/site/build/${code.id}`) - ), - }; + const prep = await prepDCode(site_id); + if (prep) { + return { id: site_id, snapshot: prep.bin[type] }; } - - return result; } return { id: site_id, snapshot: null }; }; - -const loadFolderAsDCode = (path: string) => { - const doc = new Y.Doc() as DCode; - - const dirs = readDirectoryRecursively(path); - - return doc; -}; diff --git a/app/srv/ws/sync/actions/yjs_diff_local.ts b/app/srv/ws/sync/actions/yjs_diff_local.ts index 2a8933d5..dda8ea46 100644 --- a/app/srv/ws/sync/actions/yjs_diff_local.ts +++ b/app/srv/ws/sync/actions/yjs_diff_local.ts @@ -23,19 +23,19 @@ export const yjs_diff_local: SAction["yjs"]["diff_local"] = async function ( const root = doc.getMap("map").get("root") as any; if (root) { if (mode === "page") { - await db.page.update({ - where: { id }, - data: { - content_tree: root.toJSON(), - }, - }); + // await db.page.update({ + // where: { id }, + // data: { + // content_tree: root.toJSON(), + // }, + // }); } else if (mode === "comp") { - await db.component.update({ - where: { id }, - data: { - content_tree: root.toJSON(), - }, - }); + // await db.component.update({ + // where: { id }, + // data: { + // content_tree: root.toJSON(), + // }, + // }); } } } diff --git a/app/srv/ws/sync/editor/code/prep-code.ts b/app/srv/ws/sync/editor/code/prep-code.ts index cb1d5604..45ea7f9f 100644 --- a/app/srv/ws/sync/editor/code/prep-code.ts +++ b/app/srv/ws/sync/editor/code/prep-code.ts @@ -1,6 +1,12 @@ import { dir } from "dir"; import { dirAsync } from "fs-jetpack"; import { g } from "utils/global"; +import { Doc } from "yjs"; +import { DCode } from "../../../../../web/src/utils/types/root"; +import { readDirectoryRecursively } from "../../../../api/site-export"; +import { docs } from "../../entity/docs"; +import { existsAsync } from "fs-jetpack"; +import { snapshot } from "../../entity/snapshot"; export type DBCode = Exclude>, null>; export const prepCode = async (site_id: string, name: string) => { @@ -26,6 +32,8 @@ export const prepCode = async (site_id: string, name: string) => { if (code) { await dirAsync(dir.path(`${g.datadir}/site/code/${site_id}/${code.id}`)); + + await prepDCode(site_id); return code; } let new_code = await db.code.create({ @@ -62,6 +70,8 @@ export const hello_world = () => { code = await getCode(site_id); + await prepDCode(site_id); + return code as DBCode; }; @@ -80,3 +90,73 @@ export const getCode = async (site_id: string, name?: string) => { }, }); }; + +export const prepDCode = async (site_id: string) => { + let exists = false; + if (!docs.code[site_id]) { + const code = await getCode(site_id, "site"); + + if (code) { + const path = { + src: dir.path(`${g.datadir}/site/code/${site_id}/${code.id}`), + build: dir.path(`${g.datadir}/site/build/${code.id}`), + }; + + if ((await existsAsync(path.src)) && (await existsAsync(path.build))) { + docs.code[site_id] = { + id: site_id, + src: loadFolderAsDCode(code.id, path.src), + build: loadFolderAsDCode(code.id, path.build), + }; + exists = true; + } + } + } + + if (exists) { + const src_bin = Y.encodeStateAsUpdate(docs.code[site_id].src as Doc); + const build_bin = Y.encodeStateAsUpdate(docs.code[site_id].build as Doc); + + let snap = await snapshot.getOrCreate({ + type: "site", + id: site_id, + name: "", + src: { + bin: src_bin, + id_doc: docs.code[site_id].src.clientID, + }, + build: { + bin: build_bin, + id_doc: docs.code[site_id].build.clientID, + }, + }); + + if (snap && snap.type === "site") { + return { + bin: { + src: snap.src.bin, + build: snap.build.bin, + }, + }; + } + } +}; + +const loadFolderAsDCode = (id: string, path: string) => { + const doc = new Y.Doc() as DCode; + const map = doc.getMap("map"); + + const files = new Y.Map(); + + const dirs = readDirectoryRecursively(path); + for (const [k, v] of Object.entries(dirs)) { + files.set(k, new Y.Text(v)); + } + + doc.transact(() => { + map.set("files", files as any); + map.set("id", id); + }); + + return doc; +}; diff --git a/app/srv/ws/sync/entity/snapshot.ts b/app/srv/ws/sync/entity/snapshot.ts index 13c9755d..303e4685 100644 --- a/app/srv/ws/sync/entity/snapshot.ts +++ b/app/srv/ws/sync/entity/snapshot.ts @@ -32,10 +32,9 @@ type PageSnapshot = { type SiteSnapshot = { type: "site"; id: string; - id_doc: number; name: string; - src_bin: Uint8Array; - build_bin: Uint8Array; + src: { bin: Uint8Array; id_doc: number }; + build: { bin: Uint8Array; id_doc: number }; }; type DocSnapshotMap = { @@ -45,10 +44,10 @@ type DocSnapshotMap = { "": EmptySnapshot; }; export type DocSnapshot = - | EmptySnapshot - | CompSnapshot | PageSnapshot - | SiteSnapshot; + | SiteSnapshot + | CompSnapshot + | EmptySnapshot; const emptySnapshot: DocSnapshot = { type: "", @@ -69,12 +68,14 @@ export const snapshot = { }); return this._db; }, + get db() { if (!this._db) { this._db = this.init(); } return this._db; }, + async getOrCreate(data: DocSnapshot) { const id = `${data.type}-${data.id}`; let res = this.db.get(id); @@ -84,19 +85,22 @@ export const snapshot = { } return res as DocSnapshot; }, + get(type: K, id: string) { return this.db.get(`${type}-${id}`) as DocSnapshotMap[K] | null; }, + async update(data: DocSnapshot) { const id = `${data.type}-${data.id}`; await this.db.put(id, data); return true; }, - async set( - type: keyof DocSnapshotMap, + + async set( + type: K, id: string, key: T, - value: DocSnapshot[T] + value: DocSnapshotMap[K][T] ) { const item = this.get(type, id); if (item) {