From 79a70e6c8df12b9b234281998b5a30ec8ca52ee9 Mon Sep 17 00:00:00 2001 From: Rizky Date: Sat, 4 Nov 2023 09:34:59 +0700 Subject: [PATCH] set page --- app/srv/ws/sync/actions/activity.ts | 4 +-- app/srv/ws/sync/actions/page_load.ts | 15 +++++++++++ app/srv/ws/sync/entity/activity.ts | 12 ++++----- app/srv/ws/sync/entity/room.ts | 35 ++++++++++++++++--------- app/web/src/render/ed/logic/ed-sync.tsx | 2 +- 5 files changed, 45 insertions(+), 23 deletions(-) diff --git a/app/srv/ws/sync/actions/activity.ts b/app/srv/ws/sync/actions/activity.ts index ae6b8795..e16efa3a 100644 --- a/app/srv/ws/sync/actions/activity.ts +++ b/app/srv/ws/sync/actions/activity.ts @@ -11,9 +11,9 @@ export const activity: SAction["activity"] = async function ( if (act.type === "code") { a.site.room(act.id).set(me, (ws, data) => { if (act.action === "open") { - data.code = { name: act.name }; + data.site_js = act.name; } else { - delete data.code; + delete data.site_js; } return data; }); diff --git a/app/srv/ws/sync/actions/page_load.ts b/app/srv/ws/sync/actions/page_load.ts index 7e2a27ec..f2704d3f 100644 --- a/app/srv/ws/sync/actions/page_load.ts +++ b/app/srv/ws/sync/actions/page_load.ts @@ -1,4 +1,5 @@ import { SAction } from "../actions"; +import { activity } from "../entity/activity"; import { conns } from "../entity/conn"; import { docs } from "../entity/docs"; import { snapshot } from "../entity/snapshot"; @@ -54,9 +55,19 @@ export const page_load: SAction["page"]["load"] = async function ( select: "" as "" | "comp" | "item" | "section" | "text", }; + const setActivityPage = (id_site: string, id_page: string) => { + activity.site.set(id_site, this.ws, (data) => { + data.page_id = id_page; + return data; + }); + }; + if (!snap && !ydoc) { const page = await db.page.findFirst({ where: { id } }); + if (page) { + setActivityPage(page.id_site, page.id); + const doc = new Y.Doc(); let root = doc.getMap("map"); syncronize(root, { id, root: page.content_tree }); @@ -100,6 +111,8 @@ export const page_load: SAction["page"]["load"] = async function ( } else if (snap && !ydoc) { const doc = new Y.Doc(); snapshot.set("page", id, "id_doc", doc.clientID); + setActivityPage(snap.id_site, id); + Y.applyUpdate(doc, snap.bin); let root = doc.getMap("map"); @@ -127,6 +140,8 @@ export const page_load: SAction["page"]["load"] = async function ( snapshot: await gzipAsync(snap.bin), }; } else if (snap && ydoc) { + setActivityPage(snap.id_site, id); + user.active.add({ ...defaultActive, client_id: this.client_id, diff --git a/app/srv/ws/sync/entity/activity.ts b/app/srv/ws/sync/entity/activity.ts index 26f38f36..56e1d8a6 100644 --- a/app/srv/ws/sync/entity/activity.ts +++ b/app/srv/ws/sync/entity/activity.ts @@ -2,11 +2,9 @@ import { RoomList } from "./room"; export const activity = { site: new RoomList<{ - code: { - name: string; - }; - page: { id: string }; - comp: { id: string }; - item: { id: string; js: boolean; css: boolean; html: boolean }; + site_js: string; + page_id: string; + item_id: string; + item_action: "select" | "js" | "css" | "html"; }>("site"), -}; +}; \ No newline at end of file diff --git a/app/srv/ws/sync/entity/room.ts b/app/srv/ws/sync/entity/room.ts index 2b729225..5d63ea5b 100644 --- a/app/srv/ws/sync/entity/room.ts +++ b/app/srv/ws/sync/entity/room.ts @@ -4,7 +4,7 @@ import { conns, wconns } from "./conn"; import { sendWS } from "../sync-handler"; import { SyncType } from "../type"; -export const RoomList = class> { +export const RoomList = class> { rooms = new Map>(); name = ""; constructor(name: string) { @@ -20,7 +20,7 @@ export const RoomList = class> { }); }); return rooms; - } + } disconnect(ws: ServerWebSocket) { this.rooms.forEach((room) => { room.clients.forEach((_, roomws) => { @@ -30,6 +30,20 @@ export const RoomList = class> { }); }); } + + set( + id: string, + ws: ServerWebSocket, + fn: (data: Partial) => Partial + ) { + const room = this.room(id); + if (room) { + room.set({ ws }, (ws, data) => { + return fn(data); + }); + } + } + room(id: string) { let room = this.rooms.get(id); if (!room) { @@ -56,16 +70,9 @@ export class Room> { for (const [ws, data] of this.clients) { let match = true; for (const key in where) { - if (where[key] === true) { - if (!data[key]) { - match = false; - break; - } - } else { - if (data[key] !== where[key]) { - match = false; - break; - } + if (data[key] !== where[key]) { + match = false; + break; } } if (match) clients.set(ws, data); @@ -122,6 +129,8 @@ export class Room> { this.clients.forEach((data, ws) => { const client_id = wconns.get(ws); if (client_id) { + console.log(event_name, client_id, data); + const client = conns.get(client_id); if (client) clients.push({ @@ -135,7 +144,7 @@ export class Room> { } }); - this.clients.forEach((data, ws) => { + this.clients.forEach((_, ws) => { sendWS(ws, { type: SyncType.Event, event: `${this.id}_${event_name}`, diff --git a/app/web/src/render/ed/logic/ed-sync.tsx b/app/web/src/render/ed/logic/ed-sync.tsx index 5bd62b48..9186bb09 100644 --- a/app/web/src/render/ed/logic/ed-sync.tsx +++ b/app/web/src/render/ed/logic/ed-sync.tsx @@ -1,6 +1,6 @@ import { compress, decompress } from "wasm-gzip"; import { deepClone } from "web-utils"; -import { Y } from "../../../../../srv/ws/sync/entity/docs"; +import * as Y from "yjs"; import { clientStartSync } from "../../../utils/sync/ws-client"; import { w } from "../../../utils/types/general"; import { Loading } from "../../../utils/ui/loading";