Homogenize indentation (dom and messaging were using spaces) to tabs

This commit is contained in:
Tissevert 2018-11-29 22:09:09 +01:00
parent 242b97e745
commit fff6ac34a1
2 changed files with 78 additions and 78 deletions

View File

@ -1,36 +1,36 @@
function Dom() { function Dom() {
return { return {
clear: clear, clear: clear,
make: make make: make
} }
function clear(elem) { function clear(elem) {
while(elem.firstChild) { while(elem.firstChild) {
elem.removeChild(elem.firstChild); elem.removeChild(elem.firstChild);
} }
} }
function make(tag, properties, children) { function make(tag, properties, children) {
var e = document.createElement(tag); var e = document.createElement(tag);
properties = properties || {}; properties = properties || {};
children = children || []; children = children || [];
for(key in properties) { for(key in properties) {
var value = properties[key]; var value = properties[key];
switch(key) { switch(key) {
case "class": case "class":
e.className = Array.isArray(value) ? value.join(' ') : value; e.className = Array.isArray(value) ? value.join(' ') : value;
break;; break;;
case "onClick": case "onClick":
e.addEventListener("click", value); e.addEventListener("click", value);
break;; break;;
default: default:
e[key] = value; e[key] = value;
} }
} }
for(var i = 0; i < children.length; i++) { for(var i = 0; i < children.length; i++) {
e.appendChild(children[i]); e.appendChild(children[i]);
} }
return e; return e;
} }
} }

View File

@ -1,62 +1,62 @@
function Messaging() { function Messaging() {
var ws = new WebSocket('ws://' + window.location.hostname + '/play/'); var ws = new WebSocket('ws://' + window.location.hostname + '/play/');
var keepAlivePeriod = 20000; var keepAlivePeriod = 20000;
var routes = {callbacks: [], children: {}}; var routes = {callbacks: [], children: {}};
return { return {
addEventListener: addEventListener, addEventListener: addEventListener,
send: send, send: send,
start: start start: start
} }
function get(obj, path, write) { function get(obj, path, write) {
write = write || false; write = write || false;
if(path.length < 1) { if(path.length < 1) {
return obj; return obj;
} else { } else {
if(obj.children[path[0]] == undefined && write) { if(obj.children[path[0]] == undefined && write) {
obj.children[path[0]] = {callbacks: [], children: {}}; obj.children[path[0]] = {callbacks: [], children: {}};
} }
if(obj.children[path[0]] != undefined) { if(obj.children[path[0]] != undefined) {
return get(obj.children[path[0]], path.slice(1), write); return get(obj.children[path[0]], path.slice(1), write);
} else { } else {
return null; return null;
} }
} }
} }
function addEventListener(path, callback) { function addEventListener(path, callback) {
var route = get(routes, path, true); var route = get(routes, path, true);
route.callbacks.push(callback); route.callbacks.push(callback);
} }
function messageListener(event) { function messageListener(event) {
var o = JSON.parse(event.data); var o = JSON.parse(event.data);
var path = []; var path = [];
var tmp = o; var tmp = o;
while(tmp != undefined && tmp.tag != undefined) { while(tmp != undefined && tmp.tag != undefined) {
path.push(tmp.tag); path.push(tmp.tag);
tmp = tmp.message; tmp = tmp.message;
} }
var route = get(routes, path); var route = get(routes, path);
if(route != undefined && route.callbacks != undefined) { if(route != undefined && route.callbacks != undefined) {
route.callbacks.forEach(function(f) {f(o);}); route.callbacks.forEach(function(f) {f(o);});
} else { } else {
debug.textContent = event.data; debug.textContent = event.data;
} }
}; };
function start() { function start() {
//ping(); //ping();
addEventListener(["Pong"], ping); addEventListener(["Pong"], ping);
ws.addEventListener('message', messageListener); ws.addEventListener('message', messageListener);
} }
function send(o) { function send(o) {
ws.send(JSON.stringify(o)); ws.send(JSON.stringify(o));
} }
function ping() { function ping() {
setTimeout(function() {send({tag: "Ping"});}, keepAlivePeriod); setTimeout(function() {send({tag: "Ping"});}, keepAlivePeriod);
} }
} }