This commit is contained in:
Rizky 2023-11-04 09:34:59 +07:00
parent 64fd838df2
commit 79a70e6c8d
5 changed files with 45 additions and 23 deletions

View File

@ -11,9 +11,9 @@ export const activity: SAction["activity"] = async function (
if (act.type === "code") {
a.site.room(act.id).set(me, (ws, data) => {
if (act.action === "open") {
data.code = { name: act.name };
data.site_js = act.name;
} else {
delete data.code;
delete data.site_js;
}
return data;
});

View File

@ -1,4 +1,5 @@
import { SAction } from "../actions";
import { activity } from "../entity/activity";
import { conns } from "../entity/conn";
import { docs } from "../entity/docs";
import { snapshot } from "../entity/snapshot";
@ -54,9 +55,19 @@ 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) => {
data.page_id = id_page;
return data;
});
};
if (!snap && !ydoc) {
const page = await db.page.findFirst({ where: { id } });
if (page) {
setActivityPage(page.id_site, page.id);
const doc = new Y.Doc();
let root = doc.getMap("map");
syncronize(root, { id, root: page.content_tree });
@ -100,6 +111,8 @@ 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);
Y.applyUpdate(doc, snap.bin);
let root = doc.getMap("map");
@ -127,6 +140,8 @@ export const page_load: SAction["page"]["load"] = async function (
snapshot: await gzipAsync(snap.bin),
};
} else if (snap && ydoc) {
setActivityPage(snap.id_site, id);
user.active.add({
...defaultActive,
client_id: this.client_id,

View File

@ -2,11 +2,9 @@ import { RoomList } from "./room";
export const activity = {
site: new RoomList<{
code: {
name: string;
};
page: { id: string };
comp: { id: string };
item: { id: string; js: boolean; css: boolean; html: boolean };
site_js: string;
page_id: string;
item_id: string;
item_action: "select" | "js" | "css" | "html";
}>("site"),
};

View File

@ -4,7 +4,7 @@ import { conns, wconns } from "./conn";
import { sendWS } from "../sync-handler";
import { SyncType } from "../type";
export const RoomList = class<T extends Record<string, any>> {
export const RoomList = class<T extends Record<string, string>> {
rooms = new Map<string, Room<T>>();
name = "";
constructor(name: string) {
@ -30,6 +30,20 @@ export const RoomList = class<T extends Record<string, any>> {
});
});
}
set(
id: string,
ws: ServerWebSocket<WSData>,
fn: (data: Partial<T>) => Partial<T>
) {
const room = this.room(id);
if (room) {
room.set({ ws }, (ws, data) => {
return fn(data);
});
}
}
room(id: string) {
let room = this.rooms.get(id);
if (!room) {
@ -56,18 +70,11 @@ export class Room<T extends Record<string, any>> {
for (const [ws, data] of this.clients) {
let match = true;
for (const key in where) {
if (where[key] === true) {
if (!data[key]) {
match = false;
break;
}
} else {
if (data[key] !== where[key]) {
match = false;
break;
}
}
}
if (match) clients.set(ws, data);
}
return clients;
@ -122,6 +129,8 @@ export class Room<T extends Record<string, any>> {
this.clients.forEach((data, ws) => {
const client_id = wconns.get(ws);
if (client_id) {
console.log(event_name, client_id, data);
const client = conns.get(client_id);
if (client)
clients.push({
@ -135,7 +144,7 @@ export class Room<T extends Record<string, any>> {
}
});
this.clients.forEach((data, ws) => {
this.clients.forEach((_, ws) => {
sendWS(ws, {
type: SyncType.Event,
event: `${this.id}_${event_name}`,

View File

@ -1,6 +1,6 @@
import { compress, decompress } from "wasm-gzip";
import { deepClone } from "web-utils";
import { Y } from "../../../../../srv/ws/sync/entity/docs";
import * as Y from "yjs";
import { clientStartSync } from "../../../utils/sync/ws-client";
import { w } from "../../../utils/types/general";
import { Loading } from "../../../utils/ui/loading";