fix site async

This commit is contained in:
Rizky 2023-11-07 03:52:20 +07:00
parent 29465f8729
commit 547b1c513f
4 changed files with 19 additions and 13 deletions

View File

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

View File

@ -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,

View File

@ -0,0 +1 @@
export const prepCode = async (site_id: string, name: string) => {};

View File

@ -31,15 +31,15 @@ export const RoomList = class<T extends Record<string, string>> {
});
}
set(
async set(
id: string,
ws: ServerWebSocket<WSData>,
fn: (data: Partial<T>) => Partial<T>
fn: (data: Partial<T>) => Promise<Partial<T>>
) {
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<T extends Record<string, any>> {
return clients;
}
set(
async set(
client: { ws?: ServerWebSocket<WSData>; id?: string },
action: (ws: ServerWebSocket<WSData>, data: Partial<T>) => Partial<T>
action: (
ws: ServerWebSocket<WSData>,
data: Partial<T>
) => Promise<Partial<T>>
) {
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);
}
}