summaryrefslogtreecommitdiff
path: root/src/server/api/stream/notes-stats.ts
blob: ab006200182754fbcb13c41fb2d5bbf75e7883c4 (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
import * as websocket from 'websocket';
import Xev from 'xev';

const ev = new Xev();

export default function(request: websocket.request, connection: websocket.connection): void {
	const onStats = (stats: any) => {
		connection.send(JSON.stringify({
			type: 'stats',
			body: stats
		}));
	};

	connection.on('message', async data => {
		const msg = JSON.parse(data.utf8Data);

		switch (msg.type) {
			case 'requestLog':
				ev.once('notesStatsLog:' + msg.id, statsLog => {
					connection.send(JSON.stringify({
						type: 'statsLog',
						body: statsLog
					}));
				});
				ev.emit('requestNotesStatsLog', msg.id);
				break;
		}
	});

	ev.addListener('notesStats', onStats);

	connection.on('close', () => {
		ev.removeListener('notesStats', onStats);
	});
}