react-relay 18.1.0 → 18.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,5 @@
1
1
  /**
2
- * Relay v18.1.0
2
+ * Relay v18.2.0
3
3
  *
4
4
  * Copyright (c) Meta Platforms, Inc. and affiliates.
5
5
  *
@@ -270,14 +270,11 @@ function getRelayProp(environment: IEnvironment) {
270
270
  function createContainer<
271
271
  Props: {...},
272
272
  Instance,
273
- TComponent: React.AbstractComponent<Props, Instance>,
273
+ TComponent: component(ref: React.RefSetter<Instance>, ...Props),
274
274
  >(
275
275
  Component: TComponent,
276
276
  fragmentSpec: GeneratedNodeMap,
277
- ): React.AbstractComponent<
278
- $RelayProps<React.ElementConfig<TComponent>, RelayProp>,
279
- Instance,
280
- > {
277
+ ): component(ref: React.RefSetter<Instance>, ...$RelayProps<Props, RelayProp>) {
281
278
  // $FlowFixMe[incompatible-return]
282
279
  return buildReactRelayContainer(
283
280
  Component,
package/hooks.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /**
2
- * Relay v18.1.0
2
+ * Relay v18.2.0
3
3
  *
4
4
  * Copyright (c) Meta Platforms, Inc. and affiliates.
5
5
  *
package/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /**
2
- * Relay v18.1.0
2
+ * Relay v18.2.0
3
3
  *
4
4
  * Copyright (c) Meta Platforms, Inc. and affiliates.
5
5
  *
package/index.js.flow CHANGED
@@ -28,6 +28,7 @@ const useFragment = require('./relay-hooks/useFragment');
28
28
  const useLazyLoadQuery = require('./relay-hooks/useLazyLoadQuery');
29
29
  const useMutation = require('./relay-hooks/useMutation');
30
30
  const usePaginationFragment = require('./relay-hooks/usePaginationFragment');
31
+ const usePrefetchableForwardPaginationFragment_EXPERIMENTAL = require('./relay-hooks/usePrefetchableForwardPaginationFragment_EXPERIMENTAL');
31
32
  const usePreloadedQuery = require('./relay-hooks/usePreloadedQuery');
32
33
  const useQueryLoader = require('./relay-hooks/useQueryLoader');
33
34
  const useRefetchableFragment = require('./relay-hooks/useRefetchableFragment');
@@ -125,6 +126,8 @@ module.exports = {
125
126
  usePaginationFragment: usePaginationFragment,
126
127
  usePreloadedQuery: usePreloadedQuery,
127
128
  useRefetchableFragment: useRefetchableFragment,
129
+ usePrefetchableForwardPaginationFragment_EXPERIMENTAL:
130
+ usePrefetchableForwardPaginationFragment_EXPERIMENTAL,
128
131
  useRelayEnvironment: useRelayEnvironment,
129
132
  useSubscribeToInvalidationState: useSubscribeToInvalidationState,
130
133
  useSubscription: useSubscription,
package/legacy.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /**
2
- * Relay v18.1.0
2
+ * Relay v18.2.0
3
3
  *
4
4
  * Copyright (c) Meta Platforms, Inc. and affiliates.
5
5
  *
package/lib/index.js CHANGED
@@ -18,6 +18,7 @@ var useFragment = require('./relay-hooks/useFragment');
18
18
  var useLazyLoadQuery = require('./relay-hooks/useLazyLoadQuery');
19
19
  var useMutation = require('./relay-hooks/useMutation');
20
20
  var usePaginationFragment = require('./relay-hooks/usePaginationFragment');
21
+ var usePrefetchableForwardPaginationFragment_EXPERIMENTAL = require('./relay-hooks/usePrefetchableForwardPaginationFragment_EXPERIMENTAL');
21
22
  var usePreloadedQuery = require('./relay-hooks/usePreloadedQuery');
22
23
  var useQueryLoader = require('./relay-hooks/useQueryLoader');
23
24
  var useRefetchableFragment = require('./relay-hooks/useRefetchableFragment');
@@ -57,6 +58,7 @@ module.exports = {
57
58
  usePaginationFragment: usePaginationFragment,
58
59
  usePreloadedQuery: usePreloadedQuery,
59
60
  useRefetchableFragment: useRefetchableFragment,
61
+ usePrefetchableForwardPaginationFragment_EXPERIMENTAL: usePrefetchableForwardPaginationFragment_EXPERIMENTAL,
60
62
  useRelayEnvironment: useRelayEnvironment,
61
63
  useSubscribeToInvalidationState: useSubscribeToInvalidationState,
62
64
  useSubscription: useSubscription
@@ -243,8 +243,7 @@ var FragmentResourceImpl = /*#__PURE__*/function () {
243
243
  var fragmentOwner = fragmentSelector.kind === 'PluralReaderSelector' ? fragmentSelector.selectors[0].owner : fragmentSelector.owner;
244
244
  var parentQueryPromiseResult = this._getAndSavePromiseForFragmentRequestInFlight(fragmentIdentifier, fragmentNode, fragmentOwner, fragmentResult);
245
245
  var parentQueryPromiseResultPromise = parentQueryPromiseResult === null || parentQueryPromiseResult === void 0 ? void 0 : parentQueryPromiseResult.promise;
246
- var missingResolverFieldPromises = (_missingLiveResolverF2 = (_missingLiveResolverF3 = missingLiveResolverFields(snapshot)) === null || _missingLiveResolverF3 === void 0 ? void 0 : _missingLiveResolverF3.map(function (_ref2) {
247
- var liveStateID = _ref2.liveStateID;
246
+ var missingResolverFieldPromises = (_missingLiveResolverF2 = (_missingLiveResolverF3 = missingLiveResolverFields(snapshot)) === null || _missingLiveResolverF3 === void 0 ? void 0 : _missingLiveResolverF3.map(function (liveStateID) {
248
247
  var store = environment.getStore();
249
248
  return store.getLiveResolverPromise(liveStateID);
250
249
  })) !== null && _missingLiveResolverF2 !== void 0 ? _missingLiveResolverF2 : [];
@@ -7,7 +7,6 @@ var _require = require('relay-runtime'),
7
7
  fetchQueryDeduped = _require.__internal.fetchQueryDeduped,
8
8
  Observable = _require.Observable,
9
9
  PreloadableQueryRegistry = _require.PreloadableQueryRegistry,
10
- RelayFeatureFlags = _require.RelayFeatureFlags,
11
10
  ReplaySubject = _require.ReplaySubject,
12
11
  createOperationDescriptor = _require.createOperationDescriptor,
13
12
  getRequest = _require.getRequest,
@@ -38,18 +37,12 @@ function loadQuery(environment, preloadableRequest, variables, options, environm
38
37
  var didMakeNetworkRequest = false;
39
38
  var makeNetworkRequest = function makeNetworkRequest(params) {
40
39
  didMakeNetworkRequest = true;
41
- var observable;
42
40
  var subject = new ReplaySubject();
43
- if (RelayFeatureFlags.ENABLE_LOAD_QUERY_REQUEST_DEDUPING === true) {
44
- var identifier = 'raw-network-request-' + getRequestIdentifier(params, variables);
45
- observable = fetchQueryDeduped(environment, identifier, function () {
46
- var network = environment.getNetwork();
47
- return network.execute(params, variables, networkCacheConfig);
48
- });
49
- } else {
41
+ var identifier = 'raw-network-request-' + getRequestIdentifier(params, variables);
42
+ var observable = fetchQueryDeduped(environment, identifier, function () {
50
43
  var network = environment.getNetwork();
51
- observable = network.execute(params, variables, networkCacheConfig);
52
- }
44
+ return network.execute(params, variables, networkCacheConfig);
45
+ });
53
46
  var _observable$subscribe = observable.subscribe({
54
47
  error: function error(err) {
55
48
  networkError = err;
@@ -74,9 +67,7 @@ function loadQuery(environment, preloadableRequest, variables, options, environm
74
67
  };
75
68
  var unsubscribeFromExecution;
76
69
  var executeDeduped = function executeDeduped(operation, fetchFn) {
77
- if (RelayFeatureFlags.ENABLE_LOAD_QUERY_REQUEST_DEDUPING === true) {
78
- didMakeNetworkRequest = true;
79
- }
70
+ didMakeNetworkRequest = true;
80
71
  var _fetchQueryDeduped$su = fetchQueryDeduped(environment, operation.request.identifier, fetchFn).subscribe({
81
72
  error: function error(err) {
82
73
  executionSubject.error(err);
@@ -399,8 +399,7 @@ function useFragmentInternal(fragmentNode, fragmentRef, hookDisplayName, queryOp
399
399
  if (isMissingData(state)) {
400
400
  var suspendingLiveResolvers = getSuspendingLiveResolver(state);
401
401
  if (suspendingLiveResolvers != null && suspendingLiveResolvers.length > 0) {
402
- throw Promise.all(suspendingLiveResolvers.map(function (_ref) {
403
- var liveStateID = _ref.liveStateID;
402
+ throw Promise.all(suspendingLiveResolvers.map(function (liveStateID) {
404
403
  return environment.getStore().getLiveResolverPromise(liveStateID);
405
404
  }));
406
405
  }
@@ -401,8 +401,7 @@ function useFragmentInternal_EXPERIMENTAL(fragmentNode, fragmentRef, hookDisplay
401
401
  if (isMissingData(state)) {
402
402
  var suspendingLiveResolvers = getSuspendingLiveResolver(state);
403
403
  if (suspendingLiveResolvers != null && suspendingLiveResolvers.length > 0) {
404
- throw Promise.all(suspendingLiveResolvers.map(function (_ref) {
405
- var liveStateID = _ref.liveStateID;
404
+ throw Promise.all(suspendingLiveResolvers.map(function (liveStateID) {
406
405
  return environment.getStore().getLiveResolverPromise(liveStateID);
407
406
  }));
408
407
  }
@@ -0,0 +1,227 @@
1
+ 'use strict';
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault")["default"];
4
+ var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
5
+ var useFragment = require('./useFragment');
6
+ var useLoadMoreFunction = require('./useLoadMoreFunction');
7
+ var useRefetchableFragmentInternal = require('./useRefetchableFragmentInternal');
8
+ var useRelayEnvironment = require('./useRelayEnvironment');
9
+ var useStaticFragmentNodeWarning = require('./useStaticFragmentNodeWarning');
10
+ var invariant = require('invariant');
11
+ var _require = require('react'),
12
+ useCallback = _require.useCallback,
13
+ useDebugValue = _require.useDebugValue,
14
+ useEffect = _require.useEffect,
15
+ useLayoutEffect = _require.useLayoutEffect,
16
+ useMemo = _require.useMemo,
17
+ useRef = _require.useRef,
18
+ useState = _require.useState;
19
+ var _require2 = require('relay-runtime'),
20
+ getFragment = _require2.getFragment,
21
+ getFragmentIdentifier = _require2.getFragmentIdentifier,
22
+ getPaginationMetadata = _require2.getPaginationMetadata;
23
+ var _require3 = require('relay-runtime'),
24
+ ConnectionInterface = _require3.ConnectionInterface,
25
+ getSelector = _require3.getSelector,
26
+ getValueAtPath = _require3.getValueAtPath;
27
+ function usePrefetchableForwardPaginationFragment_EXPERIMENTAL(fragmentInput, parentFragmentRef, bufferSize, initialSize, prefetchingLoadMoreOptions) {
28
+ var _fragmentInput$metada, _fragmentInput$metada2;
29
+ var minimalFetchSize = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : 1;
30
+ var fragmentNode = getFragment(fragmentInput);
31
+ useStaticFragmentNodeWarning(fragmentNode, 'first argument of usePrefetchableForwardPaginationFragment_EXPERIMENTAL()');
32
+ var componentDisplayName = 'usePrefetchableForwardPaginationFragment_EXPERIMENTAL()';
33
+ var _getPaginationMetadat = getPaginationMetadata(fragmentNode, componentDisplayName),
34
+ connectionPathInFragmentData = _getPaginationMetadat.connectionPathInFragmentData,
35
+ paginationRequest = _getPaginationMetadat.paginationRequest,
36
+ paginationMetadata = _getPaginationMetadat.paginationMetadata;
37
+ var _useRefetchableFragme = useRefetchableFragmentInternal(fragmentNode, parentFragmentRef, componentDisplayName),
38
+ fragmentData = _useRefetchableFragme.fragmentData,
39
+ fragmentRef = _useRefetchableFragme.fragmentRef,
40
+ refetch = _useRefetchableFragme.refetch;
41
+ var fragmentIdentifier = getFragmentIdentifier(fragmentNode, fragmentRef);
42
+ var edgeKeys = useMemo(function () {
43
+ var connection = getValueAtPath(fragmentData, connectionPathInFragmentData);
44
+ if (connection == null) {
45
+ return null;
46
+ }
47
+ var _ConnectionInterface$ = ConnectionInterface.get(),
48
+ EDGES = _ConnectionInterface$.EDGES;
49
+ return connection[EDGES];
50
+ }, [connectionPathInFragmentData, fragmentData]);
51
+ var sourceSize = edgeKeys == null ? -1 : edgeKeys.length;
52
+ var _useState = useState(initialSize != null ? initialSize : sourceSize),
53
+ _numInUse = _useState[0],
54
+ setNumInUse = _useState[1];
55
+ var numInUse = _numInUse;
56
+ if (_numInUse === -1 && sourceSize !== -1) {
57
+ numInUse = initialSize != null ? initialSize : sourceSize;
58
+ setNumInUse(numInUse);
59
+ }
60
+ var environment = useRelayEnvironment();
61
+ var _useState2 = useState(false),
62
+ isLoadingMore = _useState2[0],
63
+ reallySetIsLoadingMore = _useState2[1];
64
+ var _useState3 = useState(false),
65
+ isRefetching = _useState3[0],
66
+ setIsRefetching = _useState3[1];
67
+ var availableSizeRef = useRef(0);
68
+ var setIsLoadingMore = useCallback(function (value) {
69
+ var _environment$getSched;
70
+ var schedule = (_environment$getSched = environment.getScheduler()) === null || _environment$getSched === void 0 ? void 0 : _environment$getSched.schedule;
71
+ if (schedule) {
72
+ schedule(function () {
73
+ reallySetIsLoadingMore(value);
74
+ });
75
+ } else {
76
+ reallySetIsLoadingMore(value);
77
+ }
78
+ }, [environment]);
79
+ var isLoadingMoreRef = useRef(false);
80
+ var observer = useMemo(function () {
81
+ return {
82
+ start: function start() {
83
+ isLoadingMoreRef.current = true;
84
+ reallySetIsLoadingMore(true);
85
+ },
86
+ complete: function complete() {
87
+ isLoadingMoreRef.current = false;
88
+ setIsLoadingMore(false);
89
+ },
90
+ error: function error() {
91
+ isLoadingMoreRef.current = false;
92
+ setIsLoadingMore(false);
93
+ }
94
+ };
95
+ }, [setIsLoadingMore]);
96
+ var handleReset = useCallback(function () {
97
+ if (!isRefetching) {
98
+ var _environment$getSched2;
99
+ var schedule = (_environment$getSched2 = environment.getScheduler()) === null || _environment$getSched2 === void 0 ? void 0 : _environment$getSched2.schedule;
100
+ if (schedule) {
101
+ schedule(function () {
102
+ setNumInUse(-1);
103
+ });
104
+ } else {
105
+ setNumInUse(-1);
106
+ }
107
+ }
108
+ isLoadingMoreRef.current = false;
109
+ setIsLoadingMore(false);
110
+ }, [environment, isRefetching, setIsLoadingMore]);
111
+ var _useLoadMoreFunction = useLoadMoreFunction({
112
+ componentDisplayName: componentDisplayName,
113
+ connectionPathInFragmentData: connectionPathInFragmentData,
114
+ direction: 'forward',
115
+ fragmentData: fragmentData,
116
+ fragmentIdentifier: fragmentIdentifier,
117
+ fragmentNode: fragmentNode,
118
+ fragmentRef: fragmentRef,
119
+ paginationMetadata: paginationMetadata,
120
+ paginationRequest: paginationRequest,
121
+ observer: observer,
122
+ onReset: handleReset
123
+ }),
124
+ loadMore = _useLoadMoreFunction[0],
125
+ hasNext = _useLoadMoreFunction[1],
126
+ disposeFetchNext = _useLoadMoreFunction[2];
127
+ useLayoutEffect(function () {
128
+ availableSizeRef.current = sourceSize - numInUse;
129
+ }, [numInUse, sourceSize]);
130
+ var prefetchingUNSTABLE_extraVariables = prefetchingLoadMoreOptions === null || prefetchingLoadMoreOptions === void 0 ? void 0 : prefetchingLoadMoreOptions.UNSTABLE_extraVariables;
131
+ var prefetchingOnComplete = prefetchingLoadMoreOptions === null || prefetchingLoadMoreOptions === void 0 ? void 0 : prefetchingLoadMoreOptions.onComplete;
132
+ var showMore = useCallback(function (numToAdd, options) {
133
+ if (!isLoadingMoreRef.current || availableSizeRef.current >= 0) {
134
+ availableSizeRef.current -= numToAdd;
135
+ setNumInUse(function (lastNumInUse) {
136
+ return lastNumInUse + numToAdd;
137
+ });
138
+ if (!isLoadingMoreRef.current && availableSizeRef.current < 0) {
139
+ loadMore(Math.max(minimalFetchSize, Math.min(numToAdd, bufferSize - availableSizeRef.current)), options !== null && options !== void 0 ? options : {
140
+ onComplete: prefetchingOnComplete,
141
+ UNSTABLE_extraVariables: typeof prefetchingUNSTABLE_extraVariables === 'function' ? prefetchingUNSTABLE_extraVariables({
142
+ hasNext: hasNext,
143
+ data: fragmentData,
144
+ getServerEdges: function getServerEdges() {
145
+ var selector = getSelector(edgesFragment, edgeKeys);
146
+ if (selector == null) {
147
+ return [];
148
+ }
149
+ !(selector.kind === 'PluralReaderSelector') ? process.env.NODE_ENV !== "production" ? invariant(false, 'Expected a plural selector') : invariant(false) : void 0;
150
+ return selector.selectors.map(function (sel) {
151
+ return environment.lookup(sel).data;
152
+ });
153
+ }
154
+ }) : prefetchingUNSTABLE_extraVariables
155
+ });
156
+ }
157
+ }
158
+ }, [bufferSize, loadMore, minimalFetchSize, edgeKeys, fragmentData, prefetchingUNSTABLE_extraVariables, prefetchingOnComplete]);
159
+ var edgesFragment = (_fragmentInput$metada = fragmentInput.metadata) === null || _fragmentInput$metada === void 0 ? void 0 : (_fragmentInput$metada2 = _fragmentInput$metada.refetch) === null || _fragmentInput$metada2 === void 0 ? void 0 : _fragmentInput$metada2.edgesFragment;
160
+ !(edgesFragment != null) ? process.env.NODE_ENV !== "production" ? invariant(false, 'usePrefetchableForwardPaginationFragment_EXPERIMENTAL: Expected the edge fragment to be defined, ' + 'please make sure you have added `prefetchable_pagination: true` to `@connection`') : invariant(false) : void 0;
161
+ useEffect(function () {
162
+ if (!isLoadingMoreRef.current && !isLoadingMore && !isRefetching && hasNext && (sourceSize - numInUse < bufferSize || numInUse > sourceSize)) {
163
+ var onComplete = prefetchingOnComplete;
164
+ loadMore(Math.max(bufferSize - Math.max(sourceSize - numInUse, 0), numInUse - sourceSize, minimalFetchSize), {
165
+ onComplete: onComplete,
166
+ UNSTABLE_extraVariables: typeof prefetchingUNSTABLE_extraVariables === 'function' ? prefetchingUNSTABLE_extraVariables({
167
+ hasNext: hasNext,
168
+ data: fragmentData,
169
+ getServerEdges: function getServerEdges() {
170
+ var selector = getSelector(edgesFragment, edgeKeys);
171
+ if (selector == null) {
172
+ return [];
173
+ }
174
+ !(selector.kind === 'PluralReaderSelector') ? process.env.NODE_ENV !== "production" ? invariant(false, 'Expected a plural selector') : invariant(false) : void 0;
175
+ return selector.selectors.map(function (sel) {
176
+ return environment.lookup(sel).data;
177
+ });
178
+ }
179
+ }) : prefetchingUNSTABLE_extraVariables
180
+ });
181
+ }
182
+ }, [hasNext, bufferSize, isRefetching, loadMore, numInUse, prefetchingUNSTABLE_extraVariables, prefetchingOnComplete, sourceSize, edgeKeys, isLoadingMore, minimalFetchSize, environment, edgesFragment]);
183
+ var realNumInUse = Math.min(numInUse, sourceSize);
184
+ var derivedEdgeKeys = useMemo(function () {
185
+ var _edgeKeys$slice;
186
+ return (_edgeKeys$slice = edgeKeys === null || edgeKeys === void 0 ? void 0 : edgeKeys.slice(0, realNumInUse)) !== null && _edgeKeys$slice !== void 0 ? _edgeKeys$slice : [];
187
+ }, [edgeKeys, realNumInUse]);
188
+ var edges = useFragment(edgesFragment, derivedEdgeKeys);
189
+ var refetchPagination = useCallback(function (variables, options) {
190
+ disposeFetchNext();
191
+ setIsRefetching(true);
192
+ return refetch(variables, (0, _objectSpread2["default"])((0, _objectSpread2["default"])({}, options), {}, {
193
+ onComplete: function onComplete(maybeError) {
194
+ var _environment$getSched3, _options$onComplete;
195
+ var schedule = (_environment$getSched3 = environment.getScheduler()) === null || _environment$getSched3 === void 0 ? void 0 : _environment$getSched3.schedule;
196
+ if (schedule) {
197
+ schedule(function () {
198
+ setIsRefetching(false);
199
+ setNumInUse(-1);
200
+ });
201
+ } else {
202
+ setIsRefetching(false);
203
+ setNumInUse(-1);
204
+ }
205
+ options === null || options === void 0 ? void 0 : (_options$onComplete = options.onComplete) === null || _options$onComplete === void 0 ? void 0 : _options$onComplete.call(options, maybeError);
206
+ },
207
+ __environment: undefined
208
+ }));
209
+ }, [disposeFetchNext, environment, refetch]);
210
+ if (process.env.NODE_ENV !== "production") {
211
+ useDebugValue({
212
+ fragment: fragmentNode.name,
213
+ data: fragmentData,
214
+ hasNext: hasNext,
215
+ isLoadingNext: isLoadingMore
216
+ });
217
+ }
218
+ return {
219
+ edges: edges,
220
+ data: fragmentData,
221
+ loadNext: showMore,
222
+ hasNext: hasNext || sourceSize > numInUse,
223
+ isLoadingNext: isLoadingMore && numInUse > sourceSize,
224
+ refetch: refetchPagination
225
+ };
226
+ }
227
+ module.exports = usePrefetchableForwardPaginationFragment_EXPERIMENTAL;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "react-relay",
3
3
  "description": "A framework for building GraphQL-driven React applications.",
4
- "version": "18.1.0",
4
+ "version": "18.2.0",
5
5
  "keywords": [
6
6
  "graphql",
7
7
  "relay",
@@ -20,7 +20,7 @@
20
20
  "fbjs": "^3.0.2",
21
21
  "invariant": "^2.2.4",
22
22
  "nullthrows": "^1.1.1",
23
- "relay-runtime": "18.1.0"
23
+ "relay-runtime": "18.2.0"
24
24
  },
25
25
  "peerDependencies": {
26
26
  "react": "^16.9.0 || ^17 || ^18"