UNPKG

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