-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
cacheStale.mjs
28 lines (23 loc) · 969 Bytes
/
cacheStale.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// @ts-check
/**
* @import { CacheKey } from "./Cache.mjs"
* @import { CacheKeyMatcher } from "./types.mjs"
*/
import Cache from "./Cache.mjs";
import cacheEntryStale from "./cacheEntryStale.mjs";
/**
* Stales {@link Cache.store cache store} entries by using
* {@linkcode cacheEntryStale} for each entry. Useful after a mutation.
* @param {Cache} cache Cache to update.
* @param {CacheKeyMatcher} [cacheKeyMatcher] Matches
* {@link CacheKey cache keys} to stale. By default all are matched.
*/
export default function cacheStale(cache, cacheKeyMatcher) {
if (!(cache instanceof Cache))
throw new TypeError("Argument 1 `cache` must be a `Cache` instance.");
if (cacheKeyMatcher !== undefined && typeof cacheKeyMatcher !== "function")
throw new TypeError("Argument 2 `cacheKeyMatcher` must be a function.");
for (const cacheKey in cache.store)
if (!cacheKeyMatcher || cacheKeyMatcher(cacheKey))
cacheEntryStale(cache, cacheKey);
}