From 2dfa03e97d5ba6b783873ad83982b77c8e01c228 Mon Sep 17 00:00:00 2001 From: Rizky Date: Wed, 1 Nov 2023 22:08:34 +0700 Subject: [PATCH] fix activity --- app/srv/ws/sync/entity/activity.ts | 5 +++ app/srv/ws/sync/entity/room.ts | 57 ++++++++++++++++++++++++++++++ pkgs/core/index.ts | 17 +++++---- 3 files changed, 73 insertions(+), 6 deletions(-) create mode 100644 app/srv/ws/sync/entity/activity.ts create mode 100644 app/srv/ws/sync/entity/room.ts diff --git a/app/srv/ws/sync/entity/activity.ts b/app/srv/ws/sync/entity/activity.ts new file mode 100644 index 00000000..d4b4af41 --- /dev/null +++ b/app/srv/ws/sync/entity/activity.ts @@ -0,0 +1,5 @@ +import { Room } from "./room"; + +export const activity = { + site: {} as Record>, +}; diff --git a/app/srv/ws/sync/entity/room.ts b/app/srv/ws/sync/entity/room.ts new file mode 100644 index 00000000..71104c28 --- /dev/null +++ b/app/srv/ws/sync/entity/room.ts @@ -0,0 +1,57 @@ +import { ServerWebSocket } from "bun"; +import { WSData } from "../../../../../pkgs/core/server/create"; +import { conns } from "./conn"; + +export class Room>> { + name = ""; + clients = new Map, Partial>(); + + constructor() {} + + findAll(where: Partial) { + const clients = new Map, Partial>(); + 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; + } + + private identify(client: { ws?: ServerWebSocket; id?: string }) { + let ws = null as ServerWebSocket | null; + if (client.ws) { + ws = client.ws; + } else if (client.id) { + const connws = conns.get(client.id)?.ws; + if (connws) ws = connws; + } + return ws; + } + + join(client: { ws?: ServerWebSocket; id?: string }) { + const ws = this.identify(client); + if (ws) { + this.clients.set(ws, {}); + } + } + + leave(client: { ws?: ServerWebSocket; id?: string }) { + const ws = this.identify(client); + if (ws) { + this.clients.delete(ws); + } + } +} diff --git a/pkgs/core/index.ts b/pkgs/core/index.ts index 0e9c3b89..652d548d 100644 --- a/pkgs/core/index.ts +++ b/pkgs/core/index.ts @@ -26,27 +26,32 @@ if (g.mode === "dev") { await startDevWatcher(); } - /** init lmdb */ user.conf.init(); snapshot.init(); - await preparePrisma(); await ensureNotRunning(); if (g.db) { - g.db.$connect().catch((e: any) => { - g.log.error(`[DB ERROR]\n${e.message}`); - }); + g.db + .$connect() + .catch((e: any) => { + g.log.error(`[DB ERROR]\n${e.message}`); + }) + .then(() => { + g.log.info("Database connected"); + }); } - await initSrv(); await syncActionDefinition(); +g.log.info("WS Action defined"); await generateAPIFrm(); await prepareApiRoutes(); await prepareAPITypes(); +g.log.info("API Prepared"); + await parcelBuild(); await createServer();