Fix DataBoard
Related to bug https://github.com/TuxedoTako/4chan-xt/issues/7
This commit is contained in:
parent
d8c74f02ca
commit
5815070572
@ -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?.();
|
||||
|
||||
@ -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) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user