Fix DataBoard

Related to bug https://github.com/TuxedoTako/4chan-xt/issues/7
This commit is contained in:
Tuxedo Takodachi 2023-09-02 16:36:06 +02:00
parent d8c74f02ca
commit 5815070572
2 changed files with 13 additions and 25 deletions

View File

@ -13,11 +13,10 @@ import { dict, HOUR } from "../platform/helpers";
export default class DataBoard { export default class DataBoard {
static initClass() { static initClass() {
this.keys = ['hiddenThreads', 'hiddenPosts', 'lastReadPosts', 'yourPosts', 'watchedThreads', 'watcherLastModified', 'customTitles']; this.keys = ['hiddenThreads', 'hiddenPosts', 'lastReadPosts', 'yourPosts', 'watchedThreads', 'watcherLastModified', 'customTitles'];
this.changes = [];
} }
constructor(key, sync, dontClean) { constructor(key, sync, dontClean) {
this.changes = [];
this.onSync = this.onSync.bind(this); this.onSync = this.onSync.bind(this);
this.key = key; this.key = key;
this.initData(Conf[this.key]); this.initData(Conf[this.key]);
@ -48,15 +47,15 @@ export default class DataBoard {
save(change, cb) { save(change, cb) {
change(); change();
DataBoard.changes.push(change); this.changes.push(change);
return $.get(this.key, { boards: dict() }, items => { 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)); const needSync = ((items[this.key].version || 0) > (this.data.version || 0));
if (needSync) { if (needSync) {
this.initData(items[this.key]); 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; this.data.version = (this.data.version || 0) + 1;
return $.set(this.key, this.data, () => { return $.set(this.key, this.data, () => {
if (needSync) { this.sync?.(); } if (needSync) { this.sync?.(); }
@ -69,7 +68,7 @@ export default class DataBoard {
return $.get(this.key, { boards: dict() }, items => { return $.get(this.key, { boards: dict() }, items => {
if ((items[this.key].version || 0) > (this.data.version || 0)) { if ((items[this.key].version || 0) > (this.data.version || 0)) {
this.initData(items[this.key]); this.initData(items[this.key]);
for (var change of DataBoard.changes) { change(); } for (var change of this.changes) { change(); }
this.sync?.(); this.sync?.();
} }
return cb?.(); return cb?.();

View File

@ -490,27 +490,16 @@ $.debounce = function(wait, fn) {
}; };
$.queueTask = (function() { $.queueTask = (function() {
// inspired by https://www.w3.org/Bugs/Public/show_bug.cgi?id=15007
const taskQueue = []; const taskQueue = [];
const execTask = function() { const execTask = function() {
const task = taskQueue.shift(); const [func, ...args] = taskQueue.shift();
const func = task[0]; func(...args);
const args = Array.prototype.slice.call(task, 1); };
return func.apply(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) { $.global = function(fn, data) {