diff --git a/app/srv/ws/sync/actions/activity.ts b/app/srv/ws/sync/actions/activity.ts index 07d913df..ca2d7274 100644 --- a/app/srv/ws/sync/actions/activity.ts +++ b/app/srv/ws/sync/actions/activity.ts @@ -2,7 +2,7 @@ import { SAction } from "../actions"; import { SyncConnection } from "../type"; import { activity as a } from "../entity/activity"; import { prepCode } from "../editor/code/prep-code"; -import { startCodeWatcher } from "../editor/code/dev"; +import { Code, startCodeWatcher, stopCodeWatcher } from "../editor/code/watcher"; export const activity: SAction["activity"] = async function ( this: SyncConnection, name, @@ -11,17 +11,26 @@ 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") { + const code = await prepCode(act.id, act.name); a.site.set(act.id, this.ws, async (data) => { if (act.action === "open") { data.site_js = act.name; - const code = await prepCode(act.id, act.name); - if (code) { - await startCodeWatcher(code); - } } else { delete data.site_js; - } + } return data; }); + + if (act.action === "open") { + await startCodeWatcher(code); + } else { + const userCoding = a.site + .room(act.id) + .findAll({ site_js: act.name }).size; + + if (userCoding === 0) { + stopCodeWatcher(code.id); + } + } } }; diff --git a/app/srv/ws/sync/editor/code/prep-code.ts b/app/srv/ws/sync/editor/code/prep-code.ts index 985253d0..4171acbe 100644 --- a/app/srv/ws/sync/editor/code/prep-code.ts +++ b/app/srv/ws/sync/editor/code/prep-code.ts @@ -36,7 +36,7 @@ export const hello_world = () => { }); code = await getCode(site_id); - return code; + return code as DBCode; }; const getCode = async (site_id: string) => { diff --git a/app/srv/ws/sync/editor/code/dev.ts b/app/srv/ws/sync/editor/code/watcher.ts similarity index 82% rename from app/srv/ws/sync/editor/code/dev.ts rename to app/srv/ws/sync/editor/code/watcher.ts index 651ede78..61bf3838 100644 --- a/app/srv/ws/sync/editor/code/dev.ts +++ b/app/srv/ws/sync/editor/code/watcher.ts @@ -14,9 +14,9 @@ export const Code = { }; export const startCodeWatcher = async (code: DBCode) => { - // if (Code.watchers[code.id]) { - // return; - // } + if (Code.watchers[code.id]) { + return; + } for (const c of code.code_file) { const path = Code.path(c.id_code, c.path); @@ -43,3 +43,10 @@ export const startCodeWatcher = async (code: DBCode) => { } ); }; + +export const stopCodeWatcher = async (id_code: string) => { + if (Code.watchers[id_code]) { + Code.watchers[id_code].close(); + delete Code.watchers[id_code]; + } +};