2021-03-16 23:38:59 +01:00
|
|
|
import * as blueslip from "./blueslip";
|
2021-02-28 21:33:40 +01:00
|
|
|
import * as message_events from "./message_events";
|
2021-02-28 21:31:33 +01:00
|
|
|
import * as message_list from "./message_list";
|
|
|
|
|
2018-02-24 13:42:27 +01:00
|
|
|
function truncate_precision(float) {
|
2020-10-07 09:17:30 +02:00
|
|
|
return Number.parseFloat(float.toFixed(3));
|
2018-02-24 13:42:27 +01:00
|
|
|
}
|
|
|
|
|
2021-02-28 00:49:57 +01:00
|
|
|
export function insert_message(message) {
|
2018-02-24 13:42:27 +01:00
|
|
|
// 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);
|
2021-02-28 00:49:57 +01:00
|
|
|
}
|
2018-02-24 13:42:27 +01:00
|
|
|
|
2021-02-28 00:49:57 +01:00
|
|
|
export const get_next_id_float = (function () {
|
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;
|
2021-03-24 20:14:12 +01:00
|
|
|
if (message_list.all.last() !== undefined) {
|
2018-02-24 13:42:27 +01:00
|
|
|
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.");
|
2020-09-24 07:50:36 +02:00
|
|
|
return undefined;
|
2018-02-24 13:42:27 +01:00
|
|
|
}
|
|
|
|
|
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");
|
2020-09-24 07:50:36 +02:00
|
|
|
return undefined;
|
2018-02-24 13:42:27 +01:00
|
|
|
}
|
|
|
|
|
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");
|
2020-09-24 07:50:36 +02:00
|
|
|
return undefined;
|
2018-02-24 13:42:27 +01:00
|
|
|
}
|
|
|
|
|
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
|
|
|
};
|
2020-07-16 22:35:58 +02:00
|
|
|
})();
|