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 useLoading = require('./useLoading');
|
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 useLoadingEntry(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 loading = useLoading();
|
41 | const forceUpdate = useForceUpdate();
|
42 |
|
43 | const onTriggerUpdate = useCallback(() => {
|
44 | forceUpdate();
|
45 | }, [forceUpdate]);
|
46 |
|
47 | useEffect(() => {
|
48 | const eventNameStart = `${cacheKey}/start`;
|
49 | const eventNameEnd = `${cacheKey}/end`;
|
50 |
|
51 | loading.addEventListener(eventNameStart, onTriggerUpdate);
|
52 | loading.addEventListener(eventNameEnd, onTriggerUpdate);
|
53 |
|
54 | return () => {
|
55 | loading.removeEventListener(eventNameStart, onTriggerUpdate);
|
56 | loading.removeEventListener(eventNameEnd, onTriggerUpdate);
|
57 | };
|
58 | }, [loading, cacheKey, onTriggerUpdate]);
|
59 |
|
60 | const value = loading.store[cacheKey];
|
61 |
|
62 | if (typeof process === 'object' && process.env.NODE_ENV !== 'production')
|
63 |
|
64 | useDebugValue(value);
|
65 |
|
66 | return value;
|
67 | };
|