]> git.r.bdr.sh - rbdr/sorting-hat-renderer/blob - lib/services/data.js
Merge branch 'release/1.0.0'
[rbdr/sorting-hat-renderer] / lib / services / data.js
1 /* global WebSocket */
2
3 const internals = {
4 kSocketLocation: 'ws://localhost:1987',
5
6 data: {
7 state: 0,
8 runningAverages: {},
9 winner: null
10 },
11
12 initSocket() {
13
14 internals.socket = new WebSocket(internals.kSocketLocation);
15 internals.socket.addEventListener('message', (data) => {
16
17 Object.assign(internals.data, JSON.parse(data.data));
18 });
19 }
20 };
21
22 /**
23 * The data structure representing the sorting hat data
24 *
25 * @typedef tSortingHatData
26 * @type object
27 * @param {number} state the current state: 0 for waiting, 1 for
28 * polling, 2 for cool down.
29 * @param {string} [winner] the winner after polling, might be null if
30 * no winner is detected
31 * @param {Object.<string,tRunningAverages>} runningAverages the running averages for
32 * the different categories, used to render the waves
33 */
34
35 /**
36 * The running averages, including the current sum and count
37 *
38 * @typedef tRunningAverages
39 * @type object
40 * @param {number} sum the current total
41 * @param {number} count the number of samples
42 * @param {number} average the average (sum / count)
43 */
44
45 /**
46 * The main data service, connects to a socket and updates the internal
47 * data structure
48 *
49 * @class DataService
50 */
51 export default {
52
53 /**
54 * Returns the internal data structure, intended to be used as the data
55 * property in vue components
56 *
57 * @memberof DataService
58 * @method data
59 * @return tSortingHatData
60 */
61 data() {
62
63 if (!internals.socket) {
64 internals.initSocket();
65 }
66
67 return internals.data;
68 }
69 };