From 547b1c513fdb53f305dde22f22625ac2fb7c42e9 Mon Sep 17 00:00:00 2001 From: Rizky Date: Tue, 7 Nov 2023 03:52:20 +0700 Subject: [PATCH] fix site async --- app/srv/ws/sync/actions/activity.ts | 4 +++- app/srv/ws/sync/actions/page_load.ts | 10 +++++----- app/srv/ws/sync/editor/prep-code.ts | 1 + app/srv/ws/sync/entity/room.ts | 17 ++++++++++------- 4 files changed, 19 insertions(+), 13 deletions(-) create mode 100644 app/srv/ws/sync/editor/prep-code.ts diff --git a/app/srv/ws/sync/actions/activity.ts b/app/srv/ws/sync/actions/activity.ts index e4e28c7d..6e64d1a5 100644 --- a/app/srv/ws/sync/actions/activity.ts +++ b/app/srv/ws/sync/actions/activity.ts @@ -1,6 +1,7 @@ import { SAction } from "../actions"; import { SyncConnection } from "../type"; import { activity as a } from "../entity/activity"; +import { prepCode } from "../editor/prep-code"; export const activity: SAction["activity"] = async function ( this: SyncConnection, name, @@ -9,9 +10,10 @@ export const activity: SAction["activity"] = async function ( const me = { ws: this.ws }; if (act.type === "join") a.site.room(act.id).join(me); if (act.type === "code") { - a.site.set(act.id, this.ws, (data) => { + a.site.set(act.id, this.ws, async (data) => { if (act.action === "open") { data.site_js = act.name; + await prepCode(act.id, act.name); } else { delete data.site_js; } diff --git a/app/srv/ws/sync/actions/page_load.ts b/app/srv/ws/sync/actions/page_load.ts index f2704d3f..cb2c1db2 100644 --- a/app/srv/ws/sync/actions/page_load.ts +++ b/app/srv/ws/sync/actions/page_load.ts @@ -55,8 +55,8 @@ 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) => { + const setActivityPage = async (id_site: string, id_page: string) => { + await activity.site.set(id_site, this.ws, async (data) => { data.page_id = id_page; return data; }); @@ -66,7 +66,7 @@ export const page_load: SAction["page"]["load"] = async function ( const page = await db.page.findFirst({ where: { id } }); if (page) { - setActivityPage(page.id_site, page.id); + await setActivityPage(page.id_site, page.id); const doc = new Y.Doc(); let root = doc.getMap("map"); @@ -111,7 +111,7 @@ 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); + await setActivityPage(snap.id_site, id); Y.applyUpdate(doc, snap.bin); let root = doc.getMap("map"); @@ -140,7 +140,7 @@ export const page_load: SAction["page"]["load"] = async function ( snapshot: await gzipAsync(snap.bin), }; } else if (snap && ydoc) { - setActivityPage(snap.id_site, id); + await setActivityPage(snap.id_site, id); user.active.add({ ...defaultActive, diff --git a/app/srv/ws/sync/editor/prep-code.ts b/app/srv/ws/sync/editor/prep-code.ts new file mode 100644 index 00000000..15330027 --- /dev/null +++ b/app/srv/ws/sync/editor/prep-code.ts @@ -0,0 +1 @@ +export const prepCode = async (site_id: string, name: string) => {}; diff --git a/app/srv/ws/sync/entity/room.ts b/app/srv/ws/sync/entity/room.ts index a2ecce64..8debc2d3 100644 --- a/app/srv/ws/sync/entity/room.ts +++ b/app/srv/ws/sync/entity/room.ts @@ -31,15 +31,15 @@ export const RoomList = class> { }); } - set( + async set( id: string, ws: ServerWebSocket, - fn: (data: Partial) => Partial + fn: (data: Partial) => Promise> ) { const room = this.room(id); if (room) { - room.set({ ws }, (ws, data) => { - return fn(data); + await room.set({ ws }, async (ws, data) => { + return await fn(data); }); room.broadcastState("set", ws); @@ -85,15 +85,18 @@ export class Room> { return clients; } - set( + async set( client: { ws?: ServerWebSocket; id?: string }, - action: (ws: ServerWebSocket, data: Partial) => Partial + action: ( + ws: ServerWebSocket, + data: Partial + ) => Promise> ) { const ws = this.identify(client); if (ws) { const data = this.clients.get(ws); if (data) { - const newdata = action(ws, data); + const newdata = await action(ws, data); this.clients.set(ws, newdata); } }