From 49f38dcbbb820eefedb213f50062bba9c1dedfb6 Mon Sep 17 00:00:00 2001 From: Conor Sinclair Date: Tue, 20 Feb 2024 18:22:05 +0000 Subject: [PATCH] added invocation tracking to cache records --- src/query.svelte.d.ts | 4 ++++ src/query.svelte.js | 8 ++++++++ 2 files changed, 12 insertions(+) diff --git a/src/query.svelte.d.ts b/src/query.svelte.d.ts index 84c4a13..ae6ae80 100644 --- a/src/query.svelte.d.ts +++ b/src/query.svelte.d.ts @@ -22,6 +22,10 @@ export type CacheObject = { * A function that will fetch the data and update the cache record */ fetcher: () => void; + /** + * A list of invocation times for the fetcher + */ + invocations: [number, number][]; }; }; diff --git a/src/query.svelte.js b/src/query.svelte.js index bac835a..b27cbc4 100644 --- a/src/query.svelte.js +++ b/src/query.svelte.js @@ -42,17 +42,24 @@ export function createQuery(fn, config = {}) { // the fetcher uses closures to re-run the same query with the same args when needed function fetcher() { + let start = Date.now(); cache[key].loading = true; cache[key].error = undefined; fn(...args) .then((result) => { + let end = Date.now(); + // this is reactive, so mutating the data will be picked up by Svelte cache[key].data = result; cache[key].loading = false; + cache[key].invocations.push([start, end]); }) .catch((error) => { + let end = Date.now(); + cache[key].error = error; cache[key].loading = false; + cache[key].invocations.push([start, end]); if (invalidateDataOnError) { cache[key].data = undefined; @@ -67,6 +74,7 @@ export function createQuery(fn, config = {}) { error: undefined, loading: true, fetcher, + invocations: [], }; // immediately trigger the fetcher