relay-runtime 11.0.0-rc.0 → 12.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 (128) hide show
  1. package/handlers/connection/ConnectionHandler.js.flow +7 -0
  2. package/handlers/connection/MutationHandlers.js.flow +28 -0
  3. package/index.js +1 -1
  4. package/index.js.flow +20 -3
  5. package/lib/handlers/RelayDefaultHandlerProvider.js +1 -1
  6. package/lib/handlers/connection/ConnectionHandler.js +12 -6
  7. package/lib/handlers/connection/MutationHandlers.js +67 -8
  8. package/lib/index.js +15 -0
  9. package/lib/multi-actor-environment/ActorIdentifier.js +33 -0
  10. package/lib/multi-actor-environment/ActorSpecificEnvironment.js +148 -0
  11. package/lib/multi-actor-environment/ActorUtils.js +27 -0
  12. package/lib/multi-actor-environment/MultiActorEnvironment.js +406 -0
  13. package/lib/multi-actor-environment/MultiActorEnvironmentTypes.js +11 -0
  14. package/lib/multi-actor-environment/index.js +21 -0
  15. package/lib/mutations/RelayRecordProxy.js +1 -1
  16. package/lib/mutations/RelayRecordSourceMutator.js +1 -1
  17. package/lib/mutations/RelayRecordSourceProxy.js +1 -1
  18. package/lib/mutations/RelayRecordSourceSelectorProxy.js +7 -2
  19. package/lib/mutations/applyOptimisticMutation.js +1 -1
  20. package/lib/mutations/commitMutation.js +5 -2
  21. package/lib/mutations/validateMutation.js +39 -17
  22. package/lib/network/RelayNetwork.js +1 -1
  23. package/lib/network/RelayObservable.js +3 -1
  24. package/lib/network/RelayQueryResponseCache.js +20 -3
  25. package/lib/network/wrapNetworkWithLogObserver.js +78 -0
  26. package/lib/query/GraphQLTag.js +1 -1
  27. package/lib/query/fetchQuery.js +1 -1
  28. package/lib/query/fetchQueryInternal.js +1 -1
  29. package/lib/store/DataChecker.js +132 -50
  30. package/lib/store/{RelayModernQueryExecutor.js → OperationExecutor.js} +524 -187
  31. package/lib/store/RelayConcreteVariables.js +29 -4
  32. package/lib/store/RelayModernEnvironment.js +137 -220
  33. package/lib/store/RelayModernFragmentSpecResolver.js +49 -23
  34. package/lib/store/RelayModernRecord.js +36 -2
  35. package/lib/store/RelayModernSelector.js +1 -1
  36. package/lib/store/RelayModernStore.js +53 -22
  37. package/lib/store/RelayOperationTracker.js +34 -24
  38. package/lib/store/RelayPublishQueue.js +30 -8
  39. package/lib/store/RelayReader.js +177 -29
  40. package/lib/store/RelayRecordSource.js +87 -3
  41. package/lib/store/RelayReferenceMarker.js +53 -28
  42. package/lib/store/RelayResponseNormalizer.js +247 -108
  43. package/lib/store/RelayStoreReactFlightUtils.js +7 -11
  44. package/lib/store/RelayStoreSubscriptions.js +8 -5
  45. package/lib/store/RelayStoreUtils.js +10 -4
  46. package/lib/store/ResolverCache.js +213 -0
  47. package/lib/store/ResolverFragments.js +57 -0
  48. package/lib/store/cloneRelayHandleSourceField.js +1 -1
  49. package/lib/store/cloneRelayScalarHandleSourceField.js +1 -1
  50. package/lib/store/createRelayContext.js +2 -2
  51. package/lib/store/defaultGetDataID.js +3 -1
  52. package/lib/store/readInlineData.js +1 -1
  53. package/lib/subscription/requestSubscription.js +32 -6
  54. package/lib/util/RelayConcreteNode.js +3 -0
  55. package/lib/util/RelayFeatureFlags.js +5 -4
  56. package/lib/util/RelayProfiler.js +17 -187
  57. package/lib/util/RelayReplaySubject.js +22 -7
  58. package/lib/util/deepFreeze.js +1 -0
  59. package/lib/util/getPaginationMetadata.js +41 -0
  60. package/lib/util/getPaginationVariables.js +67 -0
  61. package/lib/util/getPendingOperationsForFragment.js +55 -0
  62. package/lib/util/getRefetchMetadata.js +36 -0
  63. package/lib/util/getRelayHandleKey.js +1 -1
  64. package/lib/util/getRequestIdentifier.js +1 -1
  65. package/lib/util/getValueAtPath.js +51 -0
  66. package/lib/util/isEmptyObject.js +1 -1
  67. package/lib/util/registerEnvironmentWithDevTools.js +26 -0
  68. package/lib/util/withDuration.js +31 -0
  69. package/multi-actor-environment/ActorIdentifier.js.flow +43 -0
  70. package/multi-actor-environment/ActorSpecificEnvironment.js.flow +217 -0
  71. package/multi-actor-environment/ActorUtils.js.flow +33 -0
  72. package/multi-actor-environment/MultiActorEnvironment.js.flow +485 -0
  73. package/multi-actor-environment/MultiActorEnvironmentTypes.js.flow +245 -0
  74. package/multi-actor-environment/index.js.flow +27 -0
  75. package/mutations/RelayRecordSourceSelectorProxy.js.flow +7 -2
  76. package/mutations/commitMutation.js.flow +3 -1
  77. package/mutations/validateMutation.js.flow +42 -16
  78. package/network/RelayNetworkTypes.js.flow +17 -8
  79. package/network/RelayObservable.js.flow +2 -0
  80. package/network/RelayQueryResponseCache.js.flow +31 -17
  81. package/network/wrapNetworkWithLogObserver.js.flow +99 -0
  82. package/package.json +3 -2
  83. package/relay-runtime.js +2 -2
  84. package/relay-runtime.min.js +2 -2
  85. package/store/ClientID.js.flow +5 -1
  86. package/store/DataChecker.js.flow +148 -44
  87. package/store/{RelayModernQueryExecutor.js.flow → OperationExecutor.js.flow} +578 -237
  88. package/store/RelayConcreteVariables.js.flow +31 -1
  89. package/store/RelayModernEnvironment.js.flow +132 -220
  90. package/store/RelayModernFragmentSpecResolver.js.flow +40 -14
  91. package/store/RelayModernOperationDescriptor.js.flow +9 -3
  92. package/store/RelayModernRecord.js.flow +49 -0
  93. package/store/RelayModernStore.js.flow +57 -17
  94. package/store/RelayOperationTracker.js.flow +56 -34
  95. package/store/RelayPublishQueue.js.flow +37 -11
  96. package/store/RelayReader.js.flow +186 -27
  97. package/store/RelayRecordSource.js.flow +72 -6
  98. package/store/RelayReferenceMarker.js.flow +51 -21
  99. package/store/RelayResponseNormalizer.js.flow +251 -67
  100. package/store/RelayStoreReactFlightUtils.js.flow +6 -9
  101. package/store/RelayStoreSubscriptions.js.flow +10 -3
  102. package/store/RelayStoreTypes.js.flow +144 -21
  103. package/store/RelayStoreUtils.js.flow +19 -4
  104. package/store/ResolverCache.js.flow +247 -0
  105. package/store/ResolverFragments.js.flow +128 -0
  106. package/store/createRelayContext.js.flow +1 -1
  107. package/store/defaultGetDataID.js.flow +3 -1
  108. package/subscription/requestSubscription.js.flow +43 -8
  109. package/util/NormalizationNode.js.flow +16 -3
  110. package/util/ReaderNode.js.flow +29 -2
  111. package/util/RelayConcreteNode.js.flow +3 -0
  112. package/util/RelayFeatureFlags.js.flow +10 -6
  113. package/util/RelayProfiler.js.flow +22 -194
  114. package/util/RelayReplaySubject.js.flow +7 -6
  115. package/util/RelayRuntimeTypes.js.flow +4 -2
  116. package/util/deepFreeze.js.flow +2 -1
  117. package/util/getPaginationMetadata.js.flow +74 -0
  118. package/util/getPaginationVariables.js.flow +112 -0
  119. package/util/getPendingOperationsForFragment.js.flow +62 -0
  120. package/util/getRefetchMetadata.js.flow +80 -0
  121. package/util/getValueAtPath.js.flow +46 -0
  122. package/util/isEmptyObject.js.flow +2 -1
  123. package/util/registerEnvironmentWithDevTools.js.flow +33 -0
  124. package/util/withDuration.js.flow +32 -0
  125. package/lib/store/RelayRecordSourceMapImpl.js +0 -107
  126. package/lib/store/RelayStoreSubscriptionsUsingMapByID.js +0 -318
  127. package/store/RelayRecordSourceMapImpl.js.flow +0 -91
  128. package/store/RelayStoreSubscriptionsUsingMapByID.js.flow +0 -283
@@ -42,7 +42,7 @@ class RelayReplaySubject<T> {
42
42
  _events: Array<Event<T>> = [];
43
43
  _sinks: Set<Sink<T>> = new Set();
44
44
  _observable: RelayObservable<T>;
45
- _subscription: ?Subscription = null;
45
+ _subscription: Array<Subscription> = [];
46
46
 
47
47
  constructor() {
48
48
  this._observable = RelayObservable.create(sink => {
@@ -116,15 +116,16 @@ class RelayReplaySubject<T> {
116
116
  }
117
117
 
118
118
  subscribe(observer: Observer<T> | Sink<T>): Subscription {
119
- this._subscription = this._observable.subscribe(observer);
120
- return this._subscription;
119
+ const subscription = this._observable.subscribe(observer);
120
+ this._subscription.push(subscription);
121
+ return subscription;
121
122
  }
122
123
 
123
124
  unsubscribe() {
124
- if (this._subscription) {
125
- this._subscription.unsubscribe();
126
- this._subscription = null;
125
+ for (const subscription of this._subscription) {
126
+ subscription.unsubscribe();
127
127
  }
128
+ this._subscription = [];
128
129
  }
129
130
 
130
131
  getObserverCount(): number {
@@ -21,12 +21,12 @@
21
21
  * use-case is as a return value for subscriptions, where calling `dispose()`
22
22
  * would cancel the subscription.
23
23
  */
24
- export type Disposable = {dispose(): void, ...};
24
+ export type Disposable = interface {dispose(): void};
25
25
 
26
26
  export type DataID = string;
27
27
 
28
28
  // Variables
29
- export type Variables = {+[string]: $FlowFixMe, ...};
29
+ export type Variables = {+[string]: $FlowFixMe};
30
30
 
31
31
  /**
32
32
  * Generated operation flow types are subtypes of this.
@@ -49,6 +49,7 @@ export type VariablesOf<T: OperationType> = $ElementType<T, 'variables'>;
49
49
  * in milliseconds. (This value will be passed to setTimeout.)
50
50
  * - `liveConfigId`: causes a query to live update by calling GraphQLLiveQuery,
51
51
  * it represents a configuration of gateway when doing live query
52
+ * - `onSubscribe`: Not in use.
52
53
  * - `metadata`: user-supplied metadata.
53
54
  * - `transactionId`: a user-supplied value, intended for use as a unique id for
54
55
  * a given instance of executing an operation.
@@ -57,6 +58,7 @@ export type CacheConfig = {|
57
58
  force?: ?boolean,
58
59
  poll?: ?number,
59
60
  liveConfigId?: ?string,
61
+ onSubscribe?: () => void,
60
62
  metadata?: {[key: string]: mixed, ...},
61
63
  transactionId?: ?string,
62
64
  |};
@@ -18,9 +18,10 @@
18
18
  * For convenience, and for consistency with the behavior of `Object.freeze`,
19
19
  * returns the now-frozen original object.
20
20
  */
21
- function deepFreeze<T: {...}>(object: T): T {
21
+ function deepFreeze<T: interface {}>(object: T): T {
22
22
  Object.freeze(object);
23
23
  Object.getOwnPropertyNames(object).forEach(name => {
24
+ // $FlowFixMe[prop-missing]
24
25
  const property = object[name];
25
26
  if (
26
27
  property &&
@@ -0,0 +1,74 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its 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
+ * @flow strict-local
9
+ * @format
10
+ */
11
+
12
+ // flowlint ambiguous-object-type:error
13
+
14
+ 'use strict';
15
+
16
+ const getRefetchMetadata = require('./getRefetchMetadata');
17
+ const invariant = require('invariant');
18
+
19
+ import type {
20
+ ConcreteRequest,
21
+ ReaderFragment,
22
+ ReaderPaginationMetadata,
23
+ } from 'relay-runtime';
24
+
25
+ function getPaginationMetadata(
26
+ fragmentNode: ReaderFragment,
27
+ componentDisplayName: string,
28
+ ): {|
29
+ connectionPathInFragmentData: $ReadOnlyArray<string | number>,
30
+ identifierField: ?string,
31
+ paginationRequest: ConcreteRequest,
32
+ paginationMetadata: ReaderPaginationMetadata,
33
+ stream: boolean,
34
+ |} {
35
+ const {
36
+ refetchableRequest: paginationRequest,
37
+ refetchMetadata,
38
+ } = getRefetchMetadata(fragmentNode, componentDisplayName);
39
+
40
+ const paginationMetadata = refetchMetadata.connection;
41
+ invariant(
42
+ paginationMetadata != null,
43
+ 'Relay: getPaginationMetadata(): Expected fragment `%s` to include a ' +
44
+ 'connection when using `%s`. Did you forget to add a @connection ' +
45
+ 'directive to the connection field in the fragment?',
46
+ componentDisplayName,
47
+ fragmentNode.name,
48
+ );
49
+ const connectionPathInFragmentData = paginationMetadata.path;
50
+
51
+ const connectionMetadata = (fragmentNode.metadata?.connection ?? [])[0];
52
+ invariant(
53
+ connectionMetadata != null,
54
+ 'Relay: getPaginationMetadata(): Expected fragment `%s` to include a ' +
55
+ 'connection when using `%s`. Did you forget to add a @connection ' +
56
+ 'directive to the connection field in the fragment?',
57
+ componentDisplayName,
58
+ fragmentNode.name,
59
+ );
60
+ const identifierField = refetchMetadata.identifierField;
61
+ invariant(
62
+ identifierField == null || typeof identifierField === 'string',
63
+ 'Relay: getRefetchMetadata(): Expected `identifierField` to be a string.',
64
+ );
65
+ return {
66
+ connectionPathInFragmentData,
67
+ identifierField,
68
+ paginationRequest,
69
+ paginationMetadata,
70
+ stream: connectionMetadata.stream === true,
71
+ };
72
+ }
73
+
74
+ module.exports = getPaginationMetadata;
@@ -0,0 +1,112 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its 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
+ * @flow strict-local
9
+ * @format
10
+ */
11
+
12
+ // flowlint ambiguous-object-type:error
13
+
14
+ 'use strict';
15
+
16
+ const invariant = require('invariant');
17
+ const warning = require('warning');
18
+
19
+ import type {ReaderPaginationMetadata} from './ReaderNode';
20
+ import type {Variables} from './RelayRuntimeTypes';
21
+
22
+ export type Direction = 'forward' | 'backward';
23
+
24
+ function getPaginationVariables(
25
+ direction: Direction,
26
+ count: number,
27
+ cursor: ?string,
28
+ baseVariables: Variables,
29
+ extraVariables: Variables,
30
+ paginationMetadata: ReaderPaginationMetadata,
31
+ ): {[string]: mixed, ...} {
32
+ const {
33
+ backward: backwardMetadata,
34
+ forward: forwardMetadata,
35
+ } = paginationMetadata;
36
+
37
+ if (direction === 'backward') {
38
+ invariant(
39
+ backwardMetadata != null &&
40
+ backwardMetadata.count != null &&
41
+ backwardMetadata.cursor != null,
42
+ 'Relay: Expected backward pagination metadata to be available. ' +
43
+ "If you're seeing this, this is likely a bug in Relay.",
44
+ );
45
+ warning(
46
+ !extraVariables.hasOwnProperty(backwardMetadata.cursor),
47
+ 'Relay: `UNSTABLE_extraVariables` provided by caller should not ' +
48
+ 'contain cursor variable `%s`. This variable is automatically ' +
49
+ 'determined by Relay.',
50
+ backwardMetadata.cursor,
51
+ );
52
+ warning(
53
+ !extraVariables.hasOwnProperty(backwardMetadata.count),
54
+ 'Relay: `UNSTABLE_extraVariables` provided by caller should not ' +
55
+ 'contain count variable `%s`. This variable is automatically ' +
56
+ 'determined by Relay.',
57
+ backwardMetadata.count,
58
+ );
59
+ // $FlowFixMe[cannot-spread-interface]
60
+ const paginationVariables = {
61
+ ...baseVariables,
62
+ ...extraVariables,
63
+ [backwardMetadata.cursor]: cursor,
64
+ [backwardMetadata.count]: count,
65
+ };
66
+ if (forwardMetadata && forwardMetadata.cursor) {
67
+ paginationVariables[forwardMetadata.cursor] = null;
68
+ }
69
+ if (forwardMetadata && forwardMetadata.count) {
70
+ paginationVariables[forwardMetadata.count] = null;
71
+ }
72
+ return paginationVariables;
73
+ }
74
+
75
+ invariant(
76
+ forwardMetadata != null &&
77
+ forwardMetadata.count != null &&
78
+ forwardMetadata.cursor != null,
79
+ 'Relay: Expected forward pagination metadata to be available. ' +
80
+ "If you're seeing this, this is likely a bug in Relay.",
81
+ );
82
+ warning(
83
+ !extraVariables.hasOwnProperty(forwardMetadata.cursor),
84
+ 'Relay: `UNSTABLE_extraVariables` provided by caller should not ' +
85
+ 'contain cursor variable `%s`. This variable is automatically ' +
86
+ 'determined by Relay.',
87
+ forwardMetadata.cursor,
88
+ );
89
+ warning(
90
+ !extraVariables.hasOwnProperty(forwardMetadata.count),
91
+ 'Relay: `UNSTABLE_extraVariables` provided by caller should not ' +
92
+ 'contain count variable `%s`. This variable is automatically ' +
93
+ 'determined by Relay.',
94
+ forwardMetadata.count,
95
+ );
96
+ // $FlowFixMe[cannot-spread-interface]
97
+ const paginationVariables = {
98
+ ...baseVariables,
99
+ ...extraVariables,
100
+ [forwardMetadata.cursor]: cursor,
101
+ [forwardMetadata.count]: count,
102
+ };
103
+ if (backwardMetadata && backwardMetadata.cursor) {
104
+ paginationVariables[backwardMetadata.cursor] = null;
105
+ }
106
+ if (backwardMetadata && backwardMetadata.count) {
107
+ paginationVariables[backwardMetadata.count] = null;
108
+ }
109
+ return paginationVariables;
110
+ }
111
+
112
+ module.exports = getPaginationVariables;
@@ -0,0 +1,62 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its 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
+ * @flow strict-local
9
+ * @format
10
+ */
11
+
12
+ // flowlint ambiguous-object-type:error
13
+
14
+ 'use strict';
15
+
16
+ const {getPromiseForActiveRequest} = require('../query/fetchQueryInternal');
17
+
18
+ import type {IEnvironment, RequestDescriptor} from '../store/RelayStoreTypes';
19
+ import type {ReaderFragment} from './ReaderNode';
20
+
21
+ function getPendingOperationsForFragment(
22
+ environment: IEnvironment,
23
+ fragmentNode: ReaderFragment,
24
+ fragmentOwner: RequestDescriptor,
25
+ ): {|
26
+ promise: Promise<void>,
27
+ pendingOperations: $ReadOnlyArray<RequestDescriptor>,
28
+ |} | null {
29
+ let pendingOperations: $ReadOnlyArray<RequestDescriptor> = [];
30
+ let promise = getPromiseForActiveRequest(environment, fragmentOwner);
31
+
32
+ if (promise != null) {
33
+ pendingOperations = [fragmentOwner];
34
+ } else {
35
+ const result = environment
36
+ .getOperationTracker()
37
+ .getPendingOperationsAffectingOwner(fragmentOwner);
38
+
39
+ pendingOperations = result?.pendingOperations ?? [];
40
+ promise = result?.promise ?? null;
41
+ }
42
+
43
+ if (!promise) {
44
+ return null;
45
+ }
46
+
47
+ let pendingOperationName =
48
+ pendingOperations?.map(op => op.node.params.name).join(',') ?? null;
49
+ if (pendingOperationName == null || pendingOperationName.length === 0) {
50
+ pendingOperationName = 'Unknown pending operation';
51
+ }
52
+ const fragmentName = fragmentNode.name;
53
+ const promiseDisplayName =
54
+ pendingOperationName === fragmentName
55
+ ? `Relay(${pendingOperationName})`
56
+ : `Relay(${pendingOperationName}:${fragmentName})`;
57
+ // $FlowExpectedError[prop-missing] Expando to annotate Promises.
58
+ promise.displayName = promiseDisplayName;
59
+ return {promise, pendingOperations};
60
+ }
61
+
62
+ module.exports = getPendingOperationsForFragment;
@@ -0,0 +1,80 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its 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
+ * @flow strict-local
9
+ * @format
10
+ */
11
+
12
+ // flowlint ambiguous-object-type:error
13
+
14
+ 'use strict';
15
+
16
+ const invariant = require('invariant');
17
+
18
+ import type {
19
+ ConcreteRequest,
20
+ ReaderFragment,
21
+ ReaderRefetchMetadata,
22
+ } from 'relay-runtime';
23
+
24
+ function getRefetchMetadata(
25
+ fragmentNode: ReaderFragment,
26
+ componentDisplayName: string,
27
+ ): {|
28
+ fragmentRefPathInResponse: $ReadOnlyArray<string | number>,
29
+ identifierField: ?string,
30
+ refetchableRequest: ConcreteRequest,
31
+ refetchMetadata: ReaderRefetchMetadata,
32
+ |} {
33
+ invariant(
34
+ fragmentNode.metadata?.plural !== true,
35
+ 'Relay: getRefetchMetadata(): Expected fragment `%s` not to be plural when using ' +
36
+ '`%s`. Remove `@relay(plural: true)` from fragment `%s` ' +
37
+ 'in order to use it with `%s`.',
38
+ fragmentNode.name,
39
+ componentDisplayName,
40
+ fragmentNode.name,
41
+ componentDisplayName,
42
+ );
43
+
44
+ const refetchMetadata = fragmentNode.metadata?.refetch;
45
+ invariant(
46
+ refetchMetadata != null,
47
+ 'Relay: getRefetchMetadata(): Expected fragment `%s` to be refetchable when using `%s`. ' +
48
+ 'Did you forget to add a @refetchable directive to the fragment?',
49
+ componentDisplayName,
50
+ fragmentNode.name,
51
+ );
52
+
53
+ // handle both commonjs and es modules
54
+ const refetchableRequest:
55
+ | ConcreteRequest
56
+ | string = (refetchMetadata: $FlowFixMe).operation.default
57
+ ? (refetchMetadata: $FlowFixMe).operation.default
58
+ : refetchMetadata.operation;
59
+ const fragmentRefPathInResponse = refetchMetadata.fragmentPathInResult;
60
+ invariant(
61
+ typeof refetchableRequest !== 'string',
62
+ 'Relay: getRefetchMetadata(): Expected refetch query to be an ' +
63
+ "operation and not a string when using `%s`. If you're seeing this, " +
64
+ 'this is likely a bug in Relay.',
65
+ componentDisplayName,
66
+ );
67
+ const identifierField = refetchMetadata.identifierField;
68
+ invariant(
69
+ identifierField == null || typeof identifierField === 'string',
70
+ 'Relay: getRefetchMetadata(): Expected `identifierField` to be a string.',
71
+ );
72
+ return {
73
+ fragmentRefPathInResponse,
74
+ identifierField,
75
+ refetchableRequest,
76
+ refetchMetadata,
77
+ };
78
+ }
79
+
80
+ module.exports = getRefetchMetadata;
@@ -0,0 +1,46 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its 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
+ * @flow strict-local
9
+ * @format
10
+ */
11
+
12
+ // flowlint ambiguous-object-type:error
13
+
14
+ 'use strict';
15
+
16
+ const invariant = require('invariant');
17
+
18
+ function getValueAtPath(
19
+ data: mixed,
20
+ path: $ReadOnlyArray<string | number>,
21
+ ): mixed {
22
+ let result = data;
23
+ for (const key of path) {
24
+ if (result == null) {
25
+ return null;
26
+ }
27
+ if (typeof key === 'number') {
28
+ invariant(
29
+ Array.isArray(result),
30
+ 'Relay: Expected an array when extracting value at path. ' +
31
+ "If you're seeing this, this is likely a bug in Relay.",
32
+ );
33
+ result = result[key];
34
+ } else {
35
+ invariant(
36
+ typeof result === 'object' && !Array.isArray(result),
37
+ 'Relay: Expected an object when extracting value at path. ' +
38
+ "If you're seeing this, this is likely a bug in Relay.",
39
+ );
40
+ result = result[key];
41
+ }
42
+ }
43
+ return result;
44
+ }
45
+
46
+ module.exports = getValueAtPath;
@@ -11,9 +11,10 @@
11
11
 
12
12
  'use strict';
13
13
 
14
+ // $FlowFixMe[method-unbinding] added when improving typing for this parameters
14
15
  const hasOwnProperty = Object.prototype.hasOwnProperty;
15
16
 
16
- function isEmptyObject(obj: {+[key: string]: mixed}): boolean {
17
+ function isEmptyObject(obj: interface {+[key: string]: mixed}): boolean {
17
18
  for (const key in obj) {
18
19
  if (hasOwnProperty.call(obj, key)) {
19
20
  return false;
@@ -0,0 +1,33 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its 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
+ * @flow strict-local
8
+ * @format
9
+ * @emails oncall+relay
10
+ */
11
+
12
+ // flowlint ambiguous-object-type:error
13
+
14
+ 'use strict';
15
+
16
+ import type {IEnvironment} from '../store/RelayStoreTypes';
17
+
18
+ function registerEnvironmentWithDevTools(environment: IEnvironment): void {
19
+ // Register this Relay Environment with Relay DevTools if it exists.
20
+ // Note: this must always be the last step in the constructor.
21
+ const _global =
22
+ typeof global !== 'undefined'
23
+ ? global
24
+ : typeof window !== 'undefined'
25
+ ? window
26
+ : undefined;
27
+ const devToolsHook = _global && _global.__RELAY_DEVTOOLS_HOOK__;
28
+ if (devToolsHook) {
29
+ devToolsHook.registerEnvironment(environment);
30
+ }
31
+ }
32
+
33
+ module.exports = registerEnvironmentWithDevTools;
@@ -0,0 +1,32 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its 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
+ * @flow
8
+ * @format
9
+ */
10
+
11
+ // flowlint ambiguous-object-type:error
12
+
13
+ 'use strict';
14
+
15
+ const isPerformanceNowAvailable =
16
+ typeof window !== 'undefined' &&
17
+ typeof window?.performance?.now === 'function';
18
+
19
+ function currentTimestamp(): number {
20
+ if (isPerformanceNowAvailable) {
21
+ return window.performance.now();
22
+ }
23
+ return Date.now();
24
+ }
25
+
26
+ function withDuration<T>(cb: () => T): [number, T] {
27
+ const startTime = currentTimestamp();
28
+ const result = cb();
29
+ return [currentTimestamp() - startTime, result];
30
+ }
31
+
32
+ module.exports = withDuration;
@@ -1,107 +0,0 @@
1
- /**
2
- * Copyright (c) Facebook, Inc. and its 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
- */
10
- // flowlint ambiguous-object-type:error
11
- 'use strict';
12
-
13
- var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
14
-
15
- var _createForOfIteratorHelper2 = _interopRequireDefault(require("@babel/runtime/helpers/createForOfIteratorHelper"));
16
-
17
- var RelayRecordState = require('./RelayRecordState');
18
-
19
- var EXISTENT = RelayRecordState.EXISTENT,
20
- NONEXISTENT = RelayRecordState.NONEXISTENT,
21
- UNKNOWN = RelayRecordState.UNKNOWN;
22
- /**
23
- * An implementation of the `MutableRecordSource` interface (defined in
24
- * `RelayStoreTypes`) that holds all records in memory (JS Map).
25
- */
26
-
27
- var RelayMapRecordSourceMapImpl = /*#__PURE__*/function () {
28
- function RelayMapRecordSourceMapImpl(records) {
29
- var _this = this;
30
-
31
- this._records = new Map();
32
-
33
- if (records != null) {
34
- Object.keys(records).forEach(function (key) {
35
- _this._records.set(key, records[key]);
36
- });
37
- }
38
- }
39
-
40
- var _proto = RelayMapRecordSourceMapImpl.prototype;
41
-
42
- _proto.clear = function clear() {
43
- this._records = new Map();
44
- };
45
-
46
- _proto["delete"] = function _delete(dataID) {
47
- this._records.set(dataID, null);
48
- };
49
-
50
- _proto.get = function get(dataID) {
51
- return this._records.get(dataID);
52
- };
53
-
54
- _proto.getRecordIDs = function getRecordIDs() {
55
- return Array.from(this._records.keys());
56
- };
57
-
58
- _proto.getStatus = function getStatus(dataID) {
59
- if (!this._records.has(dataID)) {
60
- return UNKNOWN;
61
- }
62
-
63
- return this._records.get(dataID) == null ? NONEXISTENT : EXISTENT;
64
- };
65
-
66
- _proto.has = function has(dataID) {
67
- return this._records.has(dataID);
68
- };
69
-
70
- _proto.remove = function remove(dataID) {
71
- this._records["delete"](dataID);
72
- };
73
-
74
- _proto.set = function set(dataID, record) {
75
- this._records.set(dataID, record);
76
- };
77
-
78
- _proto.size = function size() {
79
- return this._records.size;
80
- };
81
-
82
- _proto.toJSON = function toJSON() {
83
- var obj = {};
84
-
85
- var _iterator = (0, _createForOfIteratorHelper2["default"])(this._records),
86
- _step;
87
-
88
- try {
89
- for (_iterator.s(); !(_step = _iterator.n()).done;) {
90
- var _step$value = _step.value,
91
- key = _step$value[0],
92
- value = _step$value[1];
93
- obj[key] = value;
94
- }
95
- } catch (err) {
96
- _iterator.e(err);
97
- } finally {
98
- _iterator.f();
99
- }
100
-
101
- return obj;
102
- };
103
-
104
- return RelayMapRecordSourceMapImpl;
105
- }();
106
-
107
- module.exports = RelayMapRecordSourceMapImpl;