UNPKG

1.62 kBJavaScriptView Raw
1'use strict';
2
3const createArgErrorMessageProd = require('../private/createArgErrorMessageProd');
4const Cache = require('./Cache');
5const cacheEntryStale = require('./cacheEntryStale');
6
7/**
8 * Stales [cache]{@link Cache#store} entries. Useful after a mutation.
9 * @kind function
10 * @name cacheStale
11 * @param {Cache} cache Cache to update.
12 * @param {CacheKeyMatcher} [cacheKeyMatcher] Matches [cache keys]{@link CacheKey} to stale. By default all are matched.
13 * @fires Cache#event:stale
14 * @example <caption>Ways to `import`.</caption>
15 * ```js
16 * import { cacheStale } from 'graphql-react';
17 * ```
18 *
19 * ```js
20 * import cacheStale from 'graphql-react/public/cacheStale.js';
21 * ```
22 * @example <caption>Ways to `require`.</caption>
23 * ```js
24 * const { cacheStale } = require('graphql-react');
25 * ```
26 *
27 * ```js
28 * const cacheStale = require('graphql-react/public/cacheStale');
29 * ```
30 */
31module.exports = function cacheStale(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 cacheEntryStale(cache, cacheKey);
49};