2021-03-30 06:23:09 +02:00
|
|
|
import {all_messages_data} from "./all_messages_data";
|
2021-03-16 23:38:59 +01:00
|
|
|
import * as blueslip from "./blueslip";
|
2021-03-25 22:35:45 +01:00
|
|
|
import {page_params} from "./page_params";
|
2021-02-28 21:31:33 +01:00
|
|
|
|
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 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-30 06:23:09 +02:00
|
|
|
if (all_messages_data.last() !== undefined) {
|
|
|
|
latest = all_messages_data.last().id;
|
2018-02-24 13:42:27 +01:00
|
|
|
}
|
|
|
|
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
|
|
|
})();
|