UNPKG

2.61 kBJavaScriptView Raw
1'use strict';
2
3const isObject = require('isobject/index.cjs.js');
4const createArgErrorMessageProd = require('../private/createArgErrorMessageProd');
5
6/**
7 * Cache store.
8 * @kind class
9 * @name Cache
10 * @param {object} [store={}] Initial [cache store]{@link Cache#store}. Useful for hydrating cache data from a server side render prior to the initial client side render.
11 * @example <caption>Ways to `import`.</caption>
12 * ```js
13 * import { Cache } from 'graphql-react';
14 * ```
15 *
16 * ```js
17 * import Cache from 'graphql-react/public/Cache.js';
18 * ```
19 * @example <caption>Ways to `require`.</caption>
20 * ```js
21 * const { Cache } = require('graphql-react');
22 * ```
23 *
24 * ```js
25 * const Cache = require('graphql-react/public/Cache');
26 * ```
27 * @example <caption>Construct a new instance.</caption>
28 * ```js
29 * const cache = new Cache();
30 * ```
31 */
32module.exports = class Cache extends EventTarget {
33 constructor(store = {}) {
34 super();
35
36 if (!isObject(store))
37 throw new TypeError(
38 typeof process === 'object' && process.env.NODE_ENV !== 'production'
39 ? 'Constructor argument 1 `store` must be an object.'
40 : createArgErrorMessageProd(1)
41 );
42
43 /**
44 * Store of cache [keys]{@link CacheKey} and [values]{@link CacheValue}.
45 * @kind member
46 * @name Cache#store
47 * @type {object}
48 */
49 this.store = store;
50 }
51};
52
53/**
54 * Signals that a [cache store]{@link Cache#store} entry was set. The event name
55 * starts with the [cache key]{@link CacheKey} of the set entry, followed by
56 * `/set`.
57 * @kind event
58 * @name Cache#event:set
59 * @type {CustomEvent}
60 * @prop {object} detail Event detail.
61 * @prop {CacheValue} detail.cacheValue Cache value that was set.
62 */
63
64/**
65 * Signals that a [cache store]{@link Cache#store} entry is now stale (often due
66 * to a mutation) and should probably be reloaded. The event name starts with
67 * the [cache key]{@link CacheKey} of the stale entry, followed by `/stale`.
68 * @kind event
69 * @name Cache#event:stale
70 * @type {CustomEvent}
71 */
72
73/**
74 * Signals that a [cache store]{@link Cache#store} entry will be deleted unless
75 * the event is canceled via `event.preventDefault()`. The event name starts
76 * with the [cache key]{@link CacheKey} of the entry being pruned, followed by
77 * `/prune`.
78 * @kind event
79 * @name Cache#event:prune
80 * @type {CustomEvent}
81 */
82
83/**
84 * Signals that a [cache store]{@link Cache#store} entry was deleted. The event
85 * name starts with the [cache key]{@link CacheKey} of the deleted entry,
86 * followed by `/delete`.
87 * @kind event
88 * @name Cache#event:delete
89 * @type {CustomEvent}
90 */