|
| 1 | +// @ts-check |
| 2 | + |
1 | 3 | /**
|
2 | 4 | * Cache store.
|
3 |
| - * @kind class |
4 |
| - * @name Cache |
5 |
| - * @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. |
6 |
| - * @example <caption>How to import.</caption> |
7 |
| - * ```js |
8 |
| - * import Cache from "graphql-react/Cache.mjs"; |
9 |
| - * ``` |
10 |
| - * @example <caption>Construct a new instance.</caption> |
11 |
| - * ```js |
12 |
| - * const cache = new Cache(); |
13 |
| - * ``` |
| 5 | + * @see {@link CacheEventMap `CacheEventMap`} for a map of possible events. |
14 | 6 | */
|
15 | 7 | export default class Cache extends EventTarget {
|
| 8 | + /** |
| 9 | + * @param {CacheStore} [store] Initial {@link Cache.store cache store} record. |
| 10 | + * Defaults to `{}`. Useful for hydrating cache data from a server side |
| 11 | + * render prior to the initial client side render. |
| 12 | + */ |
16 | 13 | constructor(store = {}) {
|
17 | 14 | super();
|
18 | 15 |
|
19 | 16 | if (typeof store !== "object" || !store || Array.isArray(store))
|
20 | 17 | throw new TypeError("Constructor argument 1 `store` must be an object.");
|
21 | 18 |
|
22 | 19 | /**
|
23 |
| - * Store of cache [keys]{@link CacheKey} and [values]{@link CacheValue}. |
24 |
| - * @kind member |
25 |
| - * @name Cache#store |
26 |
| - * @type {object} |
| 20 | + * Store of cache {@link CacheKey keys} and associated |
| 21 | + * {@link CacheValue values}. |
| 22 | + * @type {CacheStore} |
27 | 23 | */
|
28 | 24 | this.store = store;
|
29 | 25 | }
|
30 | 26 | }
|
31 | 27 |
|
32 | 28 | /**
|
33 |
| - * Signals that a [cache store]{@link Cache#store} entry was set. The event name |
34 |
| - * starts with the [cache key]{@link CacheKey} of the set entry, followed by |
35 |
| - * `/set`. |
36 |
| - * @kind event |
37 |
| - * @name Cache#event:set |
38 |
| - * @type {CustomEvent} |
39 |
| - * @prop {object} detail Event detail. |
40 |
| - * @prop {CacheValue} detail.cacheValue Cache value that was set. |
| 29 | + * Map of possible {@linkcode Cache} events. Note that the keys don’t match the |
| 30 | + * dispatched event names that dynamically contain the associated |
| 31 | + * {@link CacheKey cache key}. |
| 32 | + * @typedef {object} CacheEventMap |
| 33 | + * @prop {CustomEvent<CacheEventSetDetail>} set Signals that a |
| 34 | + * {@link Cache.store cache store} entry was set. The event name starts with |
| 35 | + * the {@link CacheKey cache key} of the set entry, followed by `/set`. |
| 36 | + * @prop {CustomEvent} stale Signals that a {@link Cache.store cache store} |
| 37 | + * entry is now stale (often due to a mutation) and should probably be |
| 38 | + * reloaded. The event name starts with the |
| 39 | + * {@link CacheKey cache key} of the stale entry, followed by `/stale`. |
| 40 | + * @prop {CustomEvent} prune Signals that a {@link Cache.store cache store} |
| 41 | + * entry will be deleted unless the event is canceled via |
| 42 | + * `event.preventDefault()`. The event name starts with the |
| 43 | + * {@link CacheKey cache key} of the entry being pruned, followed by `/prune`. |
| 44 | + * @prop {CustomEvent} delete Signals that a {@link Cache.store cache store} |
| 45 | + * entry was deleted. The event name starts with the |
| 46 | + * {@link CacheKey cache key} of the deleted entry, followed by `/delete`. |
| 47 | + */ |
| 48 | + |
| 49 | +/** |
| 50 | + * @typedef {object} CacheEventSetDetail |
| 51 | + * @prop {CacheValue} cacheValue The set {@link CacheValue cache value}. |
41 | 52 | */
|
42 | 53 |
|
43 | 54 | /**
|
44 |
| - * Signals that a [cache store]{@link Cache#store} entry is now stale (often due |
45 |
| - * to a mutation) and should probably be reloaded. The event name starts with |
46 |
| - * the [cache key]{@link CacheKey} of the stale entry, followed by `/stale`. |
47 |
| - * @kind event |
48 |
| - * @name Cache#event:stale |
49 |
| - * @type {CustomEvent} |
| 55 | + * A unique key to access a {@link CacheValue cache value}. |
| 56 | + * @typedef {string} CacheKey |
50 | 57 | */
|
51 | 58 |
|
52 | 59 | /**
|
53 |
| - * Signals that a [cache store]{@link Cache#store} entry will be deleted unless |
54 |
| - * the event is canceled via `event.preventDefault()`. The event name starts |
55 |
| - * with the [cache key]{@link CacheKey} of the entry being pruned, followed by |
56 |
| - * `/prune`. |
57 |
| - * @kind event |
58 |
| - * @name Cache#event:prune |
59 |
| - * @type {CustomEvent} |
| 60 | + * A {@link Cache.store cache store} value. If server side rendering, it should |
| 61 | + * be JSON serializable for client hydration. It should contain information |
| 62 | + * about any errors that occurred during loading so they can be rendered, and if |
| 63 | + * server side rendering, be hydrated on the client. |
| 64 | + * @typedef {unknown} CacheValue |
60 | 65 | */
|
61 | 66 |
|
62 | 67 | /**
|
63 |
| - * Signals that a [cache store]{@link Cache#store} entry was deleted. The event |
64 |
| - * name starts with the [cache key]{@link CacheKey} of the deleted entry, |
65 |
| - * followed by `/delete`. |
66 |
| - * @kind event |
67 |
| - * @name Cache#event:delete |
68 |
| - * @type {CustomEvent} |
| 68 | + * Cache store record. |
| 69 | + * @typedef {Record<CacheKey, CacheValue>} CacheStore |
69 | 70 | */
|
0 commit comments