diff --git a/src/classes/DataBoard.js b/src/classes/DataBoard.js index 0271cf7f3..b17a33b07 100644 --- a/src/classes/DataBoard.js +++ b/src/classes/DataBoard.js @@ -13,11 +13,10 @@ import { dict, HOUR } from "../platform/helpers"; export default class DataBoard { static initClass() { this.keys = ['hiddenThreads', 'hiddenPosts', 'lastReadPosts', 'yourPosts', 'watchedThreads', 'watcherLastModified', 'customTitles']; - - this.changes = []; } constructor(key, sync, dontClean) { + this.changes = []; this.onSync = this.onSync.bind(this); this.key = key; this.initData(Conf[this.key]); @@ -48,15 +47,15 @@ export default class DataBoard { save(change, cb) { change(); - DataBoard.changes.push(change); + this.changes.push(change); return $.get(this.key, { boards: dict() }, items => { - if (!DataBoard.changes.length) { return; } + if (!this.changes.length) { return; } const needSync = ((items[this.key].version || 0) > (this.data.version || 0)); if (needSync) { this.initData(items[this.key]); - for (change of DataBoard.changes) { change(); } + for (change of this.changes) { change(); } } - DataBoard.changes = []; + this.changes = []; this.data.version = (this.data.version || 0) + 1; return $.set(this.key, this.data, () => { if (needSync) { this.sync?.(); } @@ -69,7 +68,7 @@ export default class DataBoard { return $.get(this.key, { boards: dict() }, items => { if ((items[this.key].version || 0) > (this.data.version || 0)) { this.initData(items[this.key]); - for (var change of DataBoard.changes) { change(); } + for (var change of this.changes) { change(); } this.sync?.(); } return cb?.(); diff --git a/src/platform/$.js b/src/platform/$.js index 2ecdbc9e9..d230c447e 100644 --- a/src/platform/$.js +++ b/src/platform/$.js @@ -490,27 +490,16 @@ $.debounce = function(wait, fn) { }; $.queueTask = (function() { - // inspired by https://www.w3.org/Bugs/Public/show_bug.cgi?id=15007 const taskQueue = []; const execTask = function() { - const task = taskQueue.shift(); - const func = task[0]; - const args = Array.prototype.slice.call(task, 1); - return func.apply(func, args); + const [func, ...args] = taskQueue.shift(); + func(...args); + }; + return function() { + taskQueue.push(arguments); + // setTimeout is throttled in background tabs on firefox + Promise.resolve().then(execTask); }; - if (window.MessageChannel) { - const taskChannel = new MessageChannel(); - taskChannel.port1.onmessage = execTask; - return function() { - taskQueue.push(arguments); - return taskChannel.port2.postMessage(null); - }; - } else { // XXX Firefox - return function() { - taskQueue.push(arguments); - return setTimeout(execTask, 0); - }; - } })(); $.global = function(fn, data) {