Files
zulip/static/js/local_message.js
Anders Kaseorg 28f3dfa284 js: Automatically convert var to let and const in most files.
This commit was originally automatically generated using `tools/lint
--only=eslint --fix`.  It was then modified by tabbott to contain only
changes to a set of files that are unlikely to result in significant
merge conflicts with any open pull request, excluding about 20 files.
His plan is to merge the remaining changes with more precise care,
potentially involving merging parts of conflicting pull requests
before running the `eslint --fix` operation.

Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2019-11-03 12:42:39 -08:00

57 lines
1.7 KiB
JavaScript

function truncate_precision(float) {
return parseFloat(float.toFixed(3));
}
exports.now = function () {
const timestamp = new XDate().getTime() / 1000;
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);
};
exports.get_next_id = (function () {
const already_used = {};
return function () {
const local_id_increment = 0.01;
let latest = page_params.max_message_id;
if (typeof message_list.all !== 'undefined' && message_list.all.last() !== undefined) {
latest = message_list.all.last().id;
}
latest = Math.max(0, latest);
const next_local_id = truncate_precision(latest + local_id_increment);
if (already_used[next_local_id]) {
// 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;
}
if (next_local_id % 1 > local_id_increment * 5) {
blueslip.warn("Turning off local echo for this message to let host catch up");
return;
}
if (next_local_id % 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;
}
already_used[next_local_id] = true;
return next_local_id;
};
}());
window.local_message = exports;