close threads when ending message has been received

This commit is contained in:
Jakob Ketterl 2020-04-20 18:31:45 +02:00
parent 5ba77012a7
commit f474ab94d2

View File

@ -1,7 +1,7 @@
Js8Thread = function(el){
this.messages = [];
this.el = el;
}
};
Js8Thread.prototype.getAverageFrequency = function(){
var total = this.messages.map(function(message){
@ -10,12 +10,12 @@ Js8Thread.prototype.getAverageFrequency = function(){
return t + f;
}, 0);
return total / this.messages.length;
}
};
Js8Thread.prototype.pushMessage = function(message) {
this.messages.push(message);
this.render();
}
};
Js8Thread.prototype.render = function() {
this.el.html(
@ -23,34 +23,36 @@ Js8Thread.prototype.render = function() {
'<td class="decimal freq">' + Math.round(this.getAverageFrequency()) + '</td>' +
'<td class="message">&lrm;' + this.renderMessages() + '</td>'
);
}
};
Js8Thread.prototype.getLatestTimestamp = function() {
var startingMessages = this.messages.filter(function(m){
return m.thread_type & 1;
});
if (startingMessages.length) {
return startingMessages[startingMessages.length - 1].timestamp;
}
return this.messages[0].timestamp;
}
};
Js8Thread.prototype.isOpen = function() {
if (!this.messages.length) return true;
var last_message = this.messages[this.messages.length - 1];
return (last_message.thread_type & 2) === 0;
};
Js8Thread.prototype.renderMessages = function() {
res = []
var res = [];
for (var i = 0; i < this.messages.length; i++) {
var msg = this.messages[i];
if (msg.thread_type & 1) {
res.push('[ ');
} else if (i == 0 || msg.timestamp - this.messages[i - 1].timestamp > 15000) {
} else if (i === 0 || msg.timestamp - this.messages[i - 1].timestamp > 15000) {
res.push(' ... ');
}
res.push(msg.msg);
if (msg.thread_type & 2) {
res.push(' ]');
} else if (i === this.messages.length -1) {
res.push(' ... ');
}
}
return res.join('');
}
};
Js8Thread.prototype.renderTimestamp = function(timestamp) {
var t = new Date(timestamp);
@ -58,7 +60,7 @@ Js8Thread.prototype.renderTimestamp = function(timestamp) {
return ('' + i).padStart(2, "0");
};
return pad(t.getUTCHours()) + pad(t.getUTCMinutes()) + pad(t.getUTCSeconds());
}
};
Js8Thread.prototype.purgeOldMessages = function() {
var now = new Date().getTime();
@ -72,7 +74,7 @@ Js8Thread.prototype.purgeOldMessages = function() {
this.render();
}
return this.messages.length;
}
};
Js8Threader = function(el){
this.threads = [];
@ -92,26 +94,26 @@ Js8Threader.prototype.purgeOldMessages = function() {
Js8Threader.prototype.findThread = function(freq) {
var matching = this.threads.filter(function(thread) {
// max frequency deviation: 5 Hz. this may be a little tight.
return Math.abs(thread.getAverageFrequency() - freq) <= 5;
return thread.isOpen() && Math.abs(thread.getAverageFrequency() - freq) <= 5;
});
return matching[0] || false;
}
};
Js8Threader.prototype.pushMessage = function(message) {
var thread = this.findThread(message.freq);
if (!thread) {
var line = $("<tr></tr>")
var line = $("<tr></tr>");
this.tbody.append(line);
var thread = new Js8Thread(line);
thread = new Js8Thread(line);
this.threads.push(thread);
}
thread.pushMessage(message);
this.tbody.scrollTop(this.tbody[0].scrollHeight);
}
};
$.fn.js8 = function() {
if (!this.data('threader')) {
this.data('threader', new Js8Threader(this));
}
return this.data('threader');
}
};