Files
wekan/imports/lib/dataCache.js
T
Lauri Ojansivu d00dc6056b Fix the "Board not found" flicker (stale-while-revalidate for the client board cache)
The client board cache (imports/lib/dataCache.js) re-fetches its value inside a
reactive computation. When the board doc is briefly absent from minimongo (a
subscription stops and restarts, momentarily removing the doc), the empty value
was surfaced immediately, flashing the "Board not found" shell — and on WebKit
throwing a Blaze "Can't select in removed DomRange" error tearing down the card
view. It self-recovers when the doc is re-delivered, so it presents as a flicker,
reliably hit only on Firefox/WebKit timing (which is why it surfaced as
browser-specific Playwright failures in 14-voting-watchers and 24-feature-issues).

Fixed with an opt-in stale-while-revalidate mode on DataCache, enabled only for
getBoard: a transient miss over an already-cached board keeps the last value and
re-checks after a short delay, surfacing an empty result only if the board is
still gone then (a genuine deletion / access loss). First-ever loads and caches
that did not opt in are unchanged. Core decision extracted to the pure
shouldDeferCacheMiss helper with unit tests.

Also hardens the related Playwright specs: a retry in CardPage.openActionsMenu, a
longer visibility budget for the WebKit slow card-render in spec 16, and removes a
reload race in the move-list-right spec (moveListBy fires two un-awaited
updateListSort calls, so an immediate reload read a half-applied order).
2026-07-06 01:26:05 +03:00

177 lines
5.0 KiB
JavaScript

import { Meteor } from 'meteor/meteor';
import { Tracker } from 'meteor/tracker';
const { shouldDeferCacheMiss } = require('./staleWhileRevalidate');
class ReactiveValueCache {
constructor(compare, shouldStop) {
this.shouldStop = shouldStop || (() => true);
this.compare = compare || ((a, b) => a === b);
this.values = {};
this.deps = {};
}
ensureDependency(key) {
if (!this.deps[key]) {
this.deps[key] = new Tracker.Dependency();
}
return this.deps[key];
}
checkDeletion(key) {
const dep = this.ensureDependency(key);
if (dep.hasDependents()) {
return false;
}
delete this.values[key];
delete this.deps[key];
return true;
}
del(key) {
const dep = this.ensureDependency(key);
delete this.values[key];
if (this.checkDeletion(key)) {
return;
}
dep.changed();
}
set(key, data, bypassCompare) {
const dep = this.ensureDependency(key);
const current = this.values[key];
this.values[key] = data;
if (!this.compare(current, data) || bypassCompare) {
dep.changed();
}
}
get(key) {
const data = this.values[key];
if (Tracker.currentComputation) {
const dep = this.ensureDependency(key);
dep.depend();
Tracker.currentComputation.onStop(() => {
if (!this.shouldStop(key)) {
return;
}
this.checkDeletion(key);
});
}
return data;
}
// Non-reactive read of the currently cached value (no dependency registered).
peek(key) {
return this.values[key];
}
}
class DataCache {
constructor(getData, options) {
this.options = {
timeout: 60 * 1000,
...(typeof options === 'function' ? { compare: options } : options),
};
this.getData = getData;
this.cache = new ReactiveValueCache(this.options.compare, () => false);
this.timeouts = {};
this.computations = {};
// Pending stale-while-revalidate re-checks, keyed like the cache.
this.staleTimers = {};
}
ensureComputation(key) {
if (this.timeouts[key]) {
clearTimeout(this.timeouts[key]);
delete this.timeouts[key];
}
if (this.computations[key] && !this.computations[key].stopped) {
return;
}
this.computations[key] = Tracker.nonreactive(() =>
Tracker.autorun(() => {
this.applyData(key, this.getData(key));
}),
);
this.computations[key].onInvalidate(() => this.checkStop(key));
}
// Write a freshly-fetched value into the cache, honouring stale-while-revalidate.
// A transient miss (null/undefined while a value is still cached) is not written
// immediately: keep the last value and re-check after options.staleMs, so a board
// that momentarily vanishes from minimongo during a subscription re-settle does
// not flash the "Board not found" shell. A miss that is still a miss after the
// delay (genuine deletion / access loss) is then surfaced.
applyData(key, data) {
if (shouldDeferCacheMiss(this.options, data, this.cache.peek(key))) {
if (this.staleTimers[key]) {
return; // a re-check is already pending
}
this.staleTimers[key] = setTimeout(() => {
delete this.staleTimers[key];
const fresh = Tracker.nonreactive(() => this.getData(key));
this.cache.set(key, fresh);
}, this.options.staleMs || 1500);
return;
}
// Real value (or no cached value to preserve): resolve now, cancel any pending
// re-check — the live autorun already delivered a fresher value.
if (this.staleTimers[key]) {
clearTimeout(this.staleTimers[key]);
delete this.staleTimers[key];
}
this.cache.set(key, data);
}
checkStop(key) {
if (this.cache.ensureDependency(key).hasDependents()) {
return;
}
if (this.timeouts[key]) {
clearTimeout(this.timeouts[key]);
delete this.timeouts[key];
}
this.timeouts[key] = setTimeout(() => {
delete this.timeouts[key];
if (!this.computations[key]) {
return;
}
// A dependent may have re-attached during the timeout window (e.g. the
// board view re-rendered). Re-check before tearing down, otherwise we
// stop the live computation and delete a value something is still using,
// which surfaces as a transient `undefined` (the board-not-found flicker).
if (this.cache.ensureDependency(key).hasDependents()) {
return;
}
this.computations[key].stop();
delete this.computations[key];
if (this.staleTimers[key]) {
clearTimeout(this.staleTimers[key]);
delete this.staleTimers[key];
}
this.cache.del(key);
}, this.options.timeout);
}
get(key) {
if (!Tracker.currentComputation) {
let data = this.cache.get(key);
if (data == null) {
data = this.getData(key);
this.cache.set(key, data);
this.checkStop(key);
}
return data;
}
this.ensureComputation(key);
const data = this.cache.get(key);
Tracker.currentComputation.onStop(() => this.checkStop(key));
return data;
}
}
export { DataCache };
export default DataCache;