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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
/* global WebSocket */
const internals = {
kSocketLocation: 'ws://localhost:1987',
data: {
state: 0,
runningAverages: {},
winner: null
},
initSocket() {
internals.socket = new WebSocket(internals.kSocketLocation);
internals.socket.addEventListener('message', (data) => {
Object.assign(internals.data, JSON.parse(data.data));
});
}
};
/**
* The data structure representing the sorting hat data
*
* @typedef tSortingHatData
* @type object
* @param {number} state the current state: 0 for waiting, 1 for
* polling, 2 for cool down.
* @param {string} [winner] the winner after polling, might be null if
* no winner is detected
* @param {Object.<string,tRunningAverages>} runningAverages the running averages for
* the different categories, used to render the waves
*/
/**
* The running averages, including the current sum and count
*
* @typedef tRunningAverages
* @type object
* @param {number} sum the current total
* @param {number} count the number of samples
* @param {number} average the average (sum / count)
*/
/**
* The main data service, connects to a socket and updates the internal
* data structure
*
* @class DataService
*/
export default {
/**
* Returns the internal data structure, intended to be used as the data
* property in vue components
*
* @memberof DataService
* @method data
* @return tSortingHatData
*/
data() {
if (!internals.socket) {
internals.initSocket();
}
return internals.data;
}
};
|