mirror of
https://github.com/zulip/zulip.git
synced 2025-11-09 00:18:12 +00:00
The only reason to use typeof foo === "undefined" is when foo is a global identifier that might not have been declared at all, so it might raise a ReferenceError if evaluated. For a variable declared with const or let or import, a function argument, or a complex expression, simply foo === undefined is equivalent. Some of these conditions have become impossible and can be removed entirely, and some can be replaced more idiomatically with default parameters (note that JavaScript does not share the Python misfeature of evaluating the default parameter at function declaration time). Signed-off-by: Anders Kaseorg <anders@zulip.com>
52 lines
1.7 KiB
JavaScript
52 lines
1.7 KiB
JavaScript
import * as blueslip from "./blueslip";
|
|
import * as message_events from "./message_events";
|
|
import * as message_list from "./message_list";
|
|
|
|
function truncate_precision(float) {
|
|
return Number.parseFloat(float.toFixed(3));
|
|
}
|
|
|
|
export function insert_message(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);
|
|
}
|
|
|
|
export const get_next_id_float = (function () {
|
|
const already_used = new Set();
|
|
|
|
return function () {
|
|
const local_id_increment = 0.01;
|
|
let latest = page_params.max_message_id;
|
|
if (message_list.all.last() !== undefined) {
|
|
latest = message_list.all.last().id;
|
|
}
|
|
latest = Math.max(0, latest);
|
|
const local_id_float = truncate_precision(latest + local_id_increment);
|
|
|
|
if (already_used.has(local_id_float)) {
|
|
// 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 undefined;
|
|
}
|
|
|
|
if (local_id_float % 1 > local_id_increment * 5) {
|
|
blueslip.warn("Turning off local echo for this message to let host catch up");
|
|
return undefined;
|
|
}
|
|
|
|
if (local_id_float % 1 === 0) {
|
|
// The logic to stop at 0.05 should prevent us from ever wrapping around
|
|
// to the next integer.
|
|
blueslip.error("Programming error");
|
|
return undefined;
|
|
}
|
|
|
|
already_used.add(local_id_float);
|
|
|
|
return local_id_float;
|
|
};
|
|
})();
|