fix code watchers

This commit is contained in:
Rizky 2023-11-09 16:20:41 +07:00
parent 3e0cd161f0
commit f620760929
3 changed files with 26 additions and 10 deletions

View File

@ -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);
}
}
}
};

View File

@ -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) => {

View File

@ -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];
}
};