Process chat messages in equestIdleCallback if available

This commit is contained in:
Pavel Djundik 2017-08-25 15:30:47 +03:00
parent d4e02d96fa
commit f56c748015

View File

@ -7,6 +7,17 @@ const chat = $("#chat");
const templates = require("../../views");
socket.on("msg", function(data) {
if (window.requestIdleCallback) {
// During an idle period the user agent will run idle callbacks in FIFO order
// until either the idle period ends or there are no more idle callbacks eligible to be run.
// We set a maximum timeout of 2 seconds so that messages don't take too long to appear.
window.requestIdleCallback(() => processReceivedMessage(data), {timeout: 2000});
} else {
processReceivedMessage(data);
}
});
function processReceivedMessage(data) {
const msg = render.buildChatMessage(data);
const targetId = data.chan;
const target = "#chan-" + targetId;
@ -73,4 +84,4 @@ socket.on("msg", function(data) {
}
});
}
});
}