2018-02-24 13:42:27 +01:00
|
|
|
function truncate_precision(float) {
|
|
|
|
return parseFloat(float.toFixed(3));
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.now = function () {
|
2019-11-02 00:06:25 +01:00
|
|
|
const timestamp = new XDate().getTime() / 1000;
|
2018-02-24 13:42:27 +01:00
|
|
|
|
|
|
|
return timestamp;
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.insert_message = function (message) {
|
|
|
|
// It is a little bit funny to go through the message_events
|
|
|
|
// codepath, but it's sort of the idea behind local echo that
|
|
|
|
// we are simulating server events before they actually arrive.
|
|
|
|
message_events.insert_new_messages([message], true);
|
|
|
|
};
|
|
|
|
|
2020-02-12 09:32:25 +01:00
|
|
|
exports.get_next_id_float = (function () {
|
2018-02-24 13:42:27 +01:00
|
|
|
|
2020-02-12 06:25:48 +01:00
|
|
|
const already_used = new Set();
|
2018-02-24 13:42:27 +01:00
|
|
|
|
|
|
|
return function () {
|
2019-11-02 00:06:25 +01:00
|
|
|
const local_id_increment = 0.01;
|
|
|
|
let latest = page_params.max_message_id;
|
2018-02-24 13:42:27 +01:00
|
|
|
if (typeof message_list.all !== 'undefined' && message_list.all.last() !== undefined) {
|
|
|
|
latest = message_list.all.last().id;
|
|
|
|
}
|
|
|
|
latest = Math.max(0, latest);
|
2020-02-12 09:32:25 +01:00
|
|
|
const local_id_float = truncate_precision(latest + local_id_increment);
|
2018-02-24 13:42:27 +01:00
|
|
|
|
2020-02-12 06:25:48 +01:00
|
|
|
if (already_used.has(local_id_float)) {
|
2018-02-24 13:42:27 +01:00
|
|
|
// If our id is already used, it is probably an edge case like we had
|
|
|
|
// to abort a very recent message.
|
|
|
|
blueslip.warn("We don't reuse ids for local echo.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-02-12 09:32:25 +01:00
|
|
|
if (local_id_float % 1 > local_id_increment * 5) {
|
2018-02-24 13:42:27 +01:00
|
|
|
blueslip.warn("Turning off local echo for this message to let host catch up");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-02-12 09:32:25 +01:00
|
|
|
if (local_id_float % 1 === 0) {
|
2018-02-24 13:42:27 +01:00
|
|
|
// The logic to stop at 0.05 should prevent us from ever wrapping around
|
|
|
|
// to the next integer.
|
|
|
|
blueslip.error("Programming error");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-02-12 06:25:48 +01:00
|
|
|
already_used.add(local_id_float);
|
2018-02-24 13:42:27 +01:00
|
|
|
|
2020-02-12 09:32:25 +01:00
|
|
|
return local_id_float;
|
2018-02-24 13:42:27 +01:00
|
|
|
};
|
|
|
|
}());
|
|
|
|
|
2019-10-25 09:45:13 +02:00
|
|
|
window.local_message = exports;
|