-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathcacheEntryPrune.mjs
30 lines (24 loc) · 990 Bytes
/
cacheEntryPrune.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
29
30
// @ts-check
/** @import { CacheEventMap, CacheKey } from "./Cache.mjs" */
import Cache from "./Cache.mjs";
import cacheEntryDelete from "./cacheEntryDelete.mjs";
/**
* Prunes a {@link Cache.store cache store} entry (if present) by dispatching
* the {@linkcode Cache} event {@link CacheEventMap.prune `prune`} and if no
* listener cancels it via `event.preventDefault()`, using
* {@linkcode cacheEntryDelete}.
* @param {Cache} cache Cache to update.
* @param {CacheKey} cacheKey Cache key.
*/
export default function cacheEntryPrune(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) {
const notCanceled = cache.dispatchEvent(
new CustomEvent(`${cacheKey}/prune`, { cancelable: true }),
);
if (notCanceled) cacheEntryDelete(cache, cacheKey);
}
}