fix site async
This commit is contained in:
parent
29465f8729
commit
547b1c513f
|
|
@ -1,6 +1,7 @@
|
||||||
import { SAction } from "../actions";
|
import { SAction } from "../actions";
|
||||||
import { SyncConnection } from "../type";
|
import { SyncConnection } from "../type";
|
||||||
import { activity as a } from "../entity/activity";
|
import { activity as a } from "../entity/activity";
|
||||||
|
import { prepCode } from "../editor/prep-code";
|
||||||
export const activity: SAction["activity"] = async function (
|
export const activity: SAction["activity"] = async function (
|
||||||
this: SyncConnection,
|
this: SyncConnection,
|
||||||
name,
|
name,
|
||||||
|
|
@ -9,9 +10,10 @@ export const activity: SAction["activity"] = async function (
|
||||||
const me = { ws: this.ws };
|
const me = { ws: this.ws };
|
||||||
if (act.type === "join") a.site.room(act.id).join(me);
|
if (act.type === "join") a.site.room(act.id).join(me);
|
||||||
if (act.type === "code") {
|
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") {
|
if (act.action === "open") {
|
||||||
data.site_js = act.name;
|
data.site_js = act.name;
|
||||||
|
await prepCode(act.id, act.name);
|
||||||
} else {
|
} else {
|
||||||
delete data.site_js;
|
delete data.site_js;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -55,8 +55,8 @@ export const page_load: SAction["page"]["load"] = async function (
|
||||||
select: "" as "" | "comp" | "item" | "section" | "text",
|
select: "" as "" | "comp" | "item" | "section" | "text",
|
||||||
};
|
};
|
||||||
|
|
||||||
const setActivityPage = (id_site: string, id_page: string) => {
|
const setActivityPage = async (id_site: string, id_page: string) => {
|
||||||
activity.site.set(id_site, this.ws, (data) => {
|
await activity.site.set(id_site, this.ws, async (data) => {
|
||||||
data.page_id = id_page;
|
data.page_id = id_page;
|
||||||
return data;
|
return data;
|
||||||
});
|
});
|
||||||
|
|
@ -66,7 +66,7 @@ export const page_load: SAction["page"]["load"] = async function (
|
||||||
const page = await db.page.findFirst({ where: { id } });
|
const page = await db.page.findFirst({ where: { id } });
|
||||||
|
|
||||||
if (page) {
|
if (page) {
|
||||||
setActivityPage(page.id_site, page.id);
|
await setActivityPage(page.id_site, page.id);
|
||||||
|
|
||||||
const doc = new Y.Doc();
|
const doc = new Y.Doc();
|
||||||
let root = doc.getMap("map");
|
let root = doc.getMap("map");
|
||||||
|
|
@ -111,7 +111,7 @@ export const page_load: SAction["page"]["load"] = async function (
|
||||||
} else if (snap && !ydoc) {
|
} else if (snap && !ydoc) {
|
||||||
const doc = new Y.Doc();
|
const doc = new Y.Doc();
|
||||||
snapshot.set("page", id, "id_doc", doc.clientID);
|
snapshot.set("page", id, "id_doc", doc.clientID);
|
||||||
setActivityPage(snap.id_site, id);
|
await setActivityPage(snap.id_site, id);
|
||||||
|
|
||||||
Y.applyUpdate(doc, snap.bin);
|
Y.applyUpdate(doc, snap.bin);
|
||||||
let root = doc.getMap("map");
|
let root = doc.getMap("map");
|
||||||
|
|
@ -140,7 +140,7 @@ export const page_load: SAction["page"]["load"] = async function (
|
||||||
snapshot: await gzipAsync(snap.bin),
|
snapshot: await gzipAsync(snap.bin),
|
||||||
};
|
};
|
||||||
} else if (snap && ydoc) {
|
} else if (snap && ydoc) {
|
||||||
setActivityPage(snap.id_site, id);
|
await setActivityPage(snap.id_site, id);
|
||||||
|
|
||||||
user.active.add({
|
user.active.add({
|
||||||
...defaultActive,
|
...defaultActive,
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1 @@
|
||||||
|
export const prepCode = async (site_id: string, name: string) => {};
|
||||||
|
|
@ -31,15 +31,15 @@ export const RoomList = class<T extends Record<string, string>> {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
set(
|
async set(
|
||||||
id: string,
|
id: string,
|
||||||
ws: ServerWebSocket<WSData>,
|
ws: ServerWebSocket<WSData>,
|
||||||
fn: (data: Partial<T>) => Partial<T>
|
fn: (data: Partial<T>) => Promise<Partial<T>>
|
||||||
) {
|
) {
|
||||||
const room = this.room(id);
|
const room = this.room(id);
|
||||||
if (room) {
|
if (room) {
|
||||||
room.set({ ws }, (ws, data) => {
|
await room.set({ ws }, async (ws, data) => {
|
||||||
return fn(data);
|
return await fn(data);
|
||||||
});
|
});
|
||||||
|
|
||||||
room.broadcastState("set", ws);
|
room.broadcastState("set", ws);
|
||||||
|
|
@ -85,15 +85,18 @@ export class Room<T extends Record<string, any>> {
|
||||||
return clients;
|
return clients;
|
||||||
}
|
}
|
||||||
|
|
||||||
set(
|
async set(
|
||||||
client: { ws?: ServerWebSocket<WSData>; id?: string },
|
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);
|
const ws = this.identify(client);
|
||||||
if (ws) {
|
if (ws) {
|
||||||
const data = this.clients.get(ws);
|
const data = this.clients.get(ws);
|
||||||
if (data) {
|
if (data) {
|
||||||
const newdata = action(ws, data);
|
const newdata = await action(ws, data);
|
||||||
this.clients.set(ws, newdata);
|
this.clients.set(ws, newdata);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue