File size: 1,206 Bytes
d092f57
 
fa45681
 
 
d092f57
fa45681
 
d092f57
 
fa45681
 
 
d092f57
 
fa45681
 
 
d092f57
 
fa45681
 
d092f57
 
fa45681
 
d092f57
 
fa45681
 
d092f57
 
fa45681
 
d092f57
 
fa45681
 
 
d092f57
 
fa45681
 
 
d092f57
 
fa45681
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import { RoomState } from "./types"

// In-memory stores
const rooms = new Map<string, RoomState>()
let userCount = 0

export const getRoom = async (roomId: string): Promise<RoomState | null> => {
  return rooms.get(roomId) ?? null
}

export const roomExists = async (roomId: string): Promise<number> => {
  // Keep Redis-like return type (0/1) for compatibility
  return rooms.has(roomId) ? 1 : 0
}

export const setRoom = async (roomId: string, data: RoomState): Promise<"OK"> => {
  rooms.set(roomId, data)
  return "OK"
}

export const deleteRoom = async (roomId: string): Promise<number> => {
  return rooms.delete(roomId) ? 1 : 0
}

export const listRooms = async (): Promise<string[]> => {
  return Array.from(rooms.keys())
}

export const countRooms = async (): Promise<number> => {
  return rooms.size
}

export const countUsers = async (): Promise<number> => {
  return userCount
}

export const incUsers = async (): Promise<number> => {
  userCount += 1
  return userCount
}

export const decUsers = async (): Promise<number> => {
  userCount = Math.max(0, userCount - 1)
  return userCount
}

export const wipeCache = async (): Promise<"OK"> => {
  rooms.clear()
  userCount = 0
  return "OK"
}