Drag and drop between columns, cards carrying an assignee, a due date, labels and a linked issue whose state shows on the card. An overloaded column says so in its header rather than blocking the drop. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
170 lines
4.4 KiB
JavaScript
170 lines
4.4 KiB
JavaScript
/**
|
|
* Live updates (M16-T12).
|
|
*
|
|
* One `EventSource` for the whole dashboard; views declare what they want to
|
|
* hear and get called back. When the stream cannot be established — a proxy
|
|
* that buffers, a network that drops it — a timer takes over so the pages still
|
|
* refresh, just less promptly.
|
|
*/
|
|
|
|
/**
|
|
* A single slot rather than a list: exactly one view is on screen, and a view
|
|
* that re-renders itself on refresh would otherwise register a second listener
|
|
* each time, doubling the work on every event.
|
|
*/
|
|
let viewRefresher = null;
|
|
const STATUS_LISTENERS = new Set();
|
|
const FALLBACK_MS = 30_000;
|
|
/** Backoff between reconnection attempts, capped so it always keeps trying. */
|
|
const RETRY_MS = [2000, 5000, 15_000, 30_000];
|
|
|
|
let source = null;
|
|
let fallbackTimer = null;
|
|
let attempts = 0;
|
|
let status = 'idle';
|
|
let lastEventAt = null;
|
|
|
|
function setStatus(next) {
|
|
if (status === next) return;
|
|
status = next;
|
|
for (const listener of STATUS_LISTENERS) listener({ status, lastEventAt });
|
|
}
|
|
|
|
function emit(event) {
|
|
lastEventAt = new Date();
|
|
try {
|
|
viewRefresher?.(event);
|
|
} catch {
|
|
// A failing view must not take the stream down with it.
|
|
}
|
|
setStatus(fallbackTimer ? 'polling' : 'live');
|
|
}
|
|
|
|
function startFallback() {
|
|
if (fallbackTimer) return;
|
|
fallbackTimer = setInterval(() => emit({ topic: 'tick', fallback: true }), FALLBACK_MS);
|
|
setStatus('polling');
|
|
}
|
|
|
|
function stopFallback() {
|
|
clearInterval(fallbackTimer);
|
|
fallbackTimer = null;
|
|
}
|
|
|
|
function connect() {
|
|
if (source || document.hidden) return;
|
|
|
|
setStatus(attempts === 0 ? 'connecting' : 'reconnecting');
|
|
source = new EventSource('/api/events', { withCredentials: true });
|
|
|
|
source.addEventListener('open', () => {
|
|
attempts = 0;
|
|
stopFallback();
|
|
setStatus('live');
|
|
});
|
|
|
|
for (const topic of [
|
|
'ready',
|
|
'tick',
|
|
'mutation',
|
|
'deployments',
|
|
'gitea',
|
|
'discord',
|
|
'community',
|
|
'projects',
|
|
'settings',
|
|
]) {
|
|
source.addEventListener(topic, (event) => {
|
|
let payload = { topic };
|
|
try {
|
|
payload = { ...JSON.parse(event.data), topic };
|
|
} catch {
|
|
// A frame we cannot read is still a signal that something moved.
|
|
}
|
|
emit(payload);
|
|
});
|
|
}
|
|
|
|
source.addEventListener('error', () => {
|
|
source?.close();
|
|
source = null;
|
|
|
|
// The browser reconnects on its own, but only while the page stays open and
|
|
// the server keeps answering; the timer covers the rest.
|
|
startFallback();
|
|
const delay = RETRY_MS[Math.min(attempts, RETRY_MS.length - 1)];
|
|
attempts += 1;
|
|
setTimeout(connect, delay);
|
|
});
|
|
}
|
|
|
|
/** Stops the stream while the tab is in the background, resumes on return. */
|
|
document.addEventListener('visibilitychange', () => {
|
|
if (document.hidden) {
|
|
source?.close();
|
|
source = null;
|
|
stopFallback();
|
|
setStatus('paused');
|
|
} else {
|
|
connect();
|
|
// Coming back to the tab, the page is stale by definition.
|
|
emit({ topic: 'tick', resumed: true });
|
|
}
|
|
});
|
|
|
|
export function startLive() {
|
|
connect();
|
|
}
|
|
|
|
export function onStatus(listener) {
|
|
STATUS_LISTENERS.add(listener);
|
|
listener({ status, lastEventAt });
|
|
return () => STATUS_LISTENERS.delete(listener);
|
|
}
|
|
|
|
export const liveStatus = () => ({ status, lastEventAt });
|
|
|
|
/** Drops the current view subscription, called when navigating elsewhere. */
|
|
export function resetSubscriptions() {
|
|
viewRefresher = null;
|
|
}
|
|
|
|
/**
|
|
* Re-runs `refresh` when a relevant event arrives.
|
|
*
|
|
* Refreshing under someone's fingers is worse than showing slightly old data,
|
|
* so a refresh is skipped while a dialog is open or a field has focus, and
|
|
* replayed once the way is clear.
|
|
*/
|
|
export function autoRefresh(refresh, { topics = ['tick'], minIntervalMs = 5000 } = {}) {
|
|
const wanted = new Set(topics);
|
|
let lastRun = Date.now();
|
|
let pending = false;
|
|
|
|
const busy = () =>
|
|
document.querySelector('.overlay') !== null ||
|
|
['INPUT', 'TEXTAREA', 'SELECT'].includes(document.activeElement?.tagName);
|
|
|
|
const run = () => {
|
|
if (busy()) {
|
|
pending = true;
|
|
return;
|
|
}
|
|
pending = false;
|
|
lastRun = Date.now();
|
|
refresh();
|
|
};
|
|
|
|
const listener = (event) => {
|
|
if (!wanted.has(event.topic) && !pending) return;
|
|
// A burst of writes must not turn into a burst of reloads.
|
|
if (Date.now() - lastRun < minIntervalMs && !pending) return;
|
|
run();
|
|
};
|
|
|
|
viewRefresher = listener;
|
|
return () => {
|
|
if (viewRefresher === listener) viewRefresher = null;
|
|
};
|
|
}
|