summaryrefslogtreecommitdiff
path: root/packages/backend/src/server/api/stream/channels/queue-stats.ts
blob: b67600474bab0822b7d0d96d0183bf00504bdb4d (plain)
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
import Xev from 'xev';
import Channel from '../channel.js';

const ev = new Xev();

export default class extends Channel {
	public readonly chName = 'queueStats';
	public static shouldShare = true;
	public static requireCredential = false;

	constructor(id: string, connection: Channel['connection']) {
		super(id, connection);
		this.onStats = this.onStats.bind(this);
		this.onMessage = this.onMessage.bind(this);
	}

	public async init(params: any) {
		ev.addListener('queueStats', this.onStats);
	}

	private onStats(stats: any) {
		this.send('stats', stats);
	}

	public onMessage(type: string, body: any) {
		switch (type) {
			case 'requestLog':
				ev.once(`queueStatsLog:${body.id}`, statsLog => {
					this.send('statsLog', statsLog);
				});
				ev.emit('requestQueueStatsLog', {
					id: body.id,
					length: body.length,
				});
				break;
		}
	}

	public dispose() {
		ev.removeListener('queueStats', this.onStats);
	}
}