aboutsummaryrefslogtreecommitdiff
path: root/lib/services/data.js
diff options
context:
space:
mode:
authorBen Beltran <ben@nsovocal.com>2017-08-30 00:29:19 -0500
committerBen Beltran <ben@nsovocal.com>2017-08-30 00:29:19 -0500
commitb78f678de4f242b2a364bbf0cc39c8ec61ee5357 (patch)
tree92cb79b266ec9b5b609a8e8ba829bda3d7315ca8 /lib/services/data.js
parentdc7f608a828fc0b34a291af915727ab1f7d6ba0a (diff)
parent21f11b5ae525f52fd1a2bf1a741ad79bfccb1278 (diff)
Merge branch 'release/1.0.0'HEADmain
Diffstat (limited to 'lib/services/data.js')
-rw-r--r--lib/services/data.js69
1 files changed, 69 insertions, 0 deletions
diff --git a/lib/services/data.js b/lib/services/data.js
new file mode 100644
index 0000000..c7f6744
--- /dev/null
+++ b/lib/services/data.js
@@ -0,0 +1,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;
+ }
+};