Optimize calendar refresh scheduling and cancel stale loads

This commit is contained in:
Jacob Schmidt 2026-02-28 17:20:40 -06:00
parent 88d808e8b2
commit 72f8221605

View File

@ -120,8 +120,10 @@
let showSaveViewInput = false; let showSaveViewInput = false;
let saveViewName = ""; let saveViewName = "";
let hasLoadedSavedViews = false; let hasLoadedSavedViews = false;
let lastCalendarTimelineKey = "";
let calendarTimelineDebounce: ReturnType<typeof setTimeout> | null = null; let calendarTimelineDebounce: ReturnType<typeof setTimeout> | null = null;
let calendarDataVersion = 0;
let calendarScheduleKey = "";
let calendarRefreshRequestId = 0;
let lastActiveSection = ""; let lastActiveSection = "";
let calendarLastRefreshedAt = ""; let calendarLastRefreshedAt = "";
let calendarDateExplicitlySelected = false; let calendarDateExplicitlySelected = false;
@ -310,11 +312,44 @@
return tagTokens.some((token) => normalized.includes(token)); return tagTokens.some((token) => normalized.includes(token));
} }
function scheduleCalendarRefresh(delayMs = 200) {
if (activeSection !== "calendar") return;
const scheduleKey = [
selectedCalendarDate?.key ?? "",
calendarYear,
calendarMonth,
calendarViewMode,
calendarSortMode,
calendarQuery,
calendarTags,
calendarTypes,
calendarStartDate,
calendarEndDate,
calendarDataVersion,
].join("|");
if (scheduleKey === calendarScheduleKey && calendarTimelineDebounce) {
return;
}
calendarScheduleKey = scheduleKey;
if (calendarTimelineDebounce) {
clearTimeout(calendarTimelineDebounce);
}
calendarTimelineDebounce = setTimeout(() => {
void refreshCalendarTimeline();
}, delayMs);
}
async function refreshCalendarTimeline(): Promise<void> { async function refreshCalendarTimeline(): Promise<void> {
const requestId = ++calendarRefreshRequestId;
calendarBusy = true; calendarBusy = true;
calendarError = ""; calendarError = "";
try { try {
const dataDirectory = await getDataDirectory(); const dataDirectory = await getDataDirectory();
if (requestId !== calendarRefreshRequestId) return;
if (!dataDirectory) { if (!dataDirectory) {
calendarTimelineItems = []; calendarTimelineItems = [];
return; return;
@ -325,12 +360,14 @@
dataDirectory, dataDirectory,
query: calendarQuery.trim() || undefined, query: calendarQuery.trim() || undefined,
}); });
if (requestId !== calendarRefreshRequestId) return;
const [fragmentDtos, listDtos, todoDtos] = await Promise.all([ const [fragmentDtos, listDtos, todoDtos] = await Promise.all([
listFragments(), listFragments(),
listLists(), listLists(),
listTodoLists(), listTodoLists(),
]); ]);
if (requestId !== calendarRefreshRequestId) return;
const query = calendarQuery.trim(); const query = calendarQuery.trim();
const tagTokens = splitFilterTokens(calendarTags); const tagTokens = splitFilterTokens(calendarTags);
@ -425,10 +462,13 @@
second: "2-digit", second: "2-digit",
}); });
} catch (error) { } catch (error) {
if (requestId !== calendarRefreshRequestId) return;
calendarError = String(error); calendarError = String(error);
calendarTimelineItems = []; calendarTimelineItems = [];
} finally { } finally {
calendarBusy = false; if (requestId === calendarRefreshRequestId) {
calendarBusy = false;
}
} }
} }
@ -818,53 +858,45 @@
loadSavedViews(); loadSavedViews();
} }
$: calendarTimelineRefreshKey = JSON.stringify({ $: if (activeSection === "calendar") {
activeSection, // Track store mutations without building large string signatures.
calendarYear, const storeInvalidationTick = [
calendarMonth, $entriesStore,
selectedCalendarDate, $fragmentsStore,
calendarViewMode, $listsStore,
calendarSortMode, $todoListsStore,
calendarQuery, $todosStore,
calendarTags, ];
calendarTypes, void storeInvalidationTick;
calendarStartDate, calendarDataVersion += 1;
calendarEndDate, }
entriesSig: $entriesStore
.map((item) => `${item.id}:${item.label}`) $: if (activeSection === "calendar") {
.join("|"), const calendarTrigger = [
fragmentsSig: $fragmentsStore selectedCalendarDate?.key ?? "",
.map((item) => `${item.id}:${item.label}`) calendarYear,
.join("|"), calendarMonth,
listsSig: $listsStore.map((item) => `${item.id}:${item.label}`).join("|"), calendarViewMode,
todosSig: $todoListsStore calendarSortMode,
.map((item) => { calendarQuery,
const todos = ($todosStore[item.id] ?? []) calendarTags,
.map((todo) => `${todo.text}:${todo.done ? "1" : "0"}`) calendarTypes,
.join("~"); calendarStartDate,
return `${item.id}:${item.label}:${todos}`; calendarEndDate,
}) calendarDataVersion,
.join("|"), ];
}); void calendarTrigger;
$: if ( scheduleCalendarRefresh(200);
activeSection === "calendar" &&
calendarTimelineRefreshKey !== lastCalendarTimelineKey
) {
lastCalendarTimelineKey = calendarTimelineRefreshKey;
if (calendarTimelineDebounce) {
clearTimeout(calendarTimelineDebounce);
}
calendarTimelineDebounce = setTimeout(() => {
void refreshCalendarTimeline();
}, 200);
} }
$: if (activeSection === "calendar" && lastActiveSection !== "calendar") { $: if (activeSection === "calendar" && lastActiveSection !== "calendar") {
calendarDateExplicitlySelected = false; calendarDateExplicitlySelected = false;
calendarScheduleKey = "";
void forceRefreshCalendar({ allowWhileBusy: true }); void forceRefreshCalendar({ allowWhileBusy: true });
setTimeout(() => { }
void forceRefreshCalendar({ allowWhileBusy: true }); $: if (activeSection !== "calendar" && calendarTimelineDebounce) {
}, 500); clearTimeout(calendarTimelineDebounce);
calendarTimelineDebounce = null;
} }
$: lastActiveSection = activeSection; $: lastActiveSection = activeSection;