From 29465f8729c1e2234b582b843f2828a9bd0279e7 Mon Sep 17 00:00:00 2001 From: Rizky Date: Tue, 7 Nov 2023 02:29:52 +0700 Subject: [PATCH] fix room --- app/srv/ws/sync/actions/activity.ts | 2 +- app/srv/ws/sync/entity/room.ts | 7 +++++-- app/web/src/render/ed/logic/ed-global.ts | 8 ++------ app/web/src/render/ed/logic/ed-sync.tsx | 4 ++-- app/web/src/render/ed/panel/popup/code/code.tsx | 15 +++++++++------ app/web/src/render/ed/panel/script/code/code.tsx | 10 ++++++++++ app/web/y.d.ts | 14 ++++++++++++++ 7 files changed, 43 insertions(+), 17 deletions(-) create mode 100644 app/web/y.d.ts diff --git a/app/srv/ws/sync/actions/activity.ts b/app/srv/ws/sync/actions/activity.ts index e16efa3a..e4e28c7d 100644 --- a/app/srv/ws/sync/actions/activity.ts +++ b/app/srv/ws/sync/actions/activity.ts @@ -9,7 +9,7 @@ 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.room(act.id).set(me, (ws, data) => { + a.site.set(act.id, this.ws, (data) => { if (act.action === "open") { data.site_js = act.name; } else { diff --git a/app/srv/ws/sync/entity/room.ts b/app/srv/ws/sync/entity/room.ts index 0512bb7c..a2ecce64 100644 --- a/app/srv/ws/sync/entity/room.ts +++ b/app/srv/ws/sync/entity/room.ts @@ -47,6 +47,9 @@ export const RoomList = class> { } room(id: string) { + if (!id) { + throw new Error("room id is empty"); + } let room = this.rooms.get(id); if (!room) { this.rooms.set(id, new Room(this.name, id)); @@ -128,11 +131,11 @@ export class Room> { triggeredBy?: ServerWebSocket | null ) => { const clients: any = []; + this.clients.forEach((data, ws) => { const client_id = wconns.get(ws); - if (client_id) { - console.log(event_name, client_id, data); + if (client_id) { const client = conns.get(client_id); if (client) clients.push({ diff --git a/app/web/src/render/ed/logic/ed-global.ts b/app/web/src/render/ed/logic/ed-global.ts index 518fa2c9..f634ba7a 100644 --- a/app/web/src/render/ed/logic/ed-global.ts +++ b/app/web/src/render/ed/logic/ed-global.ts @@ -2,7 +2,6 @@ import { NodeModel } from "@minoru/react-dnd-treeview"; import { ReactElement } from "react"; import { deepClone } from "web-utils"; import { SAction } from "../../../../../srv/ws/sync/actions"; -import { ActivityList } from "../../../../../srv/ws/sync/type"; import { clientStartSync } from "../../../utils/sync/ws-client"; import { IItem, MItem } from "../../../utils/types/item"; import { DComp, DPage, IRoot } from "../../../utils/types/root"; @@ -73,10 +72,6 @@ export const EDGlobal = { | "ready", sync: null as unknown as Awaited>, site: deepClone(EmptySite), - activity: { - page: {} as Record, - comp: {} as Record, - }, script: { siteTypes: {} as Record }, page: { cur: EmptyPage, @@ -116,9 +111,10 @@ export const EDGlobal = { }, popup: { code: { + init: false, open: false, id: "", - file: "", + name: "", }, site: null as null | ((site_id: string) => void | Promise), site_form: null as null | { diff --git a/app/web/src/render/ed/logic/ed-sync.tsx b/app/web/src/render/ed/logic/ed-sync.tsx index 6f15a3e0..0fa27f67 100644 --- a/app/web/src/render/ed/logic/ed-sync.tsx +++ b/app/web/src/render/ed/logic/ed-sync.tsx @@ -25,6 +25,7 @@ export const edInitSync = (p: PG) => { if (params.site_id !== p.site.id) { p.site = deepClone(EmptySite); p.site.id = "--loading--"; + p.ui.popup.code.init = false; p.sync.site.load(params.site_id).then((site) => { if (site) { p.site = site; @@ -62,14 +63,13 @@ export const edInitSync = (p: PG) => { page_id: params.page_id, events: { activity(arg) { - console.log(arg); }, opened() { if (w.offline) { console.log("reconnected!"); w.offline = false; p.ui.syncing = true; - p.sync.activity("site", { type: "join", id: params.id }); + p.sync.activity("site", { type: "join", id: params.site_id }); p.render(); } else { w.offline = false; diff --git a/app/web/src/render/ed/panel/popup/code/code.tsx b/app/web/src/render/ed/panel/popup/code/code.tsx index 9e4b58b5..01efcf52 100644 --- a/app/web/src/render/ed/panel/popup/code/code.tsx +++ b/app/web/src/render/ed/panel/popup/code/code.tsx @@ -7,12 +7,15 @@ export const EdPopCode = () => { const p = useGlobal(EDGlobal, "EDITOR"); useEffect(() => { - p.sync.activity("site", { - action: p.ui.popup.code.open ? "open" : "close", - id: p.site.id, - type: "code", - name: "main", - }); + if (p.ui.popup.code.init) { + p.sync.activity("site", { + action: p.ui.popup.code.open ? "open" : "close", + id: p.site.id, + type: "code", + name: "main", + }); + } + p.ui.popup.code.init = true; }, [p.ui.popup.code.open]); return ( diff --git a/app/web/src/render/ed/panel/script/code/code.tsx b/app/web/src/render/ed/panel/script/code/code.tsx index d06bd52d..93214b18 100644 --- a/app/web/src/render/ed/panel/script/code/code.tsx +++ b/app/web/src/render/ed/panel/script/code/code.tsx @@ -1,10 +1,20 @@ import { useGlobal } from "web-utils"; import { EDGlobal } from "../../../logic/ed-global"; import { EdMonaco } from "../monaco/monaco"; +import { useEffect } from "react"; export const EdCode = () => { const p = useGlobal(EDGlobal, "EDITOR"); + useEffect(() => { + p.sync.activity("site", { + type: "code", + id: p.site.id, + action: p.ui.popup.code.open ? "open" : "close", + name: p.ui.popup.code.name, + }); + }, [p.ui.popup.code.open]); + if (!p.ui.popup.code.open) return null; return ( diff --git a/app/web/y.d.ts b/app/web/y.d.ts new file mode 100644 index 00000000..82307d97 --- /dev/null +++ b/app/web/y.d.ts @@ -0,0 +1,14 @@ +import type * as Y from "yjs"; +import { syncronize } from "y-pojo"; + +export import Doc = Y.Doc; +export import UndoManager = Y.UndoManager; +export import applyUpdate = Y.applyUpdate; +export import encodeStateVector = Y.encodeStateVector; +export import encodeStateAsUpdate = Y.encodeStateAsUpdate; +export import Text = Y.Text; +export import Map = Y.Map; +export import Array = Y.Array; +export import syncronize = syncronize; + +export as namespace Y;