react-relay 14.1.0 → 16.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (196) hide show
  1. package/ReactRelayContainerUtils.js.flow +1 -0
  2. package/ReactRelayContext.js +1 -1
  3. package/ReactRelayContext.js.flow +1 -0
  4. package/ReactRelayFragmentContainer.js.flow +6 -2
  5. package/ReactRelayFragmentMockRenderer.js.flow +1 -0
  6. package/ReactRelayLocalQueryRenderer.js.flow +5 -3
  7. package/ReactRelayPaginationContainer.js.flow +21 -12
  8. package/ReactRelayQueryFetcher.js.flow +20 -10
  9. package/ReactRelayQueryRenderer.js.flow +15 -11
  10. package/ReactRelayQueryRendererContext.js.flow +1 -0
  11. package/ReactRelayRefetchContainer.js.flow +9 -5
  12. package/ReactRelayTestMocker.js.flow +3 -1
  13. package/ReactRelayTypes.js.flow +2 -0
  14. package/RelayContext.js.flow +1 -0
  15. package/__flowtests__/ReactRelayFragmentContainer-flowtest.js.flow +2 -1
  16. package/__flowtests__/ReactRelayPaginationContainer-flowtest.js.flow +1 -0
  17. package/__flowtests__/ReactRelayRefetchContainer-flowtest.js.flow +1 -0
  18. package/__flowtests__/RelayModern-flowtest.js.flow +1 -0
  19. package/__flowtests__/RelayModernFlowtest_badref.graphql.js.flow +1 -0
  20. package/__flowtests__/RelayModernFlowtest_notref.graphql.js.flow +1 -0
  21. package/__flowtests__/RelayModernFlowtest_user.graphql.js.flow +1 -0
  22. package/__flowtests__/RelayModernFlowtest_users.graphql.js.flow +1 -0
  23. package/__flowtests__/__generated__/ReactRelayFragmentContainerFlowtest_viewer.graphql.js.flow +3 -1
  24. package/__flowtests__/__generated__/ReactRelayFragmentContainerFlowtest_viewer2.graphql.js.flow +3 -1
  25. package/__flowtests__/__generated__/ReactRelayPaginationContainerFlowtestQuery.graphql.js.flow +4 -2
  26. package/__flowtests__/__generated__/ReactRelayPaginationContainerFlowtest_viewer.graphql.js.flow +3 -1
  27. package/__flowtests__/__generated__/ReactRelayRefetchContainerFlowtestQuery.graphql.js.flow +4 -2
  28. package/__flowtests__/__generated__/ReactRelayRefetchContainerFlowtest_viewer.graphql.js.flow +3 -1
  29. package/__flowtests__/__generated__/RelayModernFlowtest_badref.graphql.js.flow +4 -2
  30. package/__flowtests__/__generated__/RelayModernFlowtest_notref.graphql.js.flow +4 -2
  31. package/__flowtests__/__generated__/RelayModernFlowtest_user.graphql.js.flow +3 -1
  32. package/__flowtests__/__generated__/RelayModernFlowtest_users.graphql.js.flow +3 -1
  33. package/assertFragmentMap.js.flow +1 -0
  34. package/buildReactRelayContainer.js.flow +10 -6
  35. package/getRootVariablesForFragments.js.flow +1 -1
  36. package/hooks.js +1 -1
  37. package/hooks.js.flow +4 -0
  38. package/index.js +1 -1
  39. package/index.js.flow +4 -0
  40. package/isRelayEnvironment.js.flow +1 -0
  41. package/jest-react/enqueueTask.js.flow +1 -1
  42. package/jest-react/index.js.flow +1 -1
  43. package/jest-react/internalAct.js.flow +1 -1
  44. package/legacy.js +1 -1
  45. package/legacy.js.flow +1 -0
  46. package/lib/ReactRelayContainerUtils.js +0 -11
  47. package/lib/ReactRelayContext.js +1 -12
  48. package/lib/ReactRelayFragmentContainer.js +23 -122
  49. package/lib/ReactRelayFragmentMockRenderer.js +0 -12
  50. package/lib/ReactRelayLocalQueryRenderer.js +12 -41
  51. package/lib/ReactRelayPaginationContainer.js +45 -341
  52. package/lib/ReactRelayQueryFetcher.js +36 -111
  53. package/lib/ReactRelayQueryRenderer.js +29 -137
  54. package/lib/ReactRelayQueryRendererContext.js +0 -10
  55. package/lib/ReactRelayRefetchContainer.js +33 -166
  56. package/lib/ReactRelayTestMocker.js +18 -128
  57. package/lib/ReactRelayTypes.js +0 -9
  58. package/lib/RelayContext.js +0 -23
  59. package/lib/assertFragmentMap.js +0 -16
  60. package/lib/buildReactRelayContainer.js +7 -41
  61. package/lib/getRootVariablesForFragments.js +2 -19
  62. package/lib/hooks.js +3 -30
  63. package/lib/index.js +3 -39
  64. package/lib/isRelayEnvironment.js +1 -16
  65. package/lib/jest-react/enqueueTask.js +1 -25
  66. package/lib/jest-react/index.js +0 -1
  67. package/lib/jest-react/internalAct.js +2 -51
  68. package/lib/legacy.js +0 -20
  69. package/lib/multi-actor/ActorChange.js +0 -14
  70. package/lib/multi-actor/index.js +0 -10
  71. package/lib/multi-actor/useRelayActorEnvironment.js +2 -16
  72. package/lib/relay-hooks/EntryPointContainer.react.js +7 -23
  73. package/lib/relay-hooks/EntryPointTypes.flow.js +0 -10
  74. package/lib/relay-hooks/FragmentResource.js +130 -280
  75. package/lib/relay-hooks/HooksImplementation.js +0 -14
  76. package/lib/relay-hooks/InternalLogger.js +0 -11
  77. package/lib/relay-hooks/LRUCache.js +0 -39
  78. package/lib/relay-hooks/LazyLoadEntryPointContainer_DEPRECATED.react.js +27 -65
  79. package/lib/relay-hooks/MatchContainer.js +9 -111
  80. package/lib/relay-hooks/NestedRelayEntryPointBuilderUtils.js +9 -0
  81. package/lib/relay-hooks/ProfilerContext.js +0 -14
  82. package/lib/relay-hooks/QueryResource.js +14 -149
  83. package/lib/relay-hooks/RelayEnvironmentProvider.js +3 -17
  84. package/lib/relay-hooks/SuspenseResource.js +2 -59
  85. package/lib/relay-hooks/loadEntryPoint.js +10 -45
  86. package/lib/relay-hooks/loadQuery.js +29 -169
  87. package/lib/relay-hooks/preloadQuery_DEPRECATED.js +8 -58
  88. package/lib/relay-hooks/prepareEntryPoint_DEPRECATED.js +6 -24
  89. package/lib/relay-hooks/react-cache/RelayReactCache.js +4 -20
  90. package/lib/relay-hooks/react-cache/getQueryResultOrFetchQuery_REACT_CACHE.js +13 -102
  91. package/lib/relay-hooks/react-cache/readFragmentInternal_REACT_CACHE.js +18 -75
  92. package/lib/relay-hooks/react-cache/useFragmentInternal_REACT_CACHE.js +79 -222
  93. package/lib/relay-hooks/react-cache/useFragment_REACT_CACHE.js +3 -27
  94. package/lib/relay-hooks/react-cache/useLazyLoadQuery_REACT_CACHE.js +11 -33
  95. package/lib/relay-hooks/react-cache/usePaginationFragment_REACT_CACHE.js +62 -85
  96. package/lib/relay-hooks/react-cache/usePreloadedQuery_REACT_CACHE.js +20 -63
  97. package/lib/relay-hooks/react-cache/useRefetchableFragmentInternal_REACT_CACHE.js +53 -179
  98. package/lib/relay-hooks/react-cache/useRefetchableFragment_REACT_CACHE.js +5 -27
  99. package/lib/relay-hooks/useBlockingPaginationFragment.js +58 -121
  100. package/lib/relay-hooks/useClientQuery.js +0 -21
  101. package/lib/relay-hooks/useEntryPointLoader.js +12 -100
  102. package/lib/relay-hooks/useFetchTrackingRef.js +6 -33
  103. package/lib/relay-hooks/useFragment.js +5 -32
  104. package/lib/relay-hooks/useFragmentNode.js +14 -55
  105. package/lib/relay-hooks/useIsMountedRef.js +2 -14
  106. package/lib/relay-hooks/useIsOperationNodeActive.js +6 -29
  107. package/lib/relay-hooks/useIsParentQueryActive.js +1 -15
  108. package/lib/relay-hooks/useLazyLoadQuery.js +2 -23
  109. package/lib/relay-hooks/useLazyLoadQueryNode.js +18 -63
  110. package/lib/relay-hooks/useLoadMoreFunction.js +44 -100
  111. package/lib/relay-hooks/useMemoOperationDescriptor.js +4 -23
  112. package/lib/relay-hooks/useMemoVariables.js +8 -43
  113. package/lib/relay-hooks/useMutation.js +6 -34
  114. package/lib/relay-hooks/usePaginationFragment.js +49 -89
  115. package/lib/relay-hooks/usePreloadedQuery.js +10 -54
  116. package/lib/relay-hooks/useQueryLoader.js +18 -116
  117. package/lib/relay-hooks/useRefetchableFragment.js +4 -30
  118. package/lib/relay-hooks/useRefetchableFragmentNode.js +58 -184
  119. package/lib/relay-hooks/useRelayEnvironment.js +2 -16
  120. package/lib/relay-hooks/useStaticFragmentNodeWarning.js +2 -20
  121. package/lib/relay-hooks/useSubscribeToInvalidationState.js +3 -28
  122. package/lib/relay-hooks/useSubscription.js +3 -22
  123. package/lib/relay-hooks/useUnsafeRef_DEPRECATED.js +12 -0
  124. package/multi-actor/ActorChange.js.flow +1 -1
  125. package/multi-actor/index.js.flow +1 -1
  126. package/multi-actor/useRelayActorEnvironment.js.flow +2 -2
  127. package/package.json +2 -2
  128. package/react-relay-hooks.js +2 -2
  129. package/react-relay-hooks.min.js +2 -2
  130. package/react-relay-legacy.js +2 -2
  131. package/react-relay-legacy.min.js +2 -2
  132. package/react-relay.js +2 -2
  133. package/react-relay.min.js +2 -2
  134. package/relay-hooks/EntryPointContainer.react.js.flow +6 -1
  135. package/relay-hooks/EntryPointTypes.flow.js.flow +23 -20
  136. package/relay-hooks/FragmentResource.js.flow +148 -34
  137. package/relay-hooks/HooksImplementation.js.flow +1 -1
  138. package/relay-hooks/InternalLogger.js.flow +1 -1
  139. package/relay-hooks/LRUCache.js.flow +1 -1
  140. package/relay-hooks/LazyLoadEntryPointContainer_DEPRECATED.react.js.flow +19 -10
  141. package/relay-hooks/MatchContainer.js.flow +1 -1
  142. package/relay-hooks/NestedRelayEntryPointBuilderUtils.js.flow +51 -0
  143. package/relay-hooks/ProfilerContext.js.flow +1 -1
  144. package/relay-hooks/QueryResource.js.flow +25 -5
  145. package/relay-hooks/RelayEnvironmentProvider.js.flow +2 -2
  146. package/relay-hooks/SuspenseResource.js.flow +1 -1
  147. package/relay-hooks/__flowtests__/EntryPointTypes/EntryPointElementConfig-flowtest.js.flow +3 -1
  148. package/relay-hooks/__flowtests__/EntryPointTypes/NestedEntrypoints-flowtest.js.flow +9 -7
  149. package/relay-hooks/__flowtests__/__generated__/useFragmentFlowtest_user.graphql.js.flow +3 -1
  150. package/relay-hooks/__flowtests__/__generated__/useFragmentFlowtest_users.graphql.js.flow +3 -1
  151. package/relay-hooks/__flowtests__/useBlockingPaginationFragment-flowtest.js.flow +40 -33
  152. package/relay-hooks/__flowtests__/useFragment-flowtest.js.flow +1 -1
  153. package/relay-hooks/__flowtests__/usePaginationFragment-flowtest.js.flow +38 -32
  154. package/relay-hooks/__flowtests__/useRefetchableFragment-flowtest.js.flow +20 -18
  155. package/relay-hooks/__flowtests__/utils.js.flow +13 -2
  156. package/relay-hooks/loadEntryPoint.js.flow +15 -8
  157. package/relay-hooks/loadQuery.js.flow +32 -8
  158. package/relay-hooks/preloadQuery_DEPRECATED.js.flow +5 -6
  159. package/relay-hooks/prepareEntryPoint_DEPRECATED.js.flow +17 -10
  160. package/relay-hooks/react-cache/RelayReactCache.js.flow +1 -1
  161. package/relay-hooks/react-cache/getQueryResultOrFetchQuery_REACT_CACHE.js.flow +4 -4
  162. package/relay-hooks/react-cache/readFragmentInternal_REACT_CACHE.js.flow +5 -4
  163. package/relay-hooks/react-cache/useFragmentInternal_REACT_CACHE.js.flow +32 -14
  164. package/relay-hooks/react-cache/useFragment_REACT_CACHE.js.flow +4 -10
  165. package/relay-hooks/react-cache/useLazyLoadQuery_REACT_CACHE.js.flow +1 -1
  166. package/relay-hooks/react-cache/usePaginationFragment_REACT_CACHE.js.flow +39 -49
  167. package/relay-hooks/react-cache/usePreloadedQuery_REACT_CACHE.js.flow +1 -2
  168. package/relay-hooks/react-cache/useRefetchableFragmentInternal_REACT_CACHE.js.flow +29 -16
  169. package/relay-hooks/react-cache/useRefetchableFragment_REACT_CACHE.js.flow +17 -33
  170. package/relay-hooks/useBlockingPaginationFragment.js.flow +85 -58
  171. package/relay-hooks/useClientQuery.js.flow +3 -3
  172. package/relay-hooks/useEntryPointLoader.js.flow +10 -6
  173. package/relay-hooks/useFetchTrackingRef.js.flow +5 -4
  174. package/relay-hooks/useFragment.js.flow +2 -2
  175. package/relay-hooks/useFragmentNode.js.flow +7 -6
  176. package/relay-hooks/useIsMountedRef.js.flow +1 -1
  177. package/relay-hooks/useIsOperationNodeActive.js.flow +1 -1
  178. package/relay-hooks/useIsParentQueryActive.js.flow +1 -1
  179. package/relay-hooks/useLazyLoadQuery.js.flow +2 -2
  180. package/relay-hooks/useLazyLoadQueryNode.js.flow +2 -2
  181. package/relay-hooks/useLoadMoreFunction.js.flow +27 -16
  182. package/relay-hooks/useMemoOperationDescriptor.js.flow +3 -3
  183. package/relay-hooks/useMemoVariables.js.flow +13 -29
  184. package/relay-hooks/useMutation.js.flow +30 -13
  185. package/relay-hooks/usePaginationFragment.js.flow +55 -54
  186. package/relay-hooks/usePreloadedQuery.js.flow +47 -22
  187. package/relay-hooks/useQueryLoader.js.flow +78 -21
  188. package/relay-hooks/useRefetchableFragment.js.flow +65 -33
  189. package/relay-hooks/useRefetchableFragmentNode.js.flow +38 -17
  190. package/relay-hooks/useRelayEnvironment.js.flow +2 -2
  191. package/relay-hooks/useStaticFragmentNodeWarning.js.flow +3 -3
  192. package/relay-hooks/useSubscribeToInvalidationState.js.flow +2 -2
  193. package/relay-hooks/useSubscription.js.flow +1 -1
  194. package/relay-hooks/useUnsafeRef_DEPRECATED.js.flow +25 -0
  195. package/lib/readContext.js +0 -27
  196. package/readContext.js.flow +0 -29
@@ -1,41 +1,26 @@
1
- /**
2
- * Copyright (c) Meta Platforms, Inc. and affiliates.
3
- *
4
- * This source code is licensed under the MIT license found in the
5
- * LICENSE file in the root directory of this source tree.
6
- *
7
- *
8
- * @format
9
- * @emails oncall+relay
10
- */
11
1
  'use strict';
12
2
 
13
3
  var _require = require('./loadQuery'),
14
- loadQuery = _require.loadQuery;
15
-
4
+ loadQuery = _require.loadQuery;
16
5
  function loadEntryPoint(environmentProvider, entryPoint, entryPointParams) {
17
- // Start loading the code for the entrypoint
18
6
  var loadingPromise = null;
19
-
20
7
  if (entryPoint.root.getModuleIfRequired() == null) {
21
8
  loadingPromise = entryPoint.root.load();
22
9
  }
23
-
24
10
  var preloadProps = entryPoint.getPreloadProps(entryPointParams);
25
11
  var queries = preloadProps.queries,
26
- entryPoints = preloadProps.entryPoints,
27
- extraProps = preloadProps.extraProps;
12
+ entryPoints = preloadProps.entryPoints,
13
+ extraProps = preloadProps.extraProps;
28
14
  var preloadedQueries = {};
29
15
  var preloadedEntryPoints = {};
30
-
31
16
  if (queries != null) {
32
17
  var queriesPropNames = Object.keys(queries);
33
18
  queriesPropNames.forEach(function (queryPropName) {
34
19
  var _queries$queryPropNam = queries[queryPropName],
35
- environmentProviderOptions = _queries$queryPropNam.environmentProviderOptions,
36
- options = _queries$queryPropNam.options,
37
- parameters = _queries$queryPropNam.parameters,
38
- variables = _queries$queryPropNam.variables;
20
+ environmentProviderOptions = _queries$queryPropNam.environmentProviderOptions,
21
+ options = _queries$queryPropNam.options,
22
+ parameters = _queries$queryPropNam.parameters,
23
+ variables = _queries$queryPropNam.variables;
39
24
  var environment = environmentProvider.getEnvironment(environmentProviderOptions);
40
25
  preloadedQueries[queryPropName] = loadQuery(environment, parameters, variables, {
41
26
  fetchPolicy: options === null || options === void 0 ? void 0 : options.fetchPolicy,
@@ -44,75 +29,55 @@ function loadEntryPoint(environmentProvider, entryPoint, entryPointParams) {
44
29
  }, environmentProviderOptions);
45
30
  });
46
31
  }
47
-
48
32
  if (entryPoints != null) {
49
33
  var entryPointPropNames = Object.keys(entryPoints);
50
34
  entryPointPropNames.forEach(function (entryPointPropName) {
51
35
  var entryPointDescription = entryPoints[entryPointPropName];
52
-
53
36
  if (entryPointDescription == null) {
54
37
  return;
55
38
  }
56
-
57
39
  var nestedEntryPoint = entryPointDescription.entryPoint,
58
- nestedParams = entryPointDescription.entryPointParams;
40
+ nestedParams = entryPointDescription.entryPointParams;
59
41
  preloadedEntryPoints[entryPointPropName] = loadEntryPoint(environmentProvider, nestedEntryPoint, nestedParams);
60
42
  });
61
43
  }
62
-
63
44
  var isDisposed = false;
64
45
  return {
65
46
  dispose: function dispose() {
66
47
  if (isDisposed) {
67
48
  return;
68
49
  }
69
-
70
50
  if (preloadedQueries != null) {
71
51
  Object.values(preloadedQueries).forEach(function (_ref) {
72
52
  var innerDispose = _ref.dispose;
73
53
  innerDispose();
74
54
  });
75
55
  }
76
-
77
56
  if (preloadedEntryPoints != null) {
78
57
  Object.values(preloadedEntryPoints).forEach(function (_ref2) {
79
58
  var innerDispose = _ref2.dispose;
80
59
  innerDispose();
81
60
  });
82
61
  }
83
-
84
62
  isDisposed = true;
85
63
  },
86
64
  entryPoints: preloadedEntryPoints,
87
65
  extraProps: extraProps !== null && extraProps !== void 0 ? extraProps : null,
88
66
  getComponent: function getComponent() {
89
67
  var componentModule = entryPoint.root.getModuleIfRequired();
90
-
91
68
  if (componentModule == null) {
92
69
  var _loadingPromise;
93
-
94
70
  loadingPromise = (_loadingPromise = loadingPromise) !== null && _loadingPromise !== void 0 ? _loadingPromise : entryPoint.root.load();
95
71
  throw loadingPromise;
96
- } // On certain platforms, getting an es6 module with a default export from a JSResource will return an object like
97
- // {default: module}, so let's assume that if the "component" has a static property named "default"
98
- // that it's actually an es6 module wrapper, so unwrap it. This won't work for React classes with a static property named "default", but
99
- // that's probably a worthwhile trade-off.
100
-
101
-
102
- var component = // $FlowIgnore[prop-missing]
103
- componentModule["default"] != null ? componentModule["default"] : componentModule; // $FlowFixMe[incompatible-cast] - trust me Flow, its entryPoint component
104
-
72
+ }
73
+ var component = componentModule["default"] != null ? componentModule["default"] : componentModule;
105
74
  return component;
106
75
  },
107
-
108
- // $FlowFixMe[unsafe-getters-setters] - this has no side effects
109
76
  get isDisposed() {
110
77
  return isDisposed;
111
78
  },
112
-
113
79
  queries: preloadedQueries,
114
80
  rootModuleID: entryPoint.root.getModuleId()
115
81
  };
116
82
  }
117
-
118
83
  module.exports = loadEntryPoint;
@@ -1,132 +1,57 @@
1
- /**
2
- * Copyright (c) Meta Platforms, Inc. and affiliates.
3
- *
4
- * This source code is licensed under the MIT license found in the
5
- * LICENSE file in the root directory of this source tree.
6
- *
7
- *
8
- * @format
9
- * @emails oncall+relay
10
- */
11
1
  'use strict';
12
2
 
13
3
  var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault")["default"];
14
-
15
4
  var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
16
-
17
5
  var invariant = require('invariant');
18
-
19
6
  var React = require('react');
20
-
21
7
  var _require = require('relay-runtime'),
22
- Observable = _require.Observable,
23
- PreloadableQueryRegistry = _require.PreloadableQueryRegistry,
24
- RelayFeatureFlags = _require.RelayFeatureFlags,
25
- ReplaySubject = _require.ReplaySubject,
26
- fetchQueryDeduped = _require.__internal.fetchQueryDeduped,
27
- createOperationDescriptor = _require.createOperationDescriptor,
28
- getRequest = _require.getRequest,
29
- getRequestIdentifier = _require.getRequestIdentifier;
30
-
8
+ fetchQueryDeduped = _require.__internal.fetchQueryDeduped,
9
+ Observable = _require.Observable,
10
+ PreloadableQueryRegistry = _require.PreloadableQueryRegistry,
11
+ RelayFeatureFlags = _require.RelayFeatureFlags,
12
+ ReplaySubject = _require.ReplaySubject,
13
+ createOperationDescriptor = _require.createOperationDescriptor,
14
+ getRequest = _require.getRequest,
15
+ getRequestIdentifier = _require.getRequestIdentifier;
31
16
  var warning = require("fbjs/lib/warning");
32
-
33
17
  var RenderDispatcher = null;
34
18
  var fetchKey = 100001;
35
-
36
19
  function useTrackLoadQueryInRender() {
37
20
  if (RenderDispatcher === null) {
38
21
  var _React$__SECRET_INTER, _React$__SECRET_INTER2;
39
-
40
- // Flow does not know of React internals (rightly so), but we need to
41
- // ensure here that this function isn't called inside render.
42
- RenderDispatcher = // $FlowFixMe[prop-missing]
43
- (_React$__SECRET_INTER = React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED) === null || _React$__SECRET_INTER === void 0 ? void 0 : (_React$__SECRET_INTER2 = _React$__SECRET_INTER.ReactCurrentDispatcher) === null || _React$__SECRET_INTER2 === void 0 ? void 0 : _React$__SECRET_INTER2.current;
22
+ RenderDispatcher = (_React$__SECRET_INTER = React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED) === null || _React$__SECRET_INTER === void 0 ? void 0 : (_React$__SECRET_INTER2 = _React$__SECRET_INTER.ReactCurrentDispatcher) === null || _React$__SECRET_INTER2 === void 0 ? void 0 : _React$__SECRET_INTER2.current;
44
23
  }
45
24
  }
46
-
47
25
  function loadQuery(environment, preloadableRequest, variables, options, environmentProviderOptions) {
48
26
  var _React$__SECRET_INTER3, _React$__SECRET_INTER4, _options$__nameForWar, _options$fetchPolicy;
49
-
50
- // This code ensures that we don't call loadQuery during render.
51
- var CurrentDispatcher = // $FlowFixMe[prop-missing]
52
- (_React$__SECRET_INTER3 = React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED) === null || _React$__SECRET_INTER3 === void 0 ? void 0 : (_React$__SECRET_INTER4 = _React$__SECRET_INTER3.ReactCurrentDispatcher) === null || _React$__SECRET_INTER4 === void 0 ? void 0 : _React$__SECRET_INTER4.current;
53
- process.env.NODE_ENV !== "production" ? warning(RenderDispatcher == null || CurrentDispatcher !== RenderDispatcher, 'Relay: `%s` should not be called inside a React render function.', (_options$__nameForWar = options === null || options === void 0 ? void 0 : options.__nameForWarning) !== null && _options$__nameForWar !== void 0 ? _options$__nameForWar : 'loadQuery') : void 0; // Every time you call loadQuery, we will generate a new fetchKey.
54
- // This will ensure that every query reference that is created and
55
- // passed to usePreloadedQuery is independently evaluated,
56
- // even if they are for the same query/variables.
57
- // Specifically, we want to avoid a case where we try to refetch a
58
- // query by calling loadQuery a second time, and have the Suspense
59
- // cache in usePreloadedQuery reuse the cached result instead of
60
- // re-evaluating the new query ref and triggering a refetch if
61
- // necessary.
62
-
27
+ var CurrentDispatcher = (_React$__SECRET_INTER3 = React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED) === null || _React$__SECRET_INTER3 === void 0 ? void 0 : (_React$__SECRET_INTER4 = _React$__SECRET_INTER3.ReactCurrentDispatcher) === null || _React$__SECRET_INTER4 === void 0 ? void 0 : _React$__SECRET_INTER4.current;
28
+ process.env.NODE_ENV !== "production" ? warning(RenderDispatcher == null || CurrentDispatcher !== RenderDispatcher, 'Relay: `%s` should not be called inside a React render function.', (_options$__nameForWar = options === null || options === void 0 ? void 0 : options.__nameForWarning) !== null && _options$__nameForWar !== void 0 ? _options$__nameForWar : 'loadQuery') : void 0;
63
29
  fetchKey++;
64
30
  var fetchPolicy = (_options$fetchPolicy = options === null || options === void 0 ? void 0 : options.fetchPolicy) !== null && _options$fetchPolicy !== void 0 ? _options$fetchPolicy : 'store-or-network';
65
31
  var networkCacheConfig = (0, _objectSpread2["default"])((0, _objectSpread2["default"])({}, options === null || options === void 0 ? void 0 : options.networkCacheConfig), {}, {
66
32
  force: true
67
- }); // executeWithNetworkSource will retain and execute an operation
68
- // against the Relay store, given an Observable that would provide
69
- // the network events for the operation.
70
-
33
+ });
71
34
  var retainReference;
72
35
  var didExecuteNetworkSource = false;
73
-
74
36
  var executeWithNetworkSource = function executeWithNetworkSource(operation, networkObservable) {
75
37
  didExecuteNetworkSource = true;
76
38
  return environment.executeWithSource({
77
39
  operation: operation,
78
40
  source: networkObservable
79
41
  });
80
- }; // N.B. For loadQuery, we unconventionally want to return an Observable
81
- // that isn't lazily executed, meaning that we don't want to wait
82
- // until the returned Observable is subscribed to to actually start
83
- // fetching and executing an operation; i.e. we want to execute the
84
- // operation eagerly, when loadQuery is called.
85
- // For this reason, we use an intermediate executionSubject which
86
- // allows us to capture the events that occur during the eager execution
87
- // of the operation, and then replay them to the Observable we
88
- // ultimately return.
89
-
90
-
42
+ };
91
43
  var executionSubject = new ReplaySubject();
92
44
  var returnedObservable = Observable.create(function (sink) {
93
45
  return executionSubject.subscribe(sink);
94
46
  });
95
47
  var unsubscribeFromNetworkRequest;
96
- var networkError = null; // makeNetworkRequest will immediately start a raw network request if
97
- // one isn't already in flight and return an Observable that when
98
- // subscribed to will replay the network events that have occured so far,
99
- // as well as subsequent events.
100
-
48
+ var networkError = null;
101
49
  var didMakeNetworkRequest = false;
102
-
103
50
  var makeNetworkRequest = function makeNetworkRequest(params) {
104
- // N.B. this function is called synchronously or not at all
105
- // didMakeNetworkRequest is safe to rely on in the returned value
106
- // Even if the request gets deduped below, we still wan't to return an
107
- // observable that provides the replayed network events for the query,
108
- // so we set this to true before deduping, to guarantee that the
109
- // `source` observable is returned.
110
51
  didMakeNetworkRequest = true;
111
52
  var observable;
112
53
  var subject = new ReplaySubject();
113
-
114
54
  if (RelayFeatureFlags.ENABLE_LOAD_QUERY_REQUEST_DEDUPING === true) {
115
- // Here, we are calling fetchQueryDeduped at the network layer level,
116
- // which ensures that only a single network request is active for a given
117
- // (environment, identifier) pair.
118
- // Since network requests can be started /before/ we have the query ast
119
- // necessary to process the results, we need to dedupe the raw requests
120
- // separately from deduping the operation execution; specifically,
121
- // if `loadQuery` is called multiple times before the query ast is available,
122
- // we still want the network request to be deduped.
123
- // - If a duplicate active network request is found, it will return an
124
- // Observable that replays the events of the already active request.
125
- // - If no duplicate active network request is found, it will call the fetchFn
126
- // to start the request, and return an Observable that will replay
127
- // the events from the network request.
128
- // We provide an extra key to the identifier to distinguish deduping
129
- // of raw network requests vs deduping of operation executions.
130
55
  var identifier = 'raw-network-request-' + getRequestIdentifier(params, variables);
131
56
  observable = fetchQueryDeduped(environment, identifier, function () {
132
57
  var network = environment.getNetwork();
@@ -136,21 +61,19 @@ function loadQuery(environment, preloadableRequest, variables, options, environm
136
61
  var network = environment.getNetwork();
137
62
  observable = network.execute(params, variables, networkCacheConfig);
138
63
  }
139
-
140
64
  var _observable$subscribe = observable.subscribe({
141
- error: function error(err) {
142
- networkError = err;
143
- subject.error(err);
144
- },
145
- next: function next(data) {
146
- subject.next(data);
147
- },
148
- complete: function complete() {
149
- subject.complete();
150
- }
151
- }),
152
- unsubscribe = _observable$subscribe.unsubscribe;
153
-
65
+ error: function error(err) {
66
+ networkError = err;
67
+ subject.error(err);
68
+ },
69
+ next: function next(data) {
70
+ subject.next(data);
71
+ },
72
+ complete: function complete() {
73
+ subject.complete();
74
+ }
75
+ }),
76
+ unsubscribe = _observable$subscribe.unsubscribe;
154
77
  unsubscribeFromNetworkRequest = unsubscribe;
155
78
  return Observable.create(function (sink) {
156
79
  var subjectSubscription = subject.subscribe(sink);
@@ -160,36 +83,11 @@ function loadQuery(environment, preloadableRequest, variables, options, environm
160
83
  };
161
84
  });
162
85
  };
163
-
164
86
  var unsubscribeFromExecution;
165
-
166
87
  var executeDeduped = function executeDeduped(operation, fetchFn) {
167
88
  if (RelayFeatureFlags.ENABLE_LOAD_QUERY_REQUEST_DEDUPING === true) {
168
- // N.B. at this point, if we're calling execute with a query ast (OperationDescriptor),
169
- // we are guaranteed to have started a network request. We set this to
170
- // true here as well since `makeNetworkRequest` might get skipped in the case
171
- // where the query ast is already available and the query executions get deduped.
172
- // Even if the execution gets deduped below, we still wan't to return
173
- // an observable that provides the replayed network events for the query,
174
- // so we set this to true before deduping, to guarantee that the `source`
175
- // observable is returned.
176
89
  didMakeNetworkRequest = true;
177
- } // Here, we are calling fetchQueryDeduped, which ensures that only
178
- // a single operation is active for a given (environment, identifier) pair,
179
- // and also tracks the active state of the operation, which is necessary
180
- // for our Suspense infra to later be able to suspend (or not) on
181
- // active operations. Even though we already dedupe raw network requests,
182
- // we also need to dedupe and keep track operation execution for our Suspense
183
- // infra, and we also want to avoid processing responses more than once, for
184
- // the cases where `loadQuery` might be called multiple times after the query ast
185
- // is available.
186
- // - If a duplicate active operation is found, it will return an
187
- // Observable that replays the events of the already active operation.
188
- // - If no duplicate active operation is found, it will call the fetchFn
189
- // to execute the operation, and return an Observable that will provide
190
- // the events for executing the operation.
191
-
192
-
90
+ }
193
91
  var _fetchQueryDeduped$su = fetchQueryDeduped(environment, operation.request.identifier, fetchFn).subscribe({
194
92
  error: function error(err) {
195
93
  executionSubject.error(err);
@@ -201,71 +99,47 @@ function loadQuery(environment, preloadableRequest, variables, options, environm
201
99
  executionSubject.complete();
202
100
  }
203
101
  });
204
-
205
102
  unsubscribeFromExecution = _fetchQueryDeduped$su.unsubscribe;
206
103
  };
207
-
208
104
  var checkAvailabilityAndExecute = function checkAvailabilityAndExecute(concreteRequest) {
209
105
  var operation = createOperationDescriptor(concreteRequest, variables, networkCacheConfig);
210
106
  retainReference = environment.retain(operation);
211
-
212
107
  if (fetchPolicy === 'store-only') {
213
108
  return;
214
- } // N.B. If the fetch policy allows fulfillment from the store but the
215
- // environment already has the data for that operation cached in the store,
216
- // then we do nothing.
217
-
218
-
109
+ }
219
110
  var shouldFetch = fetchPolicy !== 'store-or-network' || environment.check(operation).status !== 'available';
220
-
221
111
  if (shouldFetch) {
222
112
  executeDeduped(operation, function () {
223
- // N.B. Since we have the operation synchronously available here,
224
- // we can immediately fetch and execute the operation.
225
113
  var networkObservable = makeNetworkRequest(concreteRequest.params);
226
114
  var executeObservable = executeWithNetworkSource(operation, networkObservable);
227
115
  return executeObservable;
228
116
  });
229
117
  }
230
118
  };
231
-
232
119
  var params;
233
120
  var cancelOnLoadCallback;
234
121
  var queryId;
235
-
236
122
  if (preloadableRequest.kind === 'PreloadableConcreteRequest') {
237
123
  var preloadableConcreteRequest = preloadableRequest;
238
124
  params = preloadableConcreteRequest.params;
239
125
  var _params = params;
240
126
  queryId = _params.id;
241
127
  !(queryId !== null) ? process.env.NODE_ENV !== "production" ? invariant(false, 'Relay: `loadQuery` requires that preloadable query `%s` has a persisted query id', params.name) : invariant(false) : void 0;
242
-
243
128
  var _module = PreloadableQueryRegistry.get(queryId);
244
-
245
129
  if (_module != null) {
246
130
  checkAvailabilityAndExecute(_module);
247
131
  } else {
248
- // If the module isn't synchronously available, we launch the
249
- // network request immediately if the fetchPolicy might produce
250
- // a network fetch, regardless of the state of the store cache. We
251
- // do this because we can't check if a query is cached without the
252
- // ast, and we know that if we don't have the query ast
253
- // available, then this query could've never been written to the
254
- // store in the first place, so it couldn't have been cached.
255
- var networkObservable = fetchPolicy === 'store-only' ? null : makeNetworkRequest(params); // $FlowFixMe[method-unbinding] added when improving typing for this parameters
256
-
132
+ var networkObservable = fetchPolicy === 'store-only' ? null : makeNetworkRequest(params);
257
133
  var _PreloadableQueryRegi = PreloadableQueryRegistry.onLoad(queryId, function (preloadedModule) {
258
134
  cancelOnLoadCallback();
259
135
  var operation = createOperationDescriptor(preloadedModule, variables, networkCacheConfig);
260
136
  retainReference = environment.retain(operation);
261
-
262
137
  if (networkObservable != null) {
263
138
  executeDeduped(operation, function () {
264
139
  return executeWithNetworkSource(operation, networkObservable);
265
140
  });
266
141
  }
267
142
  });
268
-
269
143
  cancelOnLoadCallback = _PreloadableQueryRegi.dispose;
270
144
  }
271
145
  } else {
@@ -275,35 +149,28 @@ function loadQuery(environment, preloadableRequest, variables, options, environm
275
149
  queryId = params.cacheID != null ? params.cacheID : params.id;
276
150
  checkAvailabilityAndExecute(request);
277
151
  }
278
-
279
152
  var isDisposed = false;
280
153
  var isReleased = false;
281
154
  var isNetworkRequestCancelled = false;
282
-
283
155
  var releaseQuery = function releaseQuery() {
284
156
  if (isReleased) {
285
157
  return;
286
158
  }
287
-
288
159
  retainReference && retainReference.dispose();
289
160
  isReleased = true;
290
161
  };
291
-
292
162
  var cancelNetworkRequest = function cancelNetworkRequest() {
293
163
  if (isNetworkRequestCancelled) {
294
164
  return;
295
165
  }
296
-
297
166
  if (didExecuteNetworkSource) {
298
167
  unsubscribeFromExecution && unsubscribeFromExecution();
299
168
  } else {
300
169
  unsubscribeFromNetworkRequest && unsubscribeFromNetworkRequest();
301
170
  }
302
-
303
171
  cancelOnLoadCallback && cancelOnLoadCallback();
304
172
  isNetworkRequestCancelled = true;
305
173
  };
306
-
307
174
  return {
308
175
  kind: 'PreloadedQuery',
309
176
  environment: environment,
@@ -312,7 +179,6 @@ function loadQuery(environment, preloadableRequest, variables, options, environm
312
179
  if (isDisposed) {
313
180
  return;
314
181
  }
315
-
316
182
  releaseQuery();
317
183
  cancelNetworkRequest();
318
184
  isDisposed = true;
@@ -321,17 +187,12 @@ function loadQuery(environment, preloadableRequest, variables, options, environm
321
187
  cancelNetworkRequest: cancelNetworkRequest,
322
188
  fetchKey: fetchKey,
323
189
  id: queryId,
324
-
325
- // $FlowFixMe[unsafe-getters-setters] - this has no side effects
326
190
  get isDisposed() {
327
191
  return isDisposed || isReleased;
328
192
  },
329
-
330
- // $FlowFixMe[unsafe-getters-setters] - this has no side effects
331
193
  get networkError() {
332
194
  return networkError;
333
195
  },
334
-
335
196
  name: params.name,
336
197
  networkCacheConfig: networkCacheConfig,
337
198
  fetchPolicy: fetchPolicy,
@@ -339,7 +200,6 @@ function loadQuery(environment, preloadableRequest, variables, options, environm
339
200
  variables: variables
340
201
  };
341
202
  }
342
-
343
203
  module.exports = {
344
204
  loadQuery: loadQuery,
345
205
  useTrackLoadQueryInRender: useTrackLoadQueryInRender
@@ -1,71 +1,43 @@
1
- /**
2
- * Copyright (c) Meta Platforms, Inc. and affiliates.
3
- *
4
- * This source code is licensed under the MIT license found in the
5
- * LICENSE file in the root directory of this source tree.
6
- *
7
- * @emails oncall+relay
8
- *
9
- * @format
10
- */
11
1
  'use strict';
12
2
 
13
3
  var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault")["default"];
14
-
15
4
  var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
16
-
17
5
  var _require = require('relay-runtime'),
18
- Observable = _require.Observable,
19
- PreloadableQueryRegistry = _require.PreloadableQueryRegistry,
20
- RelayFeatureFlags = _require.RelayFeatureFlags,
21
- ReplaySubject = _require.ReplaySubject,
22
- createOperationDescriptor = _require.createOperationDescriptor,
23
- getRequest = _require.getRequest,
24
- getRequestIdentifier = _require.getRequestIdentifier; // Expire results by this delay after they resolve.
25
-
26
-
27
- var DEFAULT_PREFETCH_TIMEOUT = 30 * 1000; // 30 seconds
28
-
6
+ Observable = _require.Observable,
7
+ PreloadableQueryRegistry = _require.PreloadableQueryRegistry,
8
+ ReplaySubject = _require.ReplaySubject,
9
+ createOperationDescriptor = _require.createOperationDescriptor,
10
+ getRequest = _require.getRequest,
11
+ getRequestIdentifier = _require.getRequestIdentifier;
12
+ var DEFAULT_PREFETCH_TIMEOUT = 30 * 1000;
29
13
  var WEAKMAP_SUPPORTED = typeof WeakMap === 'function';
30
14
  var STORE_OR_NETWORK_DEFAULT = 'store-or-network';
31
15
  var pendingQueriesByEnvironment = WEAKMAP_SUPPORTED ? new WeakMap() : new Map();
32
-
33
16
  function preloadQuery(environment, preloadableRequest, variables, options, environmentProviderOptions) {
34
17
  var _pendingQueries = pendingQueriesByEnvironment.get(environment);
35
-
36
18
  if (_pendingQueries == null) {
37
19
  _pendingQueries = new Map();
38
20
  pendingQueriesByEnvironment.set(environment, _pendingQueries);
39
21
  }
40
-
41
- var pendingQueries = _pendingQueries; // store in a const for flow
42
-
22
+ var pendingQueries = _pendingQueries;
43
23
  var queryEntry = preloadQueryDeduped(environment, pendingQueries, preloadableRequest, variables, options);
44
24
  var source = queryEntry.kind === 'network' ? Observable.create(function (sink) {
45
25
  var subscription;
46
-
47
26
  if (pendingQueries.get(queryEntry.cacheKey) == null) {
48
27
  var newQueryEntry = preloadQueryDeduped(environment, pendingQueries, preloadableRequest, variables, options);
49
-
50
28
  if (newQueryEntry.kind === 'network') {
51
29
  subscription = newQueryEntry.subject.subscribe(sink);
52
30
  }
53
31
  } else {
54
32
  subscription = queryEntry.subject.subscribe(sink);
55
33
  }
56
-
57
34
  return function () {
58
35
  var _subscription;
59
-
60
36
  (_subscription = subscription) === null || _subscription === void 0 ? void 0 : _subscription.unsubscribe();
61
-
62
37
  if (environment.isServer()) {
63
38
  return;
64
39
  }
65
-
66
40
  setTimeout(function () {
67
- // Clear the cache entry after the default timeout
68
- // null-check for Flow
69
41
  if (queryEntry != null) {
70
42
  cleanup(pendingQueries, queryEntry);
71
43
  }
@@ -86,13 +58,10 @@ function preloadQuery(environment, preloadableRequest, variables, options, envir
86
58
  status: queryEntry.status
87
59
  };
88
60
  }
89
-
90
61
  function preloadQueryDeduped(environment, pendingQueries, preloadableRequest, variables, options) {
91
62
  var _options$fetchPolicy;
92
-
93
63
  var params;
94
64
  var query;
95
-
96
65
  if (preloadableRequest.kind === 'PreloadableConcreteRequest') {
97
66
  var preloadableConcreteRequest = preloadableRequest;
98
67
  params = preloadableConcreteRequest.params;
@@ -101,7 +70,6 @@ function preloadQueryDeduped(environment, pendingQueries, preloadableRequest, va
101
70
  query = getRequest(preloadableRequest);
102
71
  params = query.params;
103
72
  }
104
-
105
73
  var network = environment.getNetwork();
106
74
  var fetchPolicy = (_options$fetchPolicy = options === null || options === void 0 ? void 0 : options.fetchPolicy) !== null && _options$fetchPolicy !== void 0 ? _options$fetchPolicy : STORE_OR_NETWORK_DEFAULT;
107
75
  var fetchKey = options === null || options === void 0 ? void 0 : options.fetchKey;
@@ -114,10 +82,8 @@ function preloadQueryDeduped(environment, pendingQueries, preloadableRequest, va
114
82
  status: 'missing'
115
83
  };
116
84
  var nextQueryEntry;
117
-
118
85
  if (availability.status === 'available' && query != null) {
119
86
  var _availability$fetchTi;
120
-
121
87
  nextQueryEntry = prevQueryEntry && prevQueryEntry.kind === 'cache' ? prevQueryEntry : {
122
88
  cacheKey: cacheKey,
123
89
  fetchKey: fetchKey,
@@ -131,18 +97,14 @@ function preloadQueryDeduped(environment, pendingQueries, preloadableRequest, va
131
97
  fetchTime: (_availability$fetchTi = availability === null || availability === void 0 ? void 0 : availability.fetchTime) !== null && _availability$fetchTi !== void 0 ? _availability$fetchTi : null
132
98
  }
133
99
  };
134
-
135
100
  if (!environment.isServer() && prevQueryEntry == null) {
136
101
  setTimeout(function () {
137
- // Clear the cache entry after the default timeout
138
- // null-check for Flow
139
102
  if (nextQueryEntry != null) {
140
103
  cleanup(pendingQueries, nextQueryEntry);
141
104
  }
142
105
  }, DEFAULT_PREFETCH_TIMEOUT);
143
106
  }
144
107
  } else if (prevQueryEntry == null || prevQueryEntry.kind !== 'network') {
145
- // Should fetch but we're not already fetching: fetch!
146
108
  var source = network.execute(params, variables, networkCacheConfig, null);
147
109
  var subject = new ReplaySubject();
148
110
  nextQueryEntry = {
@@ -162,10 +124,7 @@ function preloadQueryDeduped(environment, pendingQueries, preloadableRequest, va
162
124
  if (environment.isServer()) {
163
125
  return;
164
126
  }
165
-
166
127
  setTimeout(function () {
167
- // Clear the cache entry after the default timeout
168
- // null-check for Flow
169
128
  if (nextQueryEntry != null) {
170
129
  cleanup(pendingQueries, nextQueryEntry);
171
130
  }
@@ -185,25 +144,16 @@ function preloadQueryDeduped(environment, pendingQueries, preloadableRequest, va
185
144
  } else {
186
145
  nextQueryEntry = prevQueryEntry;
187
146
  }
188
-
189
147
  pendingQueries.set(cacheKey, nextQueryEntry);
190
148
  return nextQueryEntry;
191
149
  }
192
-
193
150
  function cleanup(pendingQueries, entry) {
194
- // Reload the entry by its cache key and only invalidate if its the identical
195
- // entry instance. This ensures that if the same query/variables are fetched
196
- // successively that a timeout/expiration from an earlier fetch doesn't clear
197
- // a subsequent fetch.
198
151
  var currentEntry = pendingQueries.get(entry.cacheKey);
199
-
200
152
  if (currentEntry != null && currentEntry === entry) {
201
153
  if (currentEntry.kind === 'network') {
202
154
  currentEntry.subscription.unsubscribe();
203
155
  }
204
-
205
156
  pendingQueries["delete"](currentEntry.cacheKey);
206
157
  }
207
158
  }
208
-
209
159
  module.exports = preloadQuery;