-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
prevent load functions from rerunning excessively (#1115)
* prevent load functions from re-running excessively * clarifying docs * fix checks
- Loading branch information
Showing
14 changed files
with
160 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
src/lib/stores/fetched-data-cache/fetched-data-cache.store.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { get, writable } from 'svelte/store'; | ||
|
||
interface State { | ||
data: Record<string, unknown>; | ||
} | ||
|
||
const state = writable<State>({ data: {} }); | ||
|
||
function set(key: string, value: unknown) { | ||
state.update((s) => { | ||
s.data[key] = value; | ||
return s; | ||
}); | ||
} | ||
|
||
export function clear(key: string) { | ||
state.update((s) => { | ||
delete s.data[key]; | ||
return s; | ||
}); | ||
} | ||
|
||
export function clearAll() { | ||
state.set({ data: {} }); | ||
} | ||
|
||
/** | ||
* Create a local "fetched data cache" that can be used to prevent data from being re-fetched | ||
* on navigations to a page that has already previously been navigated to. | ||
* | ||
* **Important**: There is no magic for removing data from this store. If data of a page that uses | ||
* this cache should be re-fetched, ensure that `invalidate` functions from `./invalidate.ts` are | ||
* called with the appropriate keys. | ||
* | ||
* **Also important**: No runtime type checking is being done. You need to make sure that | ||
* 1. The key you pass to this function is unique | ||
* 2. The type you pass to this function is correct | ||
* | ||
* **Also important**: What this returns is only a wrapper around a global store. Even if this | ||
* wrapper is abandoned, the values it wrote persist in the global store. | ||
* @param key | ||
* @returns | ||
*/ | ||
export const makeFetchedDataCache = <T extends Record<string, unknown> | unknown[]>( | ||
key: string, | ||
) => { | ||
function read(): T | undefined { | ||
return get(state).data[key] as T; | ||
} | ||
|
||
function write(v: T) { | ||
set(key, v); | ||
} | ||
|
||
return { | ||
subscribe: state.subscribe, | ||
read, | ||
write, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { invalidateAll as skInvalidateAll, invalidate as skInvalidate } from '$app/navigation'; | ||
import { clear, clearAll } from './fetched-data-cache.store'; | ||
|
||
/** | ||
* Invalidate `key` in the local fetched data cache and re-run all load functions | ||
* that depend on `key`. | ||
*/ | ||
export async function invalidate(key: string) { | ||
clear(key); | ||
await skInvalidate(key); | ||
} | ||
|
||
/** | ||
* Invalidate all keys in the local fetched data cache and re-run all currently active load functions. | ||
*/ | ||
export async function invalidateAll() { | ||
clearAll(); | ||
await skInvalidateAll(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters