]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/live/live-quota-store.ts
Bumped to version v5.2.1
[github/Chocobozzz/PeerTube.git] / server / lib / live / live-quota-store.ts
CommitLineData
8ebf2a5d
C
1class LiveQuotaStore {
2
3 private static instance: LiveQuotaStore
4
3f0ceab0 5 private readonly livesPerUser = new Map<number, { sessionId: string, size: number }[]>()
8ebf2a5d
C
6
7 private constructor () {
8 }
9
3f0ceab0 10 addNewLive (userId: number, sessionId: string) {
8ebf2a5d
C
11 if (!this.livesPerUser.has(userId)) {
12 this.livesPerUser.set(userId, [])
13 }
14
3f0ceab0 15 const currentUserLive = { sessionId, size: 0 }
8ebf2a5d
C
16 const livesOfUser = this.livesPerUser.get(userId)
17 livesOfUser.push(currentUserLive)
18 }
19
3f0ceab0 20 removeLive (userId: number, sessionId: string) {
8ebf2a5d 21 const newLivesPerUser = this.livesPerUser.get(userId)
3f0ceab0 22 .filter(o => o.sessionId !== sessionId)
8ebf2a5d
C
23
24 this.livesPerUser.set(userId, newLivesPerUser)
25 }
26
3f0ceab0 27 addQuotaTo (userId: number, sessionId: string, size: number) {
8ebf2a5d 28 const lives = this.livesPerUser.get(userId)
3f0ceab0 29 const live = lives.find(l => l.sessionId === sessionId)
8ebf2a5d
C
30
31 live.size += size
32 }
33
34 getLiveQuotaOf (userId: number) {
35 const currentLives = this.livesPerUser.get(userId)
36 if (!currentLives) return 0
37
38 return currentLives.reduce((sum, obj) => sum + obj.size, 0)
39 }
40
41 static get Instance () {
42 return this.instance || (this.instance = new this())
43 }
44}
45
46export {
47 LiveQuotaStore
48}