UNPKG

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