fix activity

This commit is contained in:
Rizky 2023-11-01 22:08:34 +07:00
parent 0231530bcd
commit 2dfa03e97d
3 changed files with 73 additions and 6 deletions

View File

@ -0,0 +1,5 @@
import { Room } from "./room";
export const activity = {
site: {} as Record<string, Room<{ code: { name: string } }>>,
};

View File

@ -0,0 +1,57 @@
import { ServerWebSocket } from "bun";
import { WSData } from "../../../../../pkgs/core/server/create";
import { conns } from "./conn";
export class Room<T extends Record<string, true | Record<string, any>>> {
name = "";
clients = new Map<ServerWebSocket<WSData>, Partial<T>>();
constructor() {}
findAll(where: Partial<T>) {
const clients = new Map<ServerWebSocket<WSData>, Partial<T>>();
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<WSData>; id?: string }) {
let ws = null as ServerWebSocket<WSData> | 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<WSData>; id?: string }) {
const ws = this.identify(client);
if (ws) {
this.clients.set(ws, {});
}
}
leave(client: { ws?: ServerWebSocket<WSData>; id?: string }) {
const ws = this.identify(client);
if (ws) {
this.clients.delete(ws);
}
}
}

View File

@ -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();