|
1 | | -import {useMemo} from 'react'; |
2 | | -import {useQuery} from 'react-apollo'; |
| 1 | +import {useEffect, useState} from 'react'; |
| 2 | +import {useApolloClient} from 'react-apollo'; |
3 | 3 | import {getQuery} from './useNodeInfo.gql-queries'; |
4 | | -import {useDeepCompareMemoize} from '../useDeepCompareMemo'; |
5 | 4 | import {getEncodedPermissionName} from '../../fragments/getPermissionFragment'; |
6 | 5 | import {getEncodedNodeTypeName} from '../../fragments/getIsNodeTypeFragment'; |
7 | | -import {useSchemaFields} from '../useSchemaFields'; |
| 6 | +import {SCHEMA_FIELDS_QUERY} from '../useSchemaFields/useSchemaFields.gql-queries'; |
| 7 | +import {isSubset, merge} from './useNodeInfo.utils'; |
| 8 | +import {useMemoRequest} from './useMemoRequest'; |
| 9 | +import deepEquals from 'fast-deep-equal'; |
| 10 | + |
| 11 | +let queue = []; |
| 12 | +let schemaResult; |
| 13 | +let timeout; |
| 14 | +let observedQueries = []; |
| 15 | + |
| 16 | +function scheduleQueue(client) { |
| 17 | + if (!timeout && schemaResult) { |
| 18 | + timeout = setTimeout(() => { |
| 19 | + timeoutHandler(client); |
| 20 | + clearTimeout(timeout); |
| 21 | + timeout = null; |
| 22 | + }, 0); |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +const timeoutHandler = client => { |
| 27 | + const mergedQueue = []; |
| 28 | + |
| 29 | + queue.forEach(request => { |
| 30 | + const toInsert = { |
| 31 | + variables: request.variables, |
| 32 | + queryOptions: request.queryOptions, |
| 33 | + options: request.options, |
| 34 | + originals: [request] |
| 35 | + }; |
| 36 | + |
| 37 | + const mergeable = mergedQueue.find(q => JSON.stringify(q.queryOptions) === JSON.stringify(toInsert.queryOptions) && (isSubset(q.variables, toInsert.variables) || isSubset(toInsert.variables, q.variables))); |
| 38 | + |
| 39 | + if (mergeable) { |
| 40 | + merge(mergeable, toInsert); |
| 41 | + } else { |
| 42 | + mergedQueue.push({ |
| 43 | + variables: toInsert.variables && {...toInsert.variables}, |
| 44 | + queryOptions: toInsert.queryOptions && {...toInsert.queryOptions}, |
| 45 | + options: toInsert.options && {...toInsert.options}, |
| 46 | + originals: toInsert.originals |
| 47 | + }); |
| 48 | + } |
| 49 | + }); |
| 50 | + |
| 51 | + observedQueries.forEach(obs => obs.unsubscribe()); |
| 52 | + observedQueries = []; |
| 53 | + |
| 54 | + mergedQueue.forEach(mergedRequest => { |
| 55 | + const {variables, queryOptions, options, originals} = mergedRequest; |
| 56 | + const {query, generatedVariables, skip} = getQuery(variables, schemaResult, options); |
| 57 | + if (skip) { |
| 58 | + // No query to execute |
| 59 | + originals.forEach(request => { |
| 60 | + request.setResult({ |
| 61 | + loading: false |
| 62 | + }); |
| 63 | + }); |
| 64 | + } else { |
| 65 | + const watchedQuery = client.watchQuery({query, errorPolicy: 'ignore', ...queryOptions, variables: generatedVariables}).subscribe(({data, ...others}) => { |
| 66 | + const result = getResult(data, others, options, query, generatedVariables); |
| 67 | + |
| 68 | + originals.forEach(request => { |
| 69 | + if (!deepEquals(request.result, result)) { |
| 70 | + request.result = result; |
| 71 | + request.setResult({ |
| 72 | + ...result, |
| 73 | + refetch: () => { |
| 74 | + client.refetchQueries({include: [query]}); |
| 75 | + } |
| 76 | + }); |
| 77 | + } |
| 78 | + }); |
| 79 | + }); |
| 80 | + observedQueries.push(watchedQuery); |
| 81 | + } |
| 82 | + }); |
| 83 | +}; |
8 | 84 |
|
9 | 85 | export const useNodeInfo = (variables, options, queryOptions) => { |
10 | | - let schemaResult = useSchemaFields({type: 'GqlPublicationInfo'}); |
11 | | - // Use ref to avoid infinite loop, as query object will be regenerated every time |
12 | | - const memoizedVariables = useDeepCompareMemoize(variables); |
13 | | - const memoizedOptions = useDeepCompareMemoize(options); |
| 86 | + const [result, setResult] = useState({ |
| 87 | + loading: true |
| 88 | + }); |
| 89 | + |
| 90 | + const client = useApolloClient(); |
| 91 | + |
| 92 | + if (!schemaResult) { |
| 93 | + client.query({query: SCHEMA_FIELDS_QUERY, variables: {type: 'GqlPublicationInfo'}}).then(({data}) => { |
| 94 | + schemaResult = data; |
| 95 | + scheduleQueue(client); |
| 96 | + }); |
| 97 | + } |
| 98 | + |
| 99 | + // Normalize and memoize request |
| 100 | + const [currentRequest] = useMemoRequest(variables, queryOptions, options, setResult); |
| 101 | + useEffect(() => { |
| 102 | + queue.push(currentRequest); |
| 103 | + scheduleQueue(client); |
14 | 104 |
|
15 | | - const {query, generatedVariables, skip, loading} = useMemo(() => getQuery(memoizedVariables, schemaResult, memoizedOptions), [memoizedVariables, schemaResult, memoizedOptions]); |
| 105 | + return () => { |
| 106 | + queue.splice(queue.indexOf(currentRequest), 1); |
| 107 | + }; |
| 108 | + }, [currentRequest]); |
16 | 109 |
|
17 | | - const {data, ...others} = useQuery(query, {errorPolicy: 'ignore', ...queryOptions, variables: generatedVariables, skip: (skip || loading)}); |
| 110 | + return result; |
| 111 | +}; |
18 | 112 |
|
| 113 | +const getResult = (data, others, options, query, generatedVariables) => { |
19 | 114 | const node = (data && data.jcr && (data.jcr.nodeByPath || data.jcr.nodeById)) || null; |
20 | 115 | const nodes = (data && data.jcr && (data.jcr.nodesByPath || data.jcr.nodesById)) || null; |
21 | | - |
22 | | - if (loading) { |
23 | | - return {loading}; |
24 | | - } |
| 116 | + let result = others; |
25 | 117 |
|
26 | 118 | if (node) { |
27 | | - return { |
28 | | - node: decodeResult(node, memoizedOptions), |
| 119 | + result = { |
| 120 | + node: decodeResult(node, options), |
29 | 121 | ...others, |
30 | 122 | query, |
31 | 123 | variables: generatedVariables |
32 | 124 | }; |
33 | 125 | } |
34 | 126 |
|
35 | 127 | if (nodes) { |
36 | | - return { |
37 | | - nodes: nodes.map(n => decodeResult(n, memoizedOptions)), |
| 128 | + result = { |
| 129 | + nodes: nodes.map(n => decodeResult(n, options)), |
38 | 130 | ...others, |
39 | 131 | query, |
40 | 132 | variables: generatedVariables |
41 | 133 | }; |
42 | 134 | } |
43 | 135 |
|
44 | | - return { |
45 | | - ...others |
46 | | - }; |
| 136 | + return result; |
47 | 137 | }; |
48 | 138 |
|
49 | 139 | const decodeResult = (nodeOrig, options) => { |
|
0 commit comments