From e165919ae9ca32744ddbc0fa9e196f8bc96ed981 Mon Sep 17 00:00:00 2001 From: Rizky Date: Fri, 16 Aug 2024 11:31:28 +0700 Subject: [PATCH] fix --- app/srv/ws/sync/code/parts/watcher.ts | 112 ++++++++++++-------------- 1 file changed, 52 insertions(+), 60 deletions(-) diff --git a/app/srv/ws/sync/code/parts/watcher.ts b/app/srv/ws/sync/code/parts/watcher.ts index 3a9788d6..0bf5d269 100644 --- a/app/srv/ws/sync/code/parts/watcher.ts +++ b/app/srv/ws/sync/code/parts/watcher.ts @@ -12,76 +12,68 @@ export class Watcher { constructor(path: string, id_site: string) { this.init(path, id_site); - // watcher.subscribe(path, (err, events) => { - // console.log(events); - // }); - // watch( - // path, - // { - // recursive: true, - // persistent: true, - // }, - // async (event, filename) => { - // } - // ); } async init(path: string, id_site: string) { - const broadcastLoading = async () => { - const client_ids = user.active - .findAll({ site_id: id_site }) - .map((e) => e.client_id); + try { + const broadcastLoading = async () => { + const client_ids = user.active + .findAll({ site_id: id_site }) + .map((e) => e.client_id); - const now = Date.now(); + const now = Date.now(); - client_ids.forEach((client_id) => { - const ws = conns.get(client_id)?.ws; - if (ws) - sendWS(ws, { - type: SyncType.Event, - event: "code_changes", - data: { ts: now, mode: "frontend", status: "building" }, - }); - }); - }; + client_ids.forEach((client_id) => { + const ws = conns.get(client_id)?.ws; + if (ws) + sendWS(ws, { + type: SyncType.Event, + event: "code_changes", + data: { ts: now, mode: "frontend", status: "building" }, + }); + }); + }; - this.subcription = await watcher.subscribe(path, (err, events) => { - for (const e of events) { - const filename = e.path.substring(path.length + 1); - if (e.type === "create" || e.type === "update") { - const fe = code.internal.frontend[id_site]; - if ( - filename?.startsWith("node_modules") || - filename?.startsWith("typings") || - filename?.endsWith(".log") - ) - return; + this.subcription = await watcher.subscribe(path, (err, events) => { + for (const e of events) { + const filename = e.path.substring(path.length + 1); + if (e.type === "create" || e.type === "update") { + const fe = code.internal.frontend[id_site]; + if ( + filename?.startsWith("node_modules") || + filename?.startsWith("typings") || + filename?.endsWith(".log") + ) + return; - if ( - filename?.endsWith(".tsx") || - filename?.endsWith(".ts") || - filename?.endsWith(".css") || - filename?.endsWith(".html") - ) { - if (typeof fe !== "undefined" && !fe.rebuilding) { - fe.rebuilding = true; - clearTimeout(fe.timeout); - fe.timeout = setTimeout(async () => { - try { - broadcastLoading(); - await fe.ctx.rebuild(); - fe.rebuilding = false; - } catch (e: any) { - console.error(`Frontend failed rebuild (site: ${id_site})`); - console.error(e.message); - fe.rebuilding = false; - } - }, 500); + if ( + filename?.endsWith(".tsx") || + filename?.endsWith(".ts") || + filename?.endsWith(".css") || + filename?.endsWith(".html") + ) { + if (typeof fe !== "undefined" && !fe.rebuilding) { + fe.rebuilding = true; + clearTimeout(fe.timeout); + fe.timeout = setTimeout(async () => { + try { + broadcastLoading(); + await fe.ctx.rebuild(); + fe.rebuilding = false; + } catch (e: any) { + console.error(`Frontend failed rebuild (site: ${id_site})`); + console.error(e.message); + fe.rebuilding = false; + } + }, 500); + } } } } - } - }); + }); + } catch (e) { + console.error(e); + } } async close() {