react-relay 14.1.0 → 16.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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,52 +1,30 @@
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
- * @emails oncall+relay
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 _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
18
-
19
6
  var LRUCache = require('./LRUCache');
20
-
21
7
  var SuspenseResource = require('./SuspenseResource');
22
-
23
8
  var invariant = require('invariant');
24
-
25
9
  var _require = require('relay-runtime'),
26
- isPromise = _require.isPromise;
27
-
10
+ isPromise = _require.isPromise;
28
11
  var warning = require("fbjs/lib/warning");
29
-
30
12
  var CACHE_CAPACITY = 1000;
31
13
  var DEFAULT_FETCH_POLICY = 'store-or-network';
14
+ var DEFAULT_LIVE_FETCH_POLICY = 'store-and-network';
32
15
  var WEAKMAP_SUPPORTED = typeof WeakMap === 'function';
33
-
34
16
  function operationIsLiveQuery(operation) {
35
17
  return operation.request.node.params.metadata.live !== undefined;
36
18
  }
37
-
38
19
  function getQueryCacheIdentifier(environment, operation, maybeFetchPolicy, maybeRenderPolicy, cacheBreaker) {
39
- var fetchPolicy = maybeFetchPolicy !== null && maybeFetchPolicy !== void 0 ? maybeFetchPolicy : DEFAULT_FETCH_POLICY;
20
+ var fetchPolicy = maybeFetchPolicy !== null && maybeFetchPolicy !== void 0 ? maybeFetchPolicy : operationIsLiveQuery(operation) ? DEFAULT_LIVE_FETCH_POLICY : DEFAULT_FETCH_POLICY;
40
21
  var renderPolicy = maybeRenderPolicy !== null && maybeRenderPolicy !== void 0 ? maybeRenderPolicy : environment.UNSTABLE_getDefaultRenderPolicy();
41
22
  var cacheIdentifier = "".concat(fetchPolicy, "-").concat(renderPolicy, "-").concat(operation.request.identifier);
42
-
43
23
  if (cacheBreaker != null) {
44
24
  return "".concat(cacheIdentifier, "-").concat(cacheBreaker);
45
25
  }
46
-
47
26
  return cacheIdentifier;
48
27
  }
49
-
50
28
  function getQueryResult(operation, cacheIdentifier) {
51
29
  var rootFragmentRef = {
52
30
  __id: operation.fragment.dataID,
@@ -60,9 +38,7 @@ function getQueryResult(operation, cacheIdentifier) {
60
38
  operation: operation
61
39
  };
62
40
  }
63
-
64
41
  var nextID = 200000;
65
-
66
42
  function createCacheEntry(cacheIdentifier, operation, operationAvailability, value, networkSubscription, onDispose) {
67
43
  var isLiveQuery = operationIsLiveQuery(operation);
68
44
  var currentValue = value;
@@ -71,13 +47,9 @@ function createCacheEntry(cacheIdentifier, operation, operationAvailability, val
71
47
  var retention = environment.retain(operation);
72
48
  return {
73
49
  dispose: function dispose() {
74
- // Normally if this entry never commits, the request would've ended by the
75
- // time this timeout expires and the temporary retain is released. However,
76
- // we need to do this for live queries which remain open indefinitely.
77
50
  if (isLiveQuery && currentNetworkSubscription != null) {
78
51
  currentNetworkSubscription.unsubscribe();
79
52
  }
80
-
81
53
  retention.dispose();
82
54
  onDispose(cacheEntry);
83
55
  }
@@ -98,7 +70,6 @@ function createCacheEntry(cacheIdentifier, operation, operationAvailability, val
98
70
  if (isLiveQuery && currentNetworkSubscription != null) {
99
71
  currentNetworkSubscription.unsubscribe();
100
72
  }
101
-
102
73
  currentNetworkSubscription = subscription;
103
74
  },
104
75
  temporaryRetain: function temporaryRetain(environment) {
@@ -113,71 +84,40 @@ function createCacheEntry(cacheIdentifier, operation, operationAvailability, val
113
84
  };
114
85
  return cacheEntry;
115
86
  }
116
-
117
87
  var QueryResourceImpl = /*#__PURE__*/function () {
118
88
  function QueryResourceImpl(environment) {
119
89
  var _this = this;
120
-
121
90
  (0, _defineProperty2["default"])(this, "_clearCacheEntry", function (cacheEntry) {
122
91
  _this._cache["delete"](cacheEntry.cacheIdentifier);
123
92
  });
124
93
  this._environment = environment;
125
94
  this._cache = LRUCache.create(CACHE_CAPACITY);
126
95
  }
127
-
128
96
  var _proto = QueryResourceImpl.prototype;
129
-
130
97
  _proto.prepare = function prepare(operation, fetchObservable, maybeFetchPolicy, maybeRenderPolicy, observer, cacheBreaker, profilerContext) {
131
98
  var cacheIdentifier = getQueryCacheIdentifier(this._environment, operation, maybeFetchPolicy, maybeRenderPolicy, cacheBreaker);
132
99
  return this.prepareWithIdentifier(cacheIdentifier, operation, fetchObservable, maybeFetchPolicy, maybeRenderPolicy, observer, profilerContext);
133
- }
134
- /**
135
- * This function should be called during a Component's render function,
136
- * to either read an existing cached value for the query, or fetch the query
137
- * and suspend.
138
- */
139
- ;
140
-
100
+ };
141
101
  _proto.prepareWithIdentifier = function prepareWithIdentifier(cacheIdentifier, operation, fetchObservable, maybeFetchPolicy, maybeRenderPolicy, observer, profilerContext) {
142
102
  var environment = this._environment;
143
- var fetchPolicy = maybeFetchPolicy !== null && maybeFetchPolicy !== void 0 ? maybeFetchPolicy : DEFAULT_FETCH_POLICY;
144
- var renderPolicy = maybeRenderPolicy !== null && maybeRenderPolicy !== void 0 ? maybeRenderPolicy : environment.UNSTABLE_getDefaultRenderPolicy(); // 1. Check if there's a cached value for this operation, and reuse it if
145
- // it's available
146
-
103
+ var fetchPolicy = maybeFetchPolicy !== null && maybeFetchPolicy !== void 0 ? maybeFetchPolicy : operationIsLiveQuery(operation) ? DEFAULT_LIVE_FETCH_POLICY : DEFAULT_FETCH_POLICY;
104
+ var renderPolicy = maybeRenderPolicy !== null && maybeRenderPolicy !== void 0 ? maybeRenderPolicy : environment.UNSTABLE_getDefaultRenderPolicy();
147
105
  var cacheEntry = this._cache.get(cacheIdentifier);
148
-
149
106
  var temporaryRetainDisposable = null;
150
107
  var entryWasCached = cacheEntry != null;
151
-
152
108
  if (cacheEntry == null) {
153
- // 2. If a cached value isn't available, try fetching the operation.
154
- // _fetchAndSaveQuery will update the cache with either a Promise or
155
- // an Error to throw, or a QueryResult to return.
156
109
  cacheEntry = this._fetchAndSaveQuery(cacheIdentifier, operation, fetchObservable, fetchPolicy, renderPolicy, profilerContext, (0, _objectSpread2["default"])((0, _objectSpread2["default"])({}, observer), {}, {
157
110
  unsubscribe: function unsubscribe(subscription) {
158
- // 4. If the request is cancelled, make sure to dispose
159
- // of the temporary retain; this will ensure that a promise
160
- // doesn't remain unnecessarily cached until the temporary retain
161
- // expires. Not clearing the temporary retain might cause the
162
- // query to incorrectly re-suspend.
163
111
  if (temporaryRetainDisposable != null) {
164
112
  temporaryRetainDisposable.dispose();
165
113
  }
166
-
167
114
  var observerUnsubscribe = observer === null || observer === void 0 ? void 0 : observer.unsubscribe;
168
115
  observerUnsubscribe && observerUnsubscribe(subscription);
169
116
  }
170
117
  }));
171
- } // 3. Temporarily retain here in render phase. When the component reading
172
- // the operation is committed, we will transfer ownership of data retention
173
- // to the component.
174
- // In case the component never commits (mounts or updates) from this render,
175
- // this data retention hold will auto-release itself after a timeout.
176
-
177
-
118
+ }
178
119
  temporaryRetainDisposable = cacheEntry.temporaryRetain(environment);
179
120
  var cachedValue = cacheEntry.getValue();
180
-
181
121
  if (isPromise(cachedValue)) {
182
122
  environment.__log({
183
123
  name: 'suspense.query',
@@ -187,89 +127,59 @@ var QueryResourceImpl = /*#__PURE__*/function () {
187
127
  queryAvailability: cacheEntry.operationAvailability,
188
128
  renderPolicy: renderPolicy
189
129
  });
190
-
191
130
  throw cachedValue;
192
131
  }
193
-
194
132
  if (cachedValue instanceof Error) {
195
133
  throw cachedValue;
196
134
  }
197
-
198
135
  return cachedValue;
199
- }
200
- /**
201
- * This function should be called during a component's commit phase
202
- * (e.g. inside useEffect), in order to retain the operation in the Relay store
203
- * and transfer ownership of the operation to the component lifecycle.
204
- */
205
- ;
206
-
136
+ };
207
137
  _proto.retain = function retain(queryResult, profilerContext) {
208
138
  var environment = this._environment;
209
139
  var cacheIdentifier = queryResult.cacheIdentifier,
210
- operation = queryResult.operation;
211
-
140
+ operation = queryResult.operation;
212
141
  var cacheEntry = this._getOrCreateCacheEntry(cacheIdentifier, operation, null, queryResult, null);
213
-
214
142
  var disposable = cacheEntry.permanentRetain(environment);
215
-
216
143
  environment.__log({
217
144
  name: 'queryresource.retain',
218
145
  profilerContext: profilerContext,
219
146
  resourceID: cacheEntry.id
220
147
  });
221
-
222
148
  return {
223
149
  dispose: function dispose() {
224
150
  disposable.dispose();
225
151
  }
226
152
  };
227
153
  };
228
-
229
154
  _proto.releaseTemporaryRetain = function releaseTemporaryRetain(queryResult) {
230
155
  var cacheEntry = this._cache.get(queryResult.cacheIdentifier);
231
-
232
156
  if (cacheEntry != null) {
233
157
  cacheEntry.releaseTemporaryRetain();
234
158
  }
235
159
  };
236
-
237
160
  _proto.TESTS_ONLY__getCacheEntry = function TESTS_ONLY__getCacheEntry(operation, maybeFetchPolicy, maybeRenderPolicy, cacheBreaker) {
238
161
  var environment = this._environment;
239
162
  var cacheIdentifier = getQueryCacheIdentifier(environment, operation, maybeFetchPolicy, maybeRenderPolicy, cacheBreaker);
240
163
  return this._cache.get(cacheIdentifier);
241
164
  };
242
-
243
165
  _proto._getOrCreateCacheEntry = function _getOrCreateCacheEntry(cacheIdentifier, operation, operationAvailability, value, networkSubscription) {
244
166
  var cacheEntry = this._cache.get(cacheIdentifier);
245
-
246
167
  if (cacheEntry == null) {
247
168
  cacheEntry = createCacheEntry(cacheIdentifier, operation, operationAvailability, value, networkSubscription, this._clearCacheEntry);
248
-
249
169
  this._cache.set(cacheIdentifier, cacheEntry);
250
170
  }
251
-
252
171
  return cacheEntry;
253
172
  };
254
-
255
173
  _proto._fetchAndSaveQuery = function _fetchAndSaveQuery(cacheIdentifier, operation, fetchObservable, fetchPolicy, renderPolicy, profilerContext, observer) {
256
174
  var _this2 = this;
257
-
258
- var environment = this._environment; // NOTE: Running `check` will write missing data to the store using any
259
- // missing data handlers specified on the environment;
260
- // We run it here first to make the handlers get a chance to populate
261
- // missing data.
262
-
175
+ var environment = this._environment;
263
176
  var queryAvailability = environment.check(operation);
264
177
  var queryStatus = queryAvailability.status;
265
178
  var hasFullQuery = queryStatus === 'available';
266
179
  var canPartialRender = hasFullQuery || renderPolicy === 'partial' && queryStatus !== 'stale';
267
180
  var shouldFetch;
268
- var shouldAllowRender; // Different definitions for Promise in our repos can cause this variable
269
- // to cause errors when synced elsewhere
270
-
181
+ var shouldAllowRender;
271
182
  var resolveNetworkPromise = function resolveNetworkPromise() {};
272
-
273
183
  switch (fetchPolicy) {
274
184
  case 'store-only':
275
185
  {
@@ -277,21 +187,18 @@ var QueryResourceImpl = /*#__PURE__*/function () {
277
187
  shouldAllowRender = true;
278
188
  break;
279
189
  }
280
-
281
190
  case 'store-or-network':
282
191
  {
283
192
  shouldFetch = !hasFullQuery;
284
193
  shouldAllowRender = canPartialRender;
285
194
  break;
286
195
  }
287
-
288
196
  case 'store-and-network':
289
197
  {
290
198
  shouldFetch = true;
291
199
  shouldAllowRender = canPartialRender;
292
200
  break;
293
201
  }
294
-
295
202
  case 'network-only':
296
203
  default:
297
204
  {
@@ -299,40 +206,26 @@ var QueryResourceImpl = /*#__PURE__*/function () {
299
206
  shouldAllowRender = false;
300
207
  break;
301
208
  }
302
- } // NOTE: If this value is false, we will cache a promise for this
303
- // query, which means we will suspend here at this query root.
304
- // If it's true, we will cache the query resource and allow rendering to
305
- // continue.
306
-
307
-
209
+ }
308
210
  if (shouldAllowRender) {
309
211
  var queryResult = getQueryResult(operation, cacheIdentifier);
310
-
311
212
  var _cacheEntry = createCacheEntry(cacheIdentifier, operation, queryAvailability, queryResult, null, this._clearCacheEntry);
312
-
313
213
  this._cache.set(cacheIdentifier, _cacheEntry);
314
214
  }
315
-
316
215
  if (shouldFetch) {
317
216
  var _queryResult = getQueryResult(operation, cacheIdentifier);
318
-
319
217
  var networkSubscription;
320
218
  fetchObservable.subscribe({
321
219
  start: function start(subscription) {
322
220
  networkSubscription = subscription;
323
-
324
221
  var cacheEntry = _this2._cache.get(cacheIdentifier);
325
-
326
222
  if (cacheEntry) {
327
223
  cacheEntry.setNetworkSubscription(networkSubscription);
328
224
  }
329
-
330
225
  var observerStart = observer === null || observer === void 0 ? void 0 : observer.start;
331
-
332
226
  if (observerStart) {
333
227
  var subscriptionWithConditionalCancelation = (0, _objectSpread2["default"])((0, _objectSpread2["default"])({}, subscription), {}, {
334
228
  unsubscribe: function unsubscribe() {
335
- // Only live queries should have their network requests canceled.
336
229
  if (operationIsLiveQuery(operation)) {
337
230
  subscription.unsubscribe();
338
231
  }
@@ -343,32 +236,22 @@ var QueryResourceImpl = /*#__PURE__*/function () {
343
236
  },
344
237
  next: function next() {
345
238
  var cacheEntry = _this2._getOrCreateCacheEntry(cacheIdentifier, operation, queryAvailability, _queryResult, networkSubscription);
346
-
347
239
  cacheEntry.processedPayloadsCount += 1;
348
240
  cacheEntry.setValue(_queryResult);
349
241
  resolveNetworkPromise();
350
242
  var observerNext = observer === null || observer === void 0 ? void 0 : observer.next;
351
-
352
243
  if (observerNext != null) {
353
244
  var snapshot = environment.lookup(operation.fragment);
354
245
  observerNext(snapshot);
355
246
  }
356
247
  },
357
248
  error: function error(_error) {
358
- var cacheEntry = _this2._getOrCreateCacheEntry(cacheIdentifier, operation, queryAvailability, _error, networkSubscription); // If, this is the first thing we receive for the query,
359
- // before any other payload handled is error, we will cache and
360
- // re-throw that error later.
361
- // We will ignore errors for any incremental payloads we receive.
362
-
363
-
249
+ var cacheEntry = _this2._getOrCreateCacheEntry(cacheIdentifier, operation, queryAvailability, _error, networkSubscription);
364
250
  if (cacheEntry.processedPayloadsCount === 0) {
365
251
  cacheEntry.setValue(_error);
366
252
  } else {
367
- // TODO:T92030819 Remove this warning and actually throw the network error
368
- // To complete this task we need to have a way of precisely tracking suspendable points
369
253
  process.env.NODE_ENV !== "production" ? warning(false, 'QueryResource: An incremental payload for query `%s` returned an error: `%s`.', operation.fragment.node.name, String(_error.message)) : void 0;
370
254
  }
371
-
372
255
  resolveNetworkPromise();
373
256
  networkSubscription = null;
374
257
  cacheEntry.setNetworkSubscription(null);
@@ -378,40 +261,30 @@ var QueryResourceImpl = /*#__PURE__*/function () {
378
261
  complete: function complete() {
379
262
  resolveNetworkPromise();
380
263
  networkSubscription = null;
381
-
382
264
  var cacheEntry = _this2._cache.get(cacheIdentifier);
383
-
384
265
  if (cacheEntry) {
385
266
  cacheEntry.setNetworkSubscription(null);
386
267
  }
387
-
388
268
  var observerComplete = observer === null || observer === void 0 ? void 0 : observer.complete;
389
269
  observerComplete && observerComplete();
390
270
  },
391
271
  unsubscribe: observer === null || observer === void 0 ? void 0 : observer.unsubscribe
392
272
  });
393
-
394
273
  var _cacheEntry2 = this._cache.get(cacheIdentifier);
395
-
396
274
  if (!_cacheEntry2) {
397
275
  var networkPromise = new Promise(function (resolve) {
398
276
  resolveNetworkPromise = resolve;
399
- }); // $FlowExpectedError[prop-missing] Expando to annotate Promises.
400
-
277
+ });
401
278
  networkPromise.displayName = 'Relay(' + operation.fragment.node.name + ')';
402
279
  _cacheEntry2 = createCacheEntry(cacheIdentifier, operation, queryAvailability, networkPromise, networkSubscription, this._clearCacheEntry);
403
-
404
280
  this._cache.set(cacheIdentifier, _cacheEntry2);
405
281
  }
406
282
  } else {
407
283
  var observerComplete = observer === null || observer === void 0 ? void 0 : observer.complete;
408
284
  observerComplete && observerComplete();
409
285
  }
410
-
411
286
  var cacheEntry = this._cache.get(cacheIdentifier);
412
-
413
287
  !(cacheEntry != null) ? process.env.NODE_ENV !== "production" ? invariant(false, 'Relay: Expected to have cached a result when attempting to fetch query.' + "If you're seeing this, this is likely a bug in Relay.") : invariant(false) : void 0;
414
-
415
288
  environment.__log({
416
289
  name: 'queryresource.fetch',
417
290
  resourceID: cacheEntry.id,
@@ -422,31 +295,23 @@ var QueryResourceImpl = /*#__PURE__*/function () {
422
295
  queryAvailability: queryAvailability,
423
296
  shouldFetch: shouldFetch
424
297
  });
425
-
426
298
  return cacheEntry;
427
299
  };
428
-
429
300
  return QueryResourceImpl;
430
301
  }();
431
-
432
302
  function createQueryResource(environment) {
433
303
  return new QueryResourceImpl(environment);
434
304
  }
435
-
436
305
  var dataResources = WEAKMAP_SUPPORTED ? new WeakMap() : new Map();
437
-
438
306
  function getQueryResourceForEnvironment(environment) {
439
307
  var cached = dataResources.get(environment);
440
-
441
308
  if (cached) {
442
309
  return cached;
443
310
  }
444
-
445
311
  var newDataResource = createQueryResource(environment);
446
312
  dataResources.set(environment, newDataResource);
447
313
  return newDataResource;
448
314
  }
449
-
450
315
  module.exports = {
451
316
  createQueryResource: createQueryResource,
452
317
  getQueryResourceForEnvironment: getQueryResourceForEnvironment,
@@ -1,25 +1,12 @@
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
 
3
+ var ReactRelayContext = require('./../ReactRelayContext');
13
4
  var React = require('react');
14
-
15
- var ReactRelayContext = require('react-relay/ReactRelayContext');
16
-
17
5
  var useMemo = React.useMemo;
18
-
19
6
  function RelayEnvironmentProvider(props) {
20
7
  var children = props.children,
21
- environment = props.environment,
22
- getEnvironmentForActor = props.getEnvironmentForActor;
8
+ environment = props.environment,
9
+ getEnvironmentForActor = props.getEnvironmentForActor;
23
10
  var context = useMemo(function () {
24
11
  return {
25
12
  environment: environment,
@@ -30,5 +17,4 @@ function RelayEnvironmentProvider(props) {
30
17
  value: context
31
18
  }, children);
32
19
  }
33
-
34
20
  module.exports = RelayEnvironmentProvider;
@@ -1,129 +1,72 @@
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
- * @emails oncall+relay
9
- * @format
10
- */
11
1
  'use strict';
12
2
 
13
3
  var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault")["default"];
14
-
15
4
  var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
16
-
17
5
  var invariant = require('invariant');
18
-
19
6
  var TEMPORARY_RETAIN_DURATION_MS = 5 * 60 * 1000;
20
- /**
21
- * Allows you to retain a resource as part of a component lifecycle accounting
22
- * for Suspense. You temporarily retain the resource during render, then
23
- * permanently retain it during commit and release it during unmount.
24
- */
25
-
26
7
  var SuspenseResource = /*#__PURE__*/function () {
27
8
  function SuspenseResource(retain) {
28
9
  var _this = this;
29
-
30
10
  (0, _defineProperty2["default"])(this, "_retainCount", 0);
31
11
  (0, _defineProperty2["default"])(this, "_retainDisposable", null);
32
12
  (0, _defineProperty2["default"])(this, "_releaseTemporaryRetain", null);
33
-
34
13
  this._retain = function (environment) {
35
14
  _this._retainCount++;
36
-
37
15
  if (_this._retainCount === 1) {
38
16
  _this._retainDisposable = retain(environment);
39
17
  }
40
-
41
18
  return {
42
19
  dispose: function dispose() {
43
20
  _this._retainCount = Math.max(0, _this._retainCount - 1);
44
-
45
21
  if (_this._retainCount === 0) {
46
22
  !(_this._retainDisposable != null) ? process.env.NODE_ENV !== "production" ? invariant(false, 'Relay: Expected disposable to release query to be defined.' + "If you're seeing this, this is likely a bug in Relay.") : invariant(false) : void 0;
47
-
48
23
  _this._retainDisposable.dispose();
49
-
50
24
  _this._retainDisposable = null;
51
25
  }
52
26
  }
53
27
  };
54
28
  };
55
29
  }
56
-
57
30
  var _proto = SuspenseResource.prototype;
58
-
59
31
  _proto.temporaryRetain = function temporaryRetain(environment) {
60
32
  var _this2 = this;
61
-
62
33
  var _this$_releaseTempora;
63
-
64
- // If we're executing in a server environment, there's no need
65
- // to create temporary retains, since the component will never commit.
66
34
  if (environment.isServer()) {
67
35
  return {
68
36
  dispose: function dispose() {}
69
37
  };
70
- } // temporaryRetain is called during the render phase. However,
71
- // given that we can't tell if this render will eventually commit or not,
72
- // we create a timer to autodispose of this retain in case the associated
73
- // component never commits.
74
- // If the component /does/ commit, permanentRetain will clear this timeout
75
- // and permanently retain the data.
76
-
77
-
38
+ }
78
39
  var retention = this._retain(environment);
79
-
80
40
  var releaseQueryTimeout = null;
81
-
82
41
  var releaseTemporaryRetain = function releaseTemporaryRetain() {
83
42
  clearTimeout(releaseQueryTimeout);
84
43
  releaseQueryTimeout = null;
85
44
  _this2._releaseTemporaryRetain = null;
86
45
  retention.dispose();
87
46
  };
88
-
89
- releaseQueryTimeout = setTimeout(releaseTemporaryRetain, TEMPORARY_RETAIN_DURATION_MS); // NOTE: Since temporaryRetain can be called multiple times, we release
90
- // the previous temporary retain after we re-establish a new one, since
91
- // we only ever need a single temporary retain until the permanent retain is
92
- // established.
93
- // temporaryRetain may be called multiple times by React during the render
94
- // phase, as well as multiple times by other query components that are
95
- // rendering the same query/variables.
96
-
47
+ releaseQueryTimeout = setTimeout(releaseTemporaryRetain, TEMPORARY_RETAIN_DURATION_MS);
97
48
  (_this$_releaseTempora = this._releaseTemporaryRetain) === null || _this$_releaseTempora === void 0 ? void 0 : _this$_releaseTempora.call(this);
98
49
  this._releaseTemporaryRetain = releaseTemporaryRetain;
99
50
  return {
100
51
  dispose: function dispose() {
101
52
  var _this$_releaseTempora2;
102
-
103
53
  (_this$_releaseTempora2 = _this2._releaseTemporaryRetain) === null || _this$_releaseTempora2 === void 0 ? void 0 : _this$_releaseTempora2.call(_this2);
104
54
  }
105
55
  };
106
56
  };
107
-
108
57
  _proto.permanentRetain = function permanentRetain(environment) {
109
58
  var disposable = this._retain(environment);
110
-
111
59
  this.releaseTemporaryRetain();
112
60
  return disposable;
113
61
  };
114
-
115
62
  _proto.releaseTemporaryRetain = function releaseTemporaryRetain() {
116
63
  var _this$_releaseTempora3;
117
-
118
64
  (_this$_releaseTempora3 = this._releaseTemporaryRetain) === null || _this$_releaseTempora3 === void 0 ? void 0 : _this$_releaseTempora3.call(this);
119
65
  this._releaseTemporaryRetain = null;
120
66
  };
121
-
122
67
  _proto.getRetainCount = function getRetainCount() {
123
68
  return this._retainCount;
124
69
  };
125
-
126
70
  return SuspenseResource;
127
71
  }();
128
-
129
72
  module.exports = SuspenseResource;