From 763c982946e519bd94d9a52a9a3cb8dddf12e5f1 Mon Sep 17 00:00:00 2001 From: Rizky Date: Sat, 4 Nov 2023 03:09:16 +0700 Subject: [PATCH] fix y-pojo --- app/srv/api/comp-create.ts | 1 - app/srv/api/page-reload.ts | 1 - app/srv/global.ts | 1 + app/srv/ws/edit/action/get-comp.ts | 1 - app/srv/ws/edit/action/get-page.ts | 3 +- app/srv/ws/edit/edit-handler.ts | 93 +++++++++++++++++++++++ app/srv/ws/handler.ts | 93 +---------------------- app/srv/ws/sync/actions/comp_load.ts | 3 +- app/srv/ws/sync/actions/comp_new.ts | 10 +-- app/srv/ws/sync/actions/page_load.ts | 5 +- app/srv/ws/sync/actions/yjs_diff_local.ts | 2 +- app/srv/ws/sync/actions/yjs_sv_local.ts | 2 +- app/srv/ws/sync/actions/yjs_sv_remote.ts | 2 +- app/srv/y.d.ts | 4 + pkgs/core/index.ts | 6 +- pkgs/core/server/create.ts | 2 +- pkgs/core/utils/global.ts | 6 +- 17 files changed, 120 insertions(+), 115 deletions(-) create mode 100644 app/srv/ws/edit/edit-handler.ts diff --git a/app/srv/api/comp-create.ts b/app/srv/api/comp-create.ts index e8a6c64a..b29c097f 100644 --- a/app/srv/api/comp-create.ts +++ b/app/srv/api/comp-create.ts @@ -1,4 +1,3 @@ -import { syncronize } from "y-pojo"; import { IItem, MItem } from "../../web/src/utils/types/item"; import { eg } from "../ws/edit/edit-global"; diff --git a/app/srv/api/page-reload.ts b/app/srv/api/page-reload.ts index 3e01ca95..95db9c04 100644 --- a/app/srv/api/page-reload.ts +++ b/app/srv/api/page-reload.ts @@ -1,4 +1,3 @@ -import { syncronize } from "y-pojo"; import { MPage } from "../../web/src/utils/types/general"; import { WS_MSG_SET_PAGE, WS_MSG_SV_LOCAL } from "../../web/src/utils/types/ws"; import { eg } from "../ws/edit/edit-global"; diff --git a/app/srv/global.ts b/app/srv/global.ts index dd81cd45..4eb4e56e 100644 --- a/app/srv/global.ts +++ b/app/srv/global.ts @@ -3,6 +3,7 @@ import { ExecaChildProcess } from "execa"; declare global { const Y: typeof Y; + const syncronize: typeof Y.syncronize; } export const glb = global as unknown as { diff --git a/app/srv/ws/edit/action/get-comp.ts b/app/srv/ws/edit/action/get-comp.ts index 33fbc37c..d83103ed 100644 --- a/app/srv/ws/edit/action/get-comp.ts +++ b/app/srv/ws/edit/action/get-comp.ts @@ -1,6 +1,5 @@ import { ServerWebSocket } from "bun"; import { validate } from "uuid"; -import { syncronize } from "y-pojo"; import { WSData } from "../../../../../pkgs/core/server/create"; import { WS_MSG_GET_COMP, diff --git a/app/srv/ws/edit/action/get-page.ts b/app/srv/ws/edit/action/get-page.ts index e110fde9..de6b37eb 100644 --- a/app/srv/ws/edit/action/get-page.ts +++ b/app/srv/ws/edit/action/get-page.ts @@ -1,6 +1,5 @@ import { ServerWebSocket } from "bun"; import { validate } from "uuid"; -import { syncronize } from "y-pojo"; import { WSData } from "../../../../../pkgs/core/server/create"; import { MPage } from "../../../../web/src/utils/types/general"; import { @@ -56,5 +55,5 @@ export const getPage = async ( type: "set_page", changes: Y.encodeStateAsUpdate(page.doc as any).toString(), }; - sendWS(ws,JSON.stringify(sent)); + sendWS(ws, JSON.stringify(sent)); }; diff --git a/app/srv/ws/edit/edit-handler.ts b/app/srv/ws/edit/edit-handler.ts new file mode 100644 index 00000000..a2968fdd --- /dev/null +++ b/app/srv/ws/edit/edit-handler.ts @@ -0,0 +1,93 @@ +import { createId } from "@paralleldrive/cuid2"; +import { WebSocketHandler } from "bun"; +import { WSData } from "../../../../pkgs/core/server/create"; +import { WS_MSG } from "../../../web/src/utils/types/ws"; +import { diffLocal } from "./action/diff-local"; +import { getComp } from "./action/get-comp"; +import { getPage } from "./action/get-page"; +import { svLocal } from "./action/sv-local"; +import { svdiffRemote } from "./action/svdiff-remote"; +import { redo, undo } from "./action/undo-redo"; +import { eg } from "./edit-global"; +import { sendWS } from "./send"; + +const site = { + saveTimeout: null as any, +}; + +export const editHandler: WebSocketHandler = { + open(ws) { + eg.edit.ws.set(ws, { + clientID: createId(), + }); + }, + async message(ws, raw) { + if (typeof raw === "string") { + try { + const msg = JSON.parse(raw) as WS_MSG; + + if (msg.type === "ping") { + sendWS(ws, JSON.stringify({ type: "pong" })); + return; + } + + switch (msg.type) { + case "site-js": + clearTimeout(site.saveTimeout); + site.saveTimeout = setTimeout(async () => { + const js = JSON.parse(msg.src); + await db.site.update({ + where: { + id: msg.id_site, + }, + data: { + js: js.src, + js_compiled: js.compiled, + }, + }); + }, 1000); + break; + case "get_page": + await getPage(ws, msg); + break; + case "get_comp": + await getComp(ws, msg); + break; + case "sv_local": + await svLocal(ws, msg); + break; + case "diff_local": + await diffLocal(ws, msg); + break; + case "svd_remote": + await svdiffRemote(ws, msg); + break; + case "undo": + undo(ws, msg); + break; + case "redo": + redo(ws, msg); + break; + } + } catch (e) { + console.log(e); + } + } + }, + close(ws, code, reason) { + eg.edit.ws.delete(ws); + + for (const page of Object.values(eg.edit.page)) { + page.ws.delete(ws); + } + + for (const site of Object.values(eg.edit.site)) { + site.ws.delete(ws); + } + + for (const comp of Object.values(eg.edit.comp)) { + comp.ws.delete(ws); + } + }, + drain(ws) {}, +}; diff --git a/app/srv/ws/handler.ts b/app/srv/ws/handler.ts index 2fc8b6c8..29d07499 100644 --- a/app/srv/ws/handler.ts +++ b/app/srv/ws/handler.ts @@ -1,104 +1,17 @@ -import { createId } from "@paralleldrive/cuid2"; import { WebSocketHandler } from "bun"; import { WSData } from "../../../pkgs/core/server/create"; -import { WS_MSG } from "../../web/src/utils/types/ws"; -import { diffLocal } from "./edit/action/diff-local"; -import { getComp } from "./edit/action/get-comp"; -import { getPage } from "./edit/action/get-page"; -import { svLocal } from "./edit/action/sv-local"; -import { svdiffRemote } from "./edit/action/svdiff-remote"; -import { redo, undo } from "./edit/action/undo-redo"; import { eg } from "./edit/edit-global"; -import { sendWS } from "./edit/send"; import { syncHandler } from "./sync/sync-handler"; +import { editHandler } from "./edit/edit-handler"; -(globalThis as any).Y = Y; eg.edit = { site: {}, comp: {}, page: {}, ws: new WeakMap(), }; -const site = { - saveTimeout: null as any, -}; - export const wsHandler: Record> = { "/sync": syncHandler, - "/edit": { - open(ws) { - eg.edit.ws.set(ws, { - clientID: createId(), - }); - }, - async message(ws, raw) { - if (typeof raw === "string") { - try { - const msg = JSON.parse(raw) as WS_MSG; - - if (msg.type === "ping") { - sendWS(ws, JSON.stringify({ type: "pong" })); - return; - } - - switch (msg.type) { - case "site-js": - clearTimeout(site.saveTimeout); - site.saveTimeout = setTimeout(async () => { - const js = JSON.parse(msg.src); - await db.site.update({ - where: { - id: msg.id_site, - }, - data: { - js: js.src, - js_compiled: js.compiled, - }, - }); - }, 1000); - break; - case "get_page": - await getPage(ws, msg); - break; - case "get_comp": - await getComp(ws, msg); - break; - case "sv_local": - await svLocal(ws, msg); - break; - case "diff_local": - await diffLocal(ws, msg); - break; - case "svd_remote": - await svdiffRemote(ws, msg); - break; - case "undo": - undo(ws, msg); - break; - case "redo": - redo(ws, msg); - break; - } - } catch (e) { - console.log(e); - } - } - }, - close(ws, code, reason) { - eg.edit.ws.delete(ws); - - for (const page of Object.values(eg.edit.page)) { - page.ws.delete(ws); - } - - for (const site of Object.values(eg.edit.site)) { - site.ws.delete(ws); - } - - for (const comp of Object.values(eg.edit.comp)) { - comp.ws.delete(ws); - } - }, - drain(ws) {}, - }, + "/edit": editHandler, }; + \ No newline at end of file diff --git a/app/srv/ws/sync/actions/comp_load.ts b/app/srv/ws/sync/actions/comp_load.ts index f9ed8017..ee7e8200 100644 --- a/app/srv/ws/sync/actions/comp_load.ts +++ b/app/srv/ws/sync/actions/comp_load.ts @@ -1,7 +1,6 @@ -import { syncronize } from "y-pojo"; import { SAction } from "../actions"; import { conns } from "../entity/conn"; -import { Y, docs } from "../entity/docs"; +import { docs } from "../entity/docs"; import { snapshot } from "../entity/snapshot"; import { user } from "../entity/user"; import { gzipAsync } from "../entity/zlib"; diff --git a/app/srv/ws/sync/actions/comp_new.ts b/app/srv/ws/sync/actions/comp_new.ts index 8ead049a..89408ad1 100644 --- a/app/srv/ws/sync/actions/comp_new.ts +++ b/app/srv/ws/sync/actions/comp_new.ts @@ -1,15 +1,13 @@ import { TypedArray } from "yjs-types"; -import { MContent } from "../../../../web/src/utils/types/general"; -import { IItem, MItem } from "../../../../web/src/utils/types/item"; -import { SAction } from "../actions"; -import { docs } from "../entity/docs"; -import { SyncConnection } from "../type"; -import { syncronize } from "y-pojo"; +import { MItem } from "../../../../web/src/utils/types/item"; import { FMComponent, FNComponent, } from "../../../../web/src/utils/types/meta-fn"; import { MText } from "../../../../web/src/utils/types/text"; +import { SAction } from "../actions"; +import { docs } from "../entity/docs"; +import { SyncConnection } from "../type"; export const comp_new: SAction["comp"]["new"] = async function ( this: SyncConnection, diff --git a/app/srv/ws/sync/actions/page_load.ts b/app/srv/ws/sync/actions/page_load.ts index 7e033b72..7e2a27ec 100644 --- a/app/srv/ws/sync/actions/page_load.ts +++ b/app/srv/ws/sync/actions/page_load.ts @@ -1,7 +1,6 @@ -import { syncronize } from "y-pojo"; import { SAction } from "../actions"; import { conns } from "../entity/conn"; -import { Y, docs } from "../entity/docs"; +import { docs } from "../entity/docs"; import { snapshot } from "../entity/snapshot"; import { user } from "../entity/user"; import { gzipAsync } from "../entity/zlib"; @@ -13,7 +12,7 @@ export const page_load: SAction["page"]["load"] = async function ( id: string ) { let snap = snapshot.get("page", id); - let ydoc = docs.page[id]; + let ydoc = docs.page[id]; const conf = this.conf; if (!conf) return undefined; diff --git a/app/srv/ws/sync/actions/yjs_diff_local.ts b/app/srv/ws/sync/actions/yjs_diff_local.ts index 69895987..8c556f72 100644 --- a/app/srv/ws/sync/actions/yjs_diff_local.ts +++ b/app/srv/ws/sync/actions/yjs_diff_local.ts @@ -1,5 +1,5 @@ import { SAction } from "../actions"; -import { Y, docs } from "../entity/docs"; +import { docs } from "../entity/docs"; import { gunzipAsync } from "../entity/zlib"; import { SyncConnection } from "../type"; diff --git a/app/srv/ws/sync/actions/yjs_sv_local.ts b/app/srv/ws/sync/actions/yjs_sv_local.ts index 0d952358..16471284 100644 --- a/app/srv/ws/sync/actions/yjs_sv_local.ts +++ b/app/srv/ws/sync/actions/yjs_sv_local.ts @@ -1,5 +1,5 @@ import { SAction } from "../actions"; -import { Y, docs } from "../entity/docs"; +import { docs } from "../entity/docs"; import { gunzipAsync, gzipAsync } from "../entity/zlib"; import { SyncConnection } from "../type"; diff --git a/app/srv/ws/sync/actions/yjs_sv_remote.ts b/app/srv/ws/sync/actions/yjs_sv_remote.ts index 5246d37a..2490f3b3 100644 --- a/app/srv/ws/sync/actions/yjs_sv_remote.ts +++ b/app/srv/ws/sync/actions/yjs_sv_remote.ts @@ -1,5 +1,5 @@ import { SAction } from "../actions"; -import { Y, docs } from "../entity/docs"; +import { docs } from "../entity/docs"; import { gunzipAsync, gzipAsync } from "../entity/zlib"; import { SyncConnection } from "../type"; diff --git a/app/srv/y.d.ts b/app/srv/y.d.ts index 543dfa21..82307d97 100644 --- a/app/srv/y.d.ts +++ b/app/srv/y.d.ts @@ -1,4 +1,6 @@ import type * as Y from "yjs"; +import { syncronize } from "y-pojo"; + export import Doc = Y.Doc; export import UndoManager = Y.UndoManager; export import applyUpdate = Y.applyUpdate; @@ -7,4 +9,6 @@ export import encodeStateAsUpdate = Y.encodeStateAsUpdate; export import Text = Y.Text; export import Map = Y.Map; export import Array = Y.Array; +export import syncronize = syncronize; + export as namespace Y; diff --git a/pkgs/core/index.ts b/pkgs/core/index.ts index a860e097..82c724ac 100644 --- a/pkgs/core/index.ts +++ b/pkgs/core/index.ts @@ -1,7 +1,6 @@ import { parcelBuild } from "utils/parcel"; import { generateAPIFrm } from "./server/api-frm"; import { prepareApiRoutes } from "./server/api-scan"; -import { createServer } from "./server/create"; import { prepareAPITypes } from "./server/prep-api-ts"; import { startDevWatcher } from "./utils/dev-watcher"; import { ensureNotRunning } from "./utils/ensure"; @@ -17,6 +16,7 @@ g.status = "init"; if (!g.Y) { g.Y = await import("yjs"); + g.syncronize = (await import("y-pojo")).syncronize; await createLogger(); g.api = {}; @@ -65,7 +65,7 @@ if (!g.apiPrepared) { if (!g.parcel) { await parcelBuild(); } - + +const { createServer } = await import("./server/create"); await createServer(); g.status = "ready"; - \ No newline at end of file diff --git a/pkgs/core/server/create.ts b/pkgs/core/server/create.ts index 02a69920..4cc24925 100644 --- a/pkgs/core/server/create.ts +++ b/pkgs/core/server/create.ts @@ -1,7 +1,6 @@ import { WebSocketHandler } from "bun"; import { lookup } from "mime-types"; import { createRouter } from "radix3"; -import { wsHandler } from "../../../app/srv/ws/handler"; import { dir } from "../utils/dir"; import { g } from "../utils/global"; import { serveAPI } from "./serve-api"; @@ -26,6 +25,7 @@ export const createServer = async () => { g.router.insert(route.url.replace(/\*/gi, "**"), route); } + const { wsHandler } = await import("../../../app/srv/ws/handler"); g.server = Bun.serve({ port: g.port, websocket: { diff --git a/pkgs/core/utils/global.ts b/pkgs/core/utils/global.ts index e2e41fc8..c8b42116 100644 --- a/pkgs/core/utils/global.ts +++ b/pkgs/core/utils/global.ts @@ -1,10 +1,11 @@ import { Server, Subprocess } from "bun"; import { Logger } from "pino"; import { RadixRouter } from "radix3"; -import { PrismaClient } from "../../../app/db/db"; +import { syncronize } from "y-pojo"; import type * as Y from "yjs"; +import { PrismaClient } from "../../../app/db/db"; -type SingleRoute = { +type SingleRoute = { url: string; args: string[]; fn: (...arg: any[]) => Promise; @@ -30,4 +31,5 @@ export const g = global as unknown as { parcel: Subprocess; apiPrepared: boolean; Y: typeof Y; + syncronize: typeof syncronize; };