aboutsummaryrefslogtreecommitdiff
path: root/src_js
diff options
context:
space:
mode:
authorbozo.kopic <bozo.kopic@gmail.com>2017-08-24 17:51:14 +0200
committerbozo.kopic <bozo.kopic@gmail.com>2017-08-24 17:51:14 +0200
commitc594b1fca854a7b9fb73d854a9830143cd1032fc (patch)
tree8c8b63cb4899238cf0f73a3f7bd08d9b70b16e81 /src_js
parentd736cd1392a56ad5103867c72761cfcb4ccd4f1b (diff)
frontend
Diffstat (limited to 'src_js')
-rw-r--r--src_js/hatter/common.js66
-rw-r--r--src_js/hatter/main.js29
2 files changed, 91 insertions, 4 deletions
diff --git a/src_js/hatter/common.js b/src_js/hatter/common.js
index 829685b..c6dcfcc 100644
--- a/src_js/hatter/common.js
+++ b/src_js/hatter/common.js
@@ -1,2 +1,66 @@
-export const defaultState = {};
+import r from 'hatter/renderer';
+import * as util from 'hatter/util';
+
+
+export const defaultState = {
+ conn: null,
+ log: {
+ offset: 0,
+ offsetText: '0',
+ limit: 0,
+ limitText: '0',
+ entries: []
+ },
+ job: {
+ active: null,
+ queue: []
+ },
+ repositories: []
+};
+
+
+export function processMsg(msg) {
+ if (msg.type == 'repositories') {
+ r.set('repositories', msg.repositories);
+ } else if (msg.type == 'active_job') {
+ r.set(['job', 'active'], msg.job);
+ } else if (msg.type == 'job_queue') {
+ r.set(['job', 'queue'], msg.jobs);
+ } else if (msg.type == 'log_entries') {
+ r.set(['log', 'entires'], msg.entries);
+ }
+}
+
+
+export function addJob(repository) {
+ r.get('conn').send(JSON.stringify({
+ type: 'add_job',
+ repository: repository,
+ commit: 'HEAD'
+ }));
+}
+
+
+bean.on(r, 'change', state => {
+ let newOffset = parseInt(state.log.offsetText);
+ let newLimit = parseInt(state.log.limitText);
+ if (util.isInteger(newOffset) && newOffset != state.log.offset) {
+ r.set(['log', 'offset'], newOffset).then(sendSetLog);
+ }
+ if (util.isInteger(newLimit) && newLimit != state.log.limit) {
+ r.set(['log', 'limit'], newLimit).then(sendSetLog);
+ }
+});
+
+
+function sendSetLog() {
+ let conn = r.get('conn');
+ if (!conn)
+ return;
+ conn.send(JSON.stringify({
+ type: 'set_log',
+ offset: r.get('offset'),
+ limit: r.get('limit')
+ }));
+}
diff --git a/src_js/hatter/main.js b/src_js/hatter/main.js
index 892b6af..3f64ed3 100644
--- a/src_js/hatter/main.js
+++ b/src_js/hatter/main.js
@@ -1,7 +1,7 @@
import bean from 'bean';
-import R from 'ramda';
import r from 'hatter/renderer';
+import * as util from 'hatter/util';
import * as common from 'hatter/common';
import * as vt from 'hatter/vt';
@@ -10,8 +10,31 @@ import 'style/main.scss';
function main() {
- let root = document.body.appendChild(document.createElement('div'));
- r.init(root, common.defaultState, vt.main);
+ let conn = new WebSocket(wsAddress);
+
+ conn.onopen = () => {
+ let root = document.body.appendChild(document.createElement('div'));
+ let state = util.set('conn', conn, common.defaultState);
+ r.init(root, state, vt.main);
+ };
+
+ conn.onclose = () => {
+ alert("Disconnected from server");
+ };
+
+ conn.onerror = () => {
+ alert("Couldn't connect to server");
+ };
+
+ conn.onmessage = (evt) => {
+ try {
+ let msg = JSON.parse(evt.data);
+ common.processMsg(msg);
+ } catch(e) {
+ conn.close();
+ throw e;
+ }
+ };
}