UNPKG

1.52 kBJavaScriptView Raw
1'use strict';
2
3const createArgErrorMessageProd = require('../private/createArgErrorMessageProd');
4const Cache = require('./Cache');
5
6/**
7 * Stales a [cache]{@link Cache#store} entry, signalling it should probably be
8 * reloaded.
9 * @kind function
10 * @name cacheEntryStale
11 * @param {Cache} cache Cache to update.
12 * @param {CacheKey} cacheKey Cache key.
13 * @fires Cache#event:stale
14 * @example <caption>Ways to `import`.</caption>
15 * ```js
16 * import { cacheEntryStale } from 'graphql-react';
17 * ```
18 *
19 * ```js
20 * import cacheEntryStale from 'graphql-react/public/cacheEntryStale.js';
21 * ```
22 * @example <caption>Ways to `require`.</caption>
23 * ```js
24 * const { cacheEntryStale } = require('graphql-react');
25 * ```
26 *
27 * ```js
28 * const cacheEntryStale = require('graphql-react/public/cacheEntryStale');
29 * ```
30 */
31module.exports = function cacheEntryStale(cache, cacheKey) {
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 (typeof cacheKey !== 'string')
40 throw new TypeError(
41 typeof process === 'object' && process.env.NODE_ENV !== 'production'
42 ? 'Argument 2 `cacheKey` must be a string.'
43 : createArgErrorMessageProd(2)
44 );
45
46 if (!(cacheKey in cache.store))
47 throw new Error(`Cache key \`${cacheKey}\` isn’t in the store.`);
48
49 cache.dispatchEvent(new CustomEvent(`${cacheKey}/stale`));
50};