From f0125cdc36f69eee16dd9aae59a6d48370bbbd79 Mon Sep 17 00:00:00 2001 From: ed Date: Thu, 12 Aug 2021 22:12:05 +0200 Subject: [PATCH] prevent massive stacks in chrome --- bin/mtag/res/yt-ipr.user.js | 3 +-- copyparty/web/browser.js | 21 +++++++++------------ copyparty/web/up2k.js | 8 ++------ copyparty/web/util.js | 33 +++++++++++++++++++++++++++++++++ 4 files changed, 45 insertions(+), 20 deletions(-) diff --git a/bin/mtag/res/yt-ipr.user.js b/bin/mtag/res/yt-ipr.user.js index dfac08f7..2497d10d 100644 --- a/bin/mtag/res/yt-ipr.user.js +++ b/bin/mtag/res/yt-ipr.user.js @@ -21,7 +21,6 @@ function main() { } function collect() { - setTimeout(collect, interval * 1000); try { var pd = document.querySelector('ytd-watch-flexy'); if (!pd) @@ -39,7 +38,7 @@ function main() { console.log("[yt-pdh]", ex); } } - collect(); + setInterval(collect, interval * 1000); } var scr = document.createElement('script'); diff --git a/copyparty/web/browser.js b/copyparty/web/browser.js index 0ba364bc..99569877 100644 --- a/copyparty/web/browser.js +++ b/copyparty/web/browser.js @@ -921,14 +921,16 @@ function playpause(e) { var mpui = (function () { var r = {}, nth = 0, - timeout = null, preloaded = null; r.progress_updater = function () { - clearTimeout(timeout); + timer.add(updater_impl, true); + }; + function updater_impl() { if (!mp.au) { widget.paused(true); + timer.rm(updater_impl); return; } @@ -970,9 +972,9 @@ var mpui = (function () { } } - if (!mp.au.paused) - timeout = setTimeout(r.progress_updater, 100); - }; + if (mp.au.paused) + timer.rm(updater_impl); + } r.progress_updater(); return r; })(); @@ -2822,7 +2824,7 @@ var treectl = (function () { } function onscroll() { - if (!entreed || treectl.hidden) + if (!entreed || treectl.hidden || document.visibilityState == 'hidden') return; var tree = ebi('tree'), @@ -2858,12 +2860,7 @@ var treectl = (function () { tree.style.height = treeh < 10 ? '' : treeh + 'px'; } } - - function periodic() { - onscroll(); - setTimeout(periodic, document.visibilityState ? 100 : 5000); - } - periodic(); + timer.add(onscroll, true); function onresize(e) { if (!entreed || treectl.hidden) diff --git a/copyparty/web/up2k.js b/copyparty/web/up2k.js index 90441486..57f3ad0d 100644 --- a/copyparty/web/up2k.js +++ b/copyparty/web/up2k.js @@ -929,21 +929,17 @@ function up2k_init(subtle) { } var tasker = (function () { - var tto = null, - running = false, + var running = false, was_busy = false; function defer() { running = false; - clearTimeout(tto); - tto = setTimeout(taskerd, 100); } function taskerd() { if (running) return; - clearTimeout(tto); if (crashed) return defer(); @@ -1034,7 +1030,7 @@ function up2k_init(subtle) { return defer(); } } - taskerd(); + timer.add(taskerd, true); return taskerd; })(); diff --git a/copyparty/web/util.js b/copyparty/web/util.js index 6d99fd4e..42110974 100644 --- a/copyparty/web/util.js +++ b/copyparty/web/util.js @@ -555,6 +555,39 @@ function hist_replace(url) { } +var timer = (function () { + var r = {}; + r.q = []; + r.last = 0; + + r.add = function (fun, run) { + r.rm(fun); + r.q.push(fun); + + if (run) + fun(); + }; + + r.rm = function (fun) { + apop(r.q, fun); + }; + + function impl() { + if (Date.now() - r.last < 69) + return; + + var q = r.q.slice(0); + for (var a = 0; a < q.length; a++) + q[a](); + + r.last = Date.now(); + } + setInterval(impl, 100); + + return r; +})(); + + var tt = (function () { var r = { "tt": mknod("div"),