From 83317ec5a01b4a81572f0ee252d4982feb5a8831 Mon Sep 17 00:00:00 2001 From: Rizky Date: Tue, 23 Jan 2024 11:58:24 +0700 Subject: [PATCH] wip fix --- app/srv/ws/sync/editor/code/build.ts | 45 ++++++++++++++++++++++++++++ app/srv/ws/sync/entity/room.ts | 12 ++++---- 2 files changed, 52 insertions(+), 5 deletions(-) diff --git a/app/srv/ws/sync/editor/code/build.ts b/app/srv/ws/sync/editor/code/build.ts index a41d9b93..c7f06dc5 100644 --- a/app/srv/ws/sync/editor/code/build.ts +++ b/app/srv/ws/sync/editor/code/build.ts @@ -109,3 +109,48 @@ export const codeBuild = async (code: DBCode) => { }); } }; + +const code_id = {} as Record; + +export const broadcastCode = async (id_site: string) => { + if (!code_id[id_site]) { + const res = await db.code.findMany({ where: { id_site } }); + if (res.length > 0) { + code_id[id_site] = { site: "", ssr: "" }; + for (const c of res) { + if (c.name === "site") { + code_id[c.id_site].site = c.id; + } else { + code_id[c.id_site].ssr = c.id; + } + } + } + } + + if (code_id[id_site] && code_id[id_site].site) { + const id_code = code_id[id_site].site; + const outfile = dir.path(`${g.datadir}/site/build/${id_code}/index.js`); + const out = Bun.file(outfile); + const src = (await out.text()).replace( + "//# sourceMappingURL=index.js.map", + `//# sourceMappingURL=/nova-load/code/${id_code}/index.js.map` + ); + const srcgz = await gzipAsync(encoder.encode(src)); + activity.site + .room(id_site) + .findAll() + .forEach((item, ws) => { + sendWS(ws, { + type: SyncType.Event, + event: "code", + data: { + name: "site", + id: id_code, + event: "code-done", + src: srcgz, + content: "OK", + }, + }); + }); + } +}; diff --git a/app/srv/ws/sync/entity/room.ts b/app/srv/ws/sync/entity/room.ts index 8debc2d3..1af71d96 100644 --- a/app/srv/ws/sync/entity/room.ts +++ b/app/srv/ws/sync/entity/room.ts @@ -70,14 +70,16 @@ export class Room> { this.id = id; } - findAll(where: Partial) { + findAll(where?: Partial) { const clients = new Map, Partial>(); for (const [ws, data] of this.clients) { let match = true; - for (const key in where) { - if (data[key] !== where[key]) { - match = false; - break; + if (where) { + for (const key in where) { + if (data[key] !== where[key]) { + match = false; + break; + } } } if (match) clients.set(ws, data);