Merge pull request #1508 from thelounge/xpaw/msg-preview-idle

Render link previews in browser idle event
This commit is contained in:
Jérémie Astori 2017-09-06 19:34:23 -04:00 committed by GitHub
commit 4d8fae1ab8
3 changed files with 18 additions and 12 deletions

View file

@ -3,17 +3,12 @@
const $ = require("jquery");
const socket = require("../socket");
const render = require("../render");
const utils = require("../utils");
const chat = $("#chat");
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);
}
// We set a maximum timeout of 2 seconds so that messages don't take too long to appear.
utils.requestIdleCallback(() => processReceivedMessage(data), 2000);
});
function processReceivedMessage(data) {

View file

@ -3,9 +3,9 @@
const $ = require("jquery");
const renderPreview = require("../renderPreview");
const socket = require("../socket");
const utils = require("../utils");
socket.on("msg:preview", function(data) {
const msg = $("#msg-" + data.id);
renderPreview(data.preview, msg);
// Previews are not as important, we can wait longer for them to appear
utils.requestIdleCallback(() => renderPreview(data.preview, $("#msg-" + data.id)), 6000);
});

View file

@ -12,7 +12,8 @@ module.exports = {
resetHeight,
setNick,
toggleNickEditor,
toggleNotificationMarkers
toggleNotificationMarkers,
requestIdleCallback,
};
function resetHeight(element) {
@ -77,3 +78,13 @@ function move(array, old_index, new_index) {
array.splice(new_index, 0, array.splice(old_index, 1)[0]);
return array;
}
function requestIdleCallback(callback, timeout) {
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.
window.requestIdleCallback(callback, {timeout: timeout});
} else {
callback();
}
}