Total Complexity | 5 |
Complexity/F | 2.5 |
Lines of Code | 40 |
Function Count | 2 |
Duplicated Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
1 | const io = require('socket.io-client'); |
||
2 | const isObject = require('is-it-object'); |
||
3 | |||
4 | const app = require('./../app'); |
||
5 | const counter = require('./../services/counter'); |
||
6 | const renderJson = require('./../services/renderJson'); |
||
7 | |||
8 | const socket = io.connect(); |
||
9 | |||
10 | /** |
||
11 | * Handler for WebSocket 'connect' event. |
||
12 | * Emits 'join' event which is intended to join WebSocket room for current streamId. |
||
13 | */ |
||
14 | socket.on('connect', () => { |
||
15 | socket.emit('join', app.getStreamId()); |
||
16 | }); |
||
17 | |||
18 | /** |
||
19 | * Handler for new data. |
||
20 | * Renders data on web page (add to the bottom). |
||
21 | * |
||
22 | * @event LOG.NEW |
||
23 | */ |
||
24 | socket.on('log', (data) => { |
||
25 | if (isObject(data) === false) { |
||
26 | return; |
||
27 | } |
||
28 | |||
29 | // It is only way to render proper data because socket.io rooms disabled here |
||
30 | // @see https://github.com/cn007b/log/blob/master/src/back/routes/index.js:52 |
||
31 | if (data.streamId !== app.getStreamId()) { |
||
32 | return; |
||
33 | } |
||
34 | |||
35 | if (data.format === 'json') { |
||
36 | /** global: app */ |
||
37 | renderJson(data, app.getAutoScroll()); |
||
38 | counter.inc(); |
||
39 | } |
||
40 | }); |
||
41 |