2021-06-16 15:14:41 +02:00
|
|
|
class LiveQuotaStore {
|
|
|
|
|
|
|
|
private static instance: LiveQuotaStore
|
|
|
|
|
2023-05-22 15:52:59 +02:00
|
|
|
private readonly livesPerUser = new Map<number, { sessionId: string, size: number }[]>()
|
2021-06-16 15:14:41 +02:00
|
|
|
|
|
|
|
private constructor () {
|
|
|
|
}
|
|
|
|
|
2023-05-22 15:52:59 +02:00
|
|
|
addNewLive (userId: number, sessionId: string) {
|
2021-06-16 15:14:41 +02:00
|
|
|
if (!this.livesPerUser.has(userId)) {
|
|
|
|
this.livesPerUser.set(userId, [])
|
|
|
|
}
|
|
|
|
|
2023-05-22 15:52:59 +02:00
|
|
|
const currentUserLive = { sessionId, size: 0 }
|
2021-06-16 15:14:41 +02:00
|
|
|
const livesOfUser = this.livesPerUser.get(userId)
|
|
|
|
livesOfUser.push(currentUserLive)
|
|
|
|
}
|
|
|
|
|
2023-05-22 15:52:59 +02:00
|
|
|
removeLive (userId: number, sessionId: string) {
|
2021-06-16 15:14:41 +02:00
|
|
|
const newLivesPerUser = this.livesPerUser.get(userId)
|
2023-05-22 15:52:59 +02:00
|
|
|
.filter(o => o.sessionId !== sessionId)
|
2021-06-16 15:14:41 +02:00
|
|
|
|
|
|
|
this.livesPerUser.set(userId, newLivesPerUser)
|
|
|
|
}
|
|
|
|
|
2023-05-22 15:52:59 +02:00
|
|
|
addQuotaTo (userId: number, sessionId: string, size: number) {
|
2021-06-16 15:14:41 +02:00
|
|
|
const lives = this.livesPerUser.get(userId)
|
2023-05-22 15:52:59 +02:00
|
|
|
const live = lives.find(l => l.sessionId === sessionId)
|
2021-06-16 15:14:41 +02:00
|
|
|
|
|
|
|
live.size += size
|
|
|
|
}
|
|
|
|
|
|
|
|
getLiveQuotaOf (userId: number) {
|
|
|
|
const currentLives = this.livesPerUser.get(userId)
|
|
|
|
if (!currentLives) return 0
|
|
|
|
|
|
|
|
return currentLives.reduce((sum, obj) => sum + obj.size, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
static get Instance () {
|
|
|
|
return this.instance || (this.instance = new this())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export {
|
|
|
|
LiveQuotaStore
|
|
|
|
}
|