1 | 'use strict';
|
2 |
|
3 | const { useCallback, useDebugValue, useEffect } = require('react');
|
4 | const createArgErrorMessageProd = require('../private/createArgErrorMessageProd');
|
5 | const useForceUpdate = require('../private/useForceUpdate');
|
6 | const useCache = require('./useCache');
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 | module.exports = function useCacheEntry(cacheKey) {
|
33 | if (typeof cacheKey !== 'string')
|
34 | throw new TypeError(
|
35 | typeof process === 'object' && process.env.NODE_ENV !== 'production'
|
36 | ? 'Argument 1 `cacheKey` must be a string.'
|
37 | : createArgErrorMessageProd(1)
|
38 | );
|
39 |
|
40 | const cache = useCache();
|
41 | const forceUpdate = useForceUpdate();
|
42 |
|
43 | const onTriggerUpdate = useCallback(() => {
|
44 | forceUpdate();
|
45 | }, [forceUpdate]);
|
46 |
|
47 | useEffect(() => {
|
48 | const eventNameSet = `${cacheKey}/set`;
|
49 | const eventNameDelete = `${cacheKey}/delete`;
|
50 |
|
51 | cache.addEventListener(eventNameSet, onTriggerUpdate);
|
52 | cache.addEventListener(eventNameDelete, onTriggerUpdate);
|
53 |
|
54 | return () => {
|
55 | cache.removeEventListener(eventNameSet, onTriggerUpdate);
|
56 | cache.removeEventListener(eventNameDelete, onTriggerUpdate);
|
57 | };
|
58 | }, [cache, cacheKey, onTriggerUpdate]);
|
59 |
|
60 | const value = cache.store[cacheKey];
|
61 |
|
62 | if (typeof process === 'object' && process.env.NODE_ENV !== 'production')
|
63 |
|
64 | useDebugValue(value);
|
65 |
|
66 | return value;
|
67 | };
|