UNPKG

1.5 kBJavaScriptView Raw
1'use strict';
2
3const createArgErrorMessageProd = require('../private/createArgErrorMessageProd');
4const Cache = require('./Cache');
5
6/**
7 * Sets a [cache]{@link Cache#store} entry.
8 * @kind function
9 * @name cacheEntrySet
10 * @param {Cache} cache Cache to update.
11 * @param {CacheKey} cacheKey Cache key.
12 * @param {CacheValue} cacheValue Cache value.
13 * @fires Cache#event:set
14 * @example <caption>Ways to `import`.</caption>
15 * ```js
16 * import { cacheEntrySet } from 'graphql-react';
17 * ```
18 *
19 * ```js
20 * import cacheEntrySet from 'graphql-react/public/cacheEntrySet.js';
21 * ```
22 * @example <caption>Ways to `require`.</caption>
23 * ```js
24 * const { cacheEntrySet } = require('graphql-react');
25 * ```
26 *
27 * ```js
28 * const cacheEntrySet = require('graphql-react/public/cacheEntrySet');
29 * ```
30 */
31module.exports = function cacheEntrySet(cache, cacheKey, cacheValue) {
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 cache.store[cacheKey] = cacheValue;
47
48 cache.dispatchEvent(
49 new CustomEvent(`${cacheKey}/set`, {
50 detail: {
51 cacheValue,
52 },
53 })
54 );
55};