3 private static instance: LiveQuotaStore
5 private readonly livesPerUser = new Map<number, { sessionId: string, size: number }[]>()
7 private constructor () {
10 addNewLive (userId: number, sessionId: string) {
11 if (!this.livesPerUser.has(userId)) {
12 this.livesPerUser.set(userId, [])
15 const currentUserLive = { sessionId, size: 0 }
16 const livesOfUser = this.livesPerUser.get(userId)
17 livesOfUser.push(currentUserLive)
20 removeLive (userId: number, sessionId: string) {
21 const newLivesPerUser = this.livesPerUser.get(userId)
22 .filter(o => o.sessionId !== sessionId)
24 this.livesPerUser.set(userId, newLivesPerUser)
27 addQuotaTo (userId: number, sessionId: string, size: number) {
28 const lives = this.livesPerUser.get(userId)
29 const live = lives.find(l => l.sessionId === sessionId)
34 getLiveQuotaOf (userId: number) {
35 const currentLives = this.livesPerUser.get(userId)
36 if (!currentLives) return 0
38 return currentLives.reduce((sum, obj) => sum + obj.size, 0)
41 static get Instance () {
42 return this.instance || (this.instance = new this())