1 | ;
|
2 |
|
3 | const createArgErrorMessageProd = require('../private/createArgErrorMessageProd');
|
4 | const Cache = require('./Cache');
|
5 | const cacheEntryDelete = require('./cacheEntryDelete');
|
6 |
|
7 | /**
|
8 | * Prunes a [cache]{@link Cache#store} entry, if no
|
9 | * [prune event]{@link Cache#event:prune} listener cancels the
|
10 | * [cache]{@link Cache#store} entry deletion via `event.preventDefault()`.
|
11 | * @kind function
|
12 | * @name cacheEntryPrune
|
13 | * @param {Cache} cache Cache to update.
|
14 | * @param {CacheKey} cacheKey Cache key.
|
15 | * @fires Cache#event:prune
|
16 | * @fires Cache#event:delete
|
17 | * @example <caption>Ways to `import`.</caption>
|
18 | * ```js
|
19 | * import { cacheEntryPrune } from 'graphql-react';
|
20 | * ```
|
21 | *
|
22 | * ```js
|
23 | * import cacheEntryPrune from 'graphql-react/public/cacheEntryPrune.js';
|
24 | * ```
|
25 | * @example <caption>Ways to `require`.</caption>
|
26 | * ```js
|
27 | * const { cacheEntryPrune } = require('graphql-react');
|
28 | * ```
|
29 | *
|
30 | * ```js
|
31 | * const cacheEntryPrune = require('graphql-react/public/cacheEntryPrune');
|
32 | * ```
|
33 | */
|
34 | module.exports = function cacheEntryPrune(cache, cacheKey) {
|
35 | if (!(cache instanceof Cache))
|
36 | throw new TypeError(
|
37 | typeof process === 'object' && process.env.NODE_ENV !== 'production'
|
38 | ? 'Argument 1 `cache` must be a `Cache` instance.'
|
39 | : createArgErrorMessageProd(1)
|
40 | );
|
41 |
|
42 | if (typeof cacheKey !== 'string')
|
43 | throw new TypeError(
|
44 | typeof process === 'object' && process.env.NODE_ENV !== 'production'
|
45 | ? 'Argument 2 `cacheKey` must be a string.'
|
46 | : createArgErrorMessageProd(2)
|
47 | );
|
48 |
|
49 | if (cacheKey in cache.store) {
|
50 | const notCanceled = cache.dispatchEvent(
|
51 | new CustomEvent(`${cacheKey}/prune`, { cancelable: true })
|
52 | );
|
53 |
|
54 | if (notCanceled) cacheEntryDelete(cache, cacheKey);
|
55 | }
|
56 | };
|