-
-
Notifications
You must be signed in to change notification settings - Fork 27
/
cacheEntryStale.mjs
25 lines (19 loc) · 880 Bytes
/
cacheEntryStale.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
// @ts-check
import Cache from "./Cache.mjs";
/** @typedef {import("./Cache.mjs").CacheEventMap} CacheEventMap */
/**
* Stales a {@link Cache.store cache store} entry (throwing an error if missing)
* by dispatching the {@linkcode Cache} event
* {@link CacheEventMap.stale `stale`} to signal it should probably be reloaded.
* @param {Cache} cache Cache to update.
* @param {import("./Cache.mjs").CacheKey} cacheKey Cache key.
*/
export default function cacheEntryStale(cache, cacheKey) {
if (!(cache instanceof Cache))
throw new TypeError("Argument 1 `cache` must be a `Cache` instance.");
if (typeof cacheKey !== "string")
throw new TypeError("Argument 2 `cacheKey` must be a string.");
if (!(cacheKey in cache.store))
throw new Error(`Cache key \`${cacheKey}\` isn’t in the store.`);
cache.dispatchEvent(new CustomEvent(`${cacheKey}/stale`));
}