relay-runtime 11.0.2 → 13.0.0-rc.2

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 (219) hide show
  1. package/README.md +67 -0
  2. package/handlers/RelayDefaultHandlerProvider.js.flow +3 -3
  3. package/handlers/connection/ConnectionHandler.js.flow +9 -18
  4. package/handlers/connection/ConnectionInterface.js.flow +1 -1
  5. package/handlers/connection/MutationHandlers.js.flow +8 -12
  6. package/index.js +1 -1
  7. package/index.js.flow +57 -35
  8. package/lib/handlers/RelayDefaultHandlerProvider.js +1 -1
  9. package/lib/handlers/connection/ConnectionHandler.js +13 -19
  10. package/lib/handlers/connection/ConnectionInterface.js +1 -1
  11. package/lib/handlers/connection/MutationHandlers.js +4 -7
  12. package/lib/index.js +59 -44
  13. package/lib/multi-actor-environment/ActorIdentifier.js +12 -2
  14. package/lib/multi-actor-environment/ActorSpecificEnvironment.js +64 -20
  15. package/lib/multi-actor-environment/ActorUtils.js +27 -0
  16. package/lib/multi-actor-environment/MultiActorEnvironment.js +324 -61
  17. package/lib/multi-actor-environment/MultiActorEnvironmentTypes.js +1 -1
  18. package/lib/multi-actor-environment/index.js +6 -2
  19. package/lib/mutations/RelayDeclarativeMutationConfig.js +5 -2
  20. package/lib/mutations/RelayRecordProxy.js +4 -3
  21. package/lib/mutations/RelayRecordSourceMutator.js +4 -3
  22. package/lib/mutations/RelayRecordSourceProxy.js +13 -5
  23. package/lib/mutations/RelayRecordSourceSelectorProxy.js +19 -6
  24. package/lib/mutations/applyOptimisticMutation.js +7 -7
  25. package/lib/mutations/commitLocalUpdate.js +1 -1
  26. package/lib/mutations/commitMutation.js +15 -11
  27. package/lib/mutations/readUpdatableQuery_EXPERIMENTAL.js +242 -0
  28. package/lib/mutations/validateMutation.js +11 -6
  29. package/lib/network/ConvertToExecuteFunction.js +3 -2
  30. package/lib/network/RelayNetwork.js +4 -3
  31. package/lib/network/RelayNetworkTypes.js +1 -1
  32. package/lib/network/RelayObservable.js +1 -1
  33. package/lib/network/RelayQueryResponseCache.js +22 -6
  34. package/lib/network/wrapNetworkWithLogObserver.js +79 -0
  35. package/lib/query/GraphQLTag.js +3 -2
  36. package/lib/query/PreloadableQueryRegistry.js +1 -1
  37. package/lib/query/fetchQuery.js +7 -6
  38. package/lib/query/fetchQueryInternal.js +1 -1
  39. package/lib/query/fetchQuery_DEPRECATED.js +3 -2
  40. package/lib/store/ClientID.js +8 -2
  41. package/lib/store/DataChecker.js +124 -55
  42. package/lib/store/OperationExecutor.js +489 -215
  43. package/lib/store/RelayConcreteVariables.js +27 -9
  44. package/lib/store/RelayExperimentalGraphResponseHandler.js +153 -0
  45. package/lib/store/RelayExperimentalGraphResponseTransform.js +391 -0
  46. package/lib/store/RelayModernEnvironment.js +100 -120
  47. package/lib/store/RelayModernFragmentSpecResolver.js +53 -27
  48. package/lib/store/RelayModernOperationDescriptor.js +3 -2
  49. package/lib/store/RelayModernRecord.js +48 -13
  50. package/lib/store/RelayModernSelector.js +15 -9
  51. package/lib/store/RelayModernStore.js +56 -23
  52. package/lib/store/RelayOperationTracker.js +34 -24
  53. package/lib/store/RelayOptimisticRecordSource.js +1 -1
  54. package/lib/store/RelayPublishQueue.js +35 -11
  55. package/lib/store/RelayReader.js +257 -72
  56. package/lib/store/RelayRecordSource.js +88 -4
  57. package/lib/store/RelayRecordState.js +1 -1
  58. package/lib/store/RelayReferenceMarker.js +34 -22
  59. package/lib/store/RelayResponseNormalizer.js +172 -96
  60. package/lib/store/RelayStoreReactFlightUtils.js +5 -11
  61. package/lib/store/RelayStoreSubscriptions.js +15 -10
  62. package/lib/store/RelayStoreTypes.js +1 -1
  63. package/lib/store/RelayStoreUtils.js +13 -8
  64. package/lib/store/ResolverCache.js +213 -0
  65. package/lib/store/ResolverFragments.js +10 -6
  66. package/lib/store/StoreInspector.js +1 -1
  67. package/lib/store/TypeID.js +1 -1
  68. package/lib/store/ViewerPattern.js +1 -1
  69. package/lib/store/cloneRelayHandleSourceField.js +6 -5
  70. package/lib/store/cloneRelayScalarHandleSourceField.js +6 -5
  71. package/lib/store/createFragmentSpecResolver.js +1 -1
  72. package/lib/store/createRelayContext.js +5 -3
  73. package/lib/store/defaultGetDataID.js +1 -1
  74. package/lib/store/defaultRequiredFieldLogger.js +1 -1
  75. package/lib/store/hasOverlappingIDs.js +1 -1
  76. package/lib/store/isRelayModernEnvironment.js +1 -1
  77. package/lib/store/normalizeRelayPayload.js +1 -1
  78. package/lib/store/readInlineData.js +7 -3
  79. package/lib/subscription/requestSubscription.js +32 -34
  80. package/lib/util/JSResourceTypes.flow.js +1 -1
  81. package/lib/util/NormalizationNode.js +1 -1
  82. package/lib/util/ReaderNode.js +1 -1
  83. package/lib/util/RelayConcreteNode.js +3 -1
  84. package/lib/util/RelayDefaultHandleKey.js +1 -1
  85. package/lib/util/RelayError.js +1 -1
  86. package/lib/util/RelayFeatureFlags.js +10 -7
  87. package/lib/util/RelayProfiler.js +1 -1
  88. package/lib/util/RelayReplaySubject.js +22 -7
  89. package/lib/util/RelayRuntimeTypes.js +1 -7
  90. package/lib/util/StringInterner.js +71 -0
  91. package/lib/util/createPayloadFor3DField.js +1 -1
  92. package/lib/util/deepFreeze.js +1 -1
  93. package/lib/util/generateID.js +1 -1
  94. package/lib/util/getAllRootVariables.js +29 -0
  95. package/lib/util/getFragmentIdentifier.js +16 -8
  96. package/lib/util/getOperation.js +3 -2
  97. package/lib/util/getPaginationMetadata.js +41 -0
  98. package/lib/util/getPaginationVariables.js +66 -0
  99. package/lib/util/getPendingOperationsForFragment.js +55 -0
  100. package/lib/util/getRefetchMetadata.js +36 -0
  101. package/lib/util/getRelayHandleKey.js +3 -3
  102. package/lib/util/getRequestIdentifier.js +3 -3
  103. package/lib/util/getValueAtPath.js +51 -0
  104. package/lib/util/isEmptyObject.js +2 -2
  105. package/lib/util/isPromise.js +1 -1
  106. package/lib/util/isScalarAndEqual.js +1 -1
  107. package/lib/util/recycleNodesInto.js +1 -1
  108. package/lib/util/registerEnvironmentWithDevTools.js +26 -0
  109. package/lib/util/reportMissingRequiredFields.js +1 -1
  110. package/lib/util/resolveImmediate.js +1 -1
  111. package/lib/util/stableCopy.js +1 -1
  112. package/lib/util/withDuration.js +31 -0
  113. package/multi-actor-environment/ActorIdentifier.js.flow +18 -2
  114. package/multi-actor-environment/ActorSpecificEnvironment.js.flow +94 -58
  115. package/multi-actor-environment/ActorUtils.js.flow +33 -0
  116. package/multi-actor-environment/MultiActorEnvironment.js.flow +366 -93
  117. package/multi-actor-environment/MultiActorEnvironmentTypes.js.flow +88 -23
  118. package/multi-actor-environment/index.js.flow +3 -1
  119. package/mutations/RelayDeclarativeMutationConfig.js.flow +33 -27
  120. package/mutations/RelayRecordProxy.js.flow +5 -6
  121. package/mutations/RelayRecordSourceMutator.js.flow +5 -7
  122. package/mutations/RelayRecordSourceProxy.js.flow +20 -11
  123. package/mutations/RelayRecordSourceSelectorProxy.js.flow +23 -8
  124. package/mutations/applyOptimisticMutation.js.flow +14 -15
  125. package/mutations/commitLocalUpdate.js.flow +2 -2
  126. package/mutations/commitMutation.js.flow +36 -47
  127. package/mutations/readUpdatableQuery_EXPERIMENTAL.js.flow +318 -0
  128. package/mutations/validateMutation.js.flow +27 -17
  129. package/network/ConvertToExecuteFunction.js.flow +3 -3
  130. package/network/RelayNetwork.js.flow +5 -6
  131. package/network/RelayNetworkTypes.js.flow +1 -1
  132. package/network/RelayObservable.js.flow +2 -2
  133. package/network/RelayQueryResponseCache.js.flow +35 -22
  134. package/network/wrapNetworkWithLogObserver.js.flow +99 -0
  135. package/package.json +2 -2
  136. package/query/GraphQLTag.js.flow +11 -11
  137. package/query/PreloadableQueryRegistry.js.flow +5 -3
  138. package/query/fetchQuery.js.flow +19 -19
  139. package/query/fetchQueryInternal.js.flow +7 -10
  140. package/query/fetchQuery_DEPRECATED.js.flow +7 -7
  141. package/relay-runtime.js +2 -2
  142. package/relay-runtime.min.js +2 -2
  143. package/store/ClientID.js.flow +15 -4
  144. package/store/DataChecker.js.flow +142 -60
  145. package/store/OperationExecutor.js.flow +575 -320
  146. package/store/RelayConcreteVariables.js.flow +28 -9
  147. package/store/RelayExperimentalGraphResponseHandler.js.flow +121 -0
  148. package/store/RelayExperimentalGraphResponseTransform.js.flow +470 -0
  149. package/store/RelayModernEnvironment.js.flow +91 -115
  150. package/store/RelayModernFragmentSpecResolver.js.flow +56 -32
  151. package/store/RelayModernOperationDescriptor.js.flow +13 -8
  152. package/store/RelayModernRecord.js.flow +68 -12
  153. package/store/RelayModernSelector.js.flow +25 -15
  154. package/store/RelayModernStore.js.flow +67 -32
  155. package/store/RelayOperationTracker.js.flow +60 -44
  156. package/store/RelayOptimisticRecordSource.js.flow +3 -3
  157. package/store/RelayPublishQueue.js.flow +74 -32
  158. package/store/RelayReader.js.flow +319 -100
  159. package/store/RelayRecordSource.js.flow +73 -7
  160. package/store/RelayRecordState.js.flow +1 -1
  161. package/store/RelayReferenceMarker.js.flow +41 -27
  162. package/store/RelayResponseNormalizer.js.flow +204 -86
  163. package/store/RelayStoreReactFlightUtils.js.flow +5 -12
  164. package/store/RelayStoreSubscriptions.js.flow +20 -12
  165. package/store/RelayStoreTypes.js.flow +200 -41
  166. package/store/RelayStoreUtils.js.flow +25 -12
  167. package/store/ResolverCache.js.flow +249 -0
  168. package/store/ResolverFragments.js.flow +16 -20
  169. package/store/StoreInspector.js.flow +3 -3
  170. package/store/TypeID.js.flow +2 -2
  171. package/store/ViewerPattern.js.flow +3 -3
  172. package/store/cloneRelayHandleSourceField.js.flow +6 -7
  173. package/store/cloneRelayScalarHandleSourceField.js.flow +6 -7
  174. package/store/createFragmentSpecResolver.js.flow +4 -5
  175. package/store/createRelayContext.js.flow +4 -4
  176. package/store/defaultGetDataID.js.flow +1 -1
  177. package/store/defaultRequiredFieldLogger.js.flow +1 -1
  178. package/store/hasOverlappingIDs.js.flow +1 -1
  179. package/store/isRelayModernEnvironment.js.flow +1 -1
  180. package/store/normalizeRelayPayload.js.flow +7 -8
  181. package/store/readInlineData.js.flow +8 -9
  182. package/subscription/requestSubscription.js.flow +55 -51
  183. package/util/JSResourceTypes.flow.js.flow +1 -1
  184. package/util/NormalizationNode.js.flow +11 -4
  185. package/util/ReaderNode.js.flow +25 -2
  186. package/util/RelayConcreteNode.js.flow +5 -1
  187. package/util/RelayDefaultHandleKey.js.flow +1 -1
  188. package/util/RelayError.js.flow +1 -1
  189. package/util/RelayFeatureFlags.js.flow +23 -15
  190. package/util/RelayProfiler.js.flow +1 -1
  191. package/util/RelayReplaySubject.js.flow +10 -10
  192. package/util/RelayRuntimeTypes.js.flow +70 -3
  193. package/util/StringInterner.js.flow +69 -0
  194. package/util/createPayloadFor3DField.js.flow +4 -4
  195. package/util/deepFreeze.js.flow +1 -1
  196. package/util/generateID.js.flow +1 -1
  197. package/util/getAllRootVariables.js.flow +36 -0
  198. package/util/getFragmentIdentifier.js.flow +28 -16
  199. package/util/getOperation.js.flow +3 -3
  200. package/util/getPaginationMetadata.js.flow +69 -0
  201. package/util/getPaginationVariables.js.flow +108 -0
  202. package/util/getPendingOperationsForFragment.js.flow +62 -0
  203. package/util/getRefetchMetadata.js.flow +76 -0
  204. package/util/getRelayHandleKey.js.flow +2 -3
  205. package/util/getRequestIdentifier.js.flow +4 -4
  206. package/util/getValueAtPath.js.flow +46 -0
  207. package/util/isEmptyObject.js.flow +2 -1
  208. package/util/isPromise.js.flow +1 -1
  209. package/util/isScalarAndEqual.js.flow +1 -1
  210. package/util/recycleNodesInto.js.flow +1 -1
  211. package/util/registerEnvironmentWithDevTools.js.flow +33 -0
  212. package/util/reportMissingRequiredFields.js.flow +1 -1
  213. package/util/resolveImmediate.js.flow +2 -2
  214. package/util/stableCopy.js.flow +1 -1
  215. package/util/withDuration.js.flow +32 -0
  216. package/lib/store/RelayRecordSourceMapImpl.js +0 -107
  217. package/lib/store/RelayStoreSubscriptionsUsingMapByID.js +0 -318
  218. package/store/RelayRecordSourceMapImpl.js.flow +0 -91
  219. package/store/RelayStoreSubscriptionsUsingMapByID.js.flow +0 -283
@@ -1,5 +1,5 @@
1
1
  /**
2
- * Copyright (c) Facebook, Inc. and its affiliates.
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
3
  *
4
4
  * This source code is licensed under the MIT license found in the
5
5
  * LICENSE file in the root directory of this source tree.
@@ -11,93 +11,136 @@
11
11
 
12
12
  'use strict';
13
13
 
14
- const ActorSpecificEnvironment = require('./ActorSpecificEnvironment');
15
- const OperationExecutor = require('../store/OperationExecutor');
16
- const RelayDefaultHandlerProvider = require('../handlers/RelayDefaultHandlerProvider');
17
- const RelayModernStore = require('../store/RelayModernStore');
18
- const RelayObservable = require('../network/RelayObservable');
19
- const RelayPublishQueue = require('../store/RelayPublishQueue');
20
- const RelayRecordSource = require('../store/RelayRecordSource');
21
-
22
- const defaultGetDataID = require('../store/defaultGetDataID');
23
-
24
14
  import type {HandlerProvider} from '../handlers/RelayDefaultHandlerProvider';
25
15
  import type {GraphQLResponse, PayloadData} from '../network/RelayNetworkTypes';
26
16
  import type {INetwork} from '../network/RelayNetworkTypes';
27
- import type {ActiveState} from '../store/OperationExecutor';
17
+ import type {ActiveState, TaskScheduler} from '../store/OperationExecutor';
28
18
  import type {GetDataID} from '../store/RelayResponseNormalizer';
29
19
  import type {
30
- IEnvironment,
20
+ ExecuteMutationConfig,
21
+ LogFunction,
22
+ MissingFieldHandler,
23
+ MutableRecordSource,
24
+ MutationParameters,
25
+ OperationAvailability,
26
+ OperationDescriptor,
27
+ OperationLoader,
31
28
  OptimisticResponseConfig,
32
29
  OptimisticUpdateFunction,
33
- OperationDescriptor,
34
- OperationAvailability,
35
- Snapshot,
30
+ ReactFlightPayloadDeserializer,
31
+ ReactFlightServerErrorHandler,
32
+ RequiredFieldLogger,
36
33
  SelectorStoreUpdater,
37
34
  SingularReaderSelector,
35
+ Snapshot,
36
+ Store,
38
37
  StoreUpdater,
39
- RequiredFieldLogger,
40
- ExecuteMutationConfig,
41
- LogFunction,
42
38
  } from '../store/RelayStoreTypes';
43
39
  import type {Disposable} from '../util/RelayRuntimeTypes';
40
+ import type {RenderPolicy} from '../util/RelayRuntimeTypes';
44
41
  import type {ActorIdentifier} from './ActorIdentifier';
45
42
  import type {
46
43
  IActorEnvironment,
47
44
  IMultiActorEnvironment,
45
+ MultiActorStoreUpdater,
48
46
  } from './MultiActorEnvironmentTypes';
49
47
 
50
- function todo(what: string) {
51
- throw new Error(`Not implementd: ${what}`);
52
- }
48
+ const RelayDefaultHandlerProvider = require('../handlers/RelayDefaultHandlerProvider');
49
+ const RelayObservable = require('../network/RelayObservable');
50
+ const defaultGetDataID = require('../store/defaultGetDataID');
51
+ const defaultRequiredFieldLogger = require('../store/defaultRequiredFieldLogger');
52
+ const OperationExecutor = require('../store/OperationExecutor');
53
+ const RelayModernStore = require('../store/RelayModernStore');
54
+ const RelayRecordSource = require('../store/RelayRecordSource');
55
+ const ActorSpecificEnvironment = require('./ActorSpecificEnvironment');
53
56
 
54
57
  export type MultiActorEnvironmentConfig = $ReadOnly<{
58
+ createConfigNameForActor?: ?(actorIdentifier: ActorIdentifier) => string,
55
59
  createNetworkForActor: (actorIdentifier: ActorIdentifier) => INetwork,
60
+ createStoreForActor?: ?(actorIdentifier: ActorIdentifier) => Store,
61
+ defaultRenderPolicy?: ?RenderPolicy,
56
62
  getDataID?: GetDataID,
57
63
  handlerProvider?: HandlerProvider,
58
- logFn: LogFunction,
59
- requiredFieldLogger: RequiredFieldLogger,
64
+ isServer?: ?boolean,
65
+ logFn?: ?LogFunction,
66
+ missingFieldHandlers?: ?$ReadOnlyArray<MissingFieldHandler>,
67
+ operationLoader?: ?OperationLoader,
68
+ reactFlightPayloadDeserializer?: ?ReactFlightPayloadDeserializer,
69
+ reactFlightServerErrorHandler?: ?ReactFlightServerErrorHandler,
70
+ requiredFieldLogger?: ?RequiredFieldLogger,
71
+ scheduler?: ?TaskScheduler,
72
+ shouldProcessClientComponents?: ?boolean,
60
73
  treatMissingFieldsAsNull?: boolean,
61
74
  }>;
62
75
 
63
76
  class MultiActorEnvironment implements IMultiActorEnvironment {
64
77
  +_actorEnvironments: Map<ActorIdentifier, IActorEnvironment>;
78
+ +_createConfigNameForActor: ?(actorIdentifier: ActorIdentifier) => string;
65
79
  +_createNetworkForActor: (actorIdentifier: ActorIdentifier) => INetwork;
80
+ +_createStoreForActor: ?(actorIdentifier: ActorIdentifier) => Store;
81
+ +_defaultRenderPolicy: RenderPolicy;
66
82
  +_getDataID: GetDataID;
67
83
  +_handlerProvider: HandlerProvider;
84
+ +_isServer: boolean;
68
85
  +_logFn: LogFunction;
86
+ +_missingFieldHandlers: ?$ReadOnlyArray<MissingFieldHandler>;
69
87
  +_operationExecutions: Map<string, ActiveState>;
88
+ +_operationLoader: ?OperationLoader;
89
+ +_reactFlightPayloadDeserializer: ?ReactFlightPayloadDeserializer;
90
+ +_reactFlightServerErrorHandler: ?ReactFlightServerErrorHandler;
70
91
  +_requiredFieldLogger: RequiredFieldLogger;
92
+ +_scheduler: ?TaskScheduler;
93
+ +_shouldProcessClientComponents: ?boolean;
71
94
  +_treatMissingFieldsAsNull: boolean;
72
95
 
73
96
  constructor(config: MultiActorEnvironmentConfig) {
74
97
  this._actorEnvironments = new Map();
98
+ this._operationLoader = config.operationLoader;
75
99
  this._createNetworkForActor = config.createNetworkForActor;
100
+ this._scheduler = config.scheduler;
76
101
  this._getDataID = config.getDataID ?? defaultGetDataID;
77
102
  this._handlerProvider = config.handlerProvider
78
103
  ? config.handlerProvider
79
104
  : RelayDefaultHandlerProvider;
80
- this._logFn = config.logFn;
105
+ this._logFn = config.logFn ?? emptyFunction;
81
106
  this._operationExecutions = new Map();
82
- this._requiredFieldLogger = config.requiredFieldLogger;
107
+ this._requiredFieldLogger =
108
+ config.requiredFieldLogger ?? defaultRequiredFieldLogger;
109
+ this._shouldProcessClientComponents = config.shouldProcessClientComponents;
83
110
  this._treatMissingFieldsAsNull = config.treatMissingFieldsAsNull ?? false;
111
+ this._isServer = config.isServer ?? false;
112
+ this._missingFieldHandlers = config.missingFieldHandlers;
113
+ this._createStoreForActor = config.createStoreForActor;
114
+ this._reactFlightPayloadDeserializer =
115
+ config.reactFlightPayloadDeserializer;
116
+ this._reactFlightServerErrorHandler = config.reactFlightServerErrorHandler;
117
+ this._createConfigNameForActor = config.createConfigNameForActor;
118
+ this._defaultRenderPolicy = config.defaultRenderPolicy ?? 'partial';
84
119
  }
85
120
 
86
121
  /**
87
- * This method will create an actor specfic environment. It will create a new instance
88
- * and store it in the internal maps. If will return a memozied version
122
+ * This method will create an actor specific environment. It will create a new instance
123
+ * and store it in the internal maps. If will return a memoized version
89
124
  * of the environment if we already created one for actor.
90
125
  */
91
- forActor(actorIdentifier: ActorIdentifier): IEnvironment & IActorEnvironment {
126
+ forActor(actorIdentifier: ActorIdentifier): IActorEnvironment {
92
127
  const environment = this._actorEnvironments.get(actorIdentifier);
93
128
  if (environment == null) {
94
129
  const newEnvironment = new ActorSpecificEnvironment({
130
+ configName: this._createConfigNameForActor
131
+ ? this._createConfigNameForActor(actorIdentifier)
132
+ : null,
95
133
  actorIdentifier,
96
134
  multiActorEnvironment: this,
97
135
  logFn: this._logFn,
98
136
  requiredFieldLogger: this._requiredFieldLogger,
99
- store: new RelayModernStore(RelayRecordSource.create()),
137
+ store:
138
+ this._createStoreForActor != null
139
+ ? this._createStoreForActor(actorIdentifier)
140
+ : new RelayModernStore(RelayRecordSource.create()),
100
141
  network: this._createNetworkForActor(actorIdentifier),
142
+ handlerProvider: this._handlerProvider,
143
+ defaultRenderPolicy: this._defaultRenderPolicy,
101
144
  });
102
145
  this._actorEnvironments.set(actorIdentifier, newEnvironment);
103
146
  return newEnvironment;
@@ -107,127 +150,357 @@ class MultiActorEnvironment implements IMultiActorEnvironment {
107
150
  }
108
151
 
109
152
  check(
110
- actorIdentifier: ActorIdentifier,
153
+ actorEnvironment: IActorEnvironment,
111
154
  operation: OperationDescriptor,
112
155
  ): OperationAvailability {
113
- return todo('check');
156
+ if (
157
+ this._missingFieldHandlers == null ||
158
+ this._missingFieldHandlers.length === 0
159
+ ) {
160
+ return actorEnvironment.getStore().check(operation, {
161
+ handlers: [],
162
+ defaultActorIdentifier: actorEnvironment.actorIdentifier,
163
+ getSourceForActor: actorIdentifier => {
164
+ return this.forActor(actorIdentifier).getStore().getSource();
165
+ },
166
+ getTargetForActor: () => {
167
+ return RelayRecordSource.create();
168
+ },
169
+ });
170
+ }
171
+ return this._checkSelectorAndHandleMissingFields(
172
+ actorEnvironment,
173
+ operation,
174
+ this._missingFieldHandlers,
175
+ );
176
+ }
177
+
178
+ _checkSelectorAndHandleMissingFields(
179
+ actorEnvironment: IActorEnvironment,
180
+ operation: OperationDescriptor,
181
+ handlers: $ReadOnlyArray<MissingFieldHandler>,
182
+ ): OperationAvailability {
183
+ const targets: Map<ActorIdentifier, MutableRecordSource> = new Map([
184
+ [actorEnvironment.actorIdentifier, RelayRecordSource.create()],
185
+ ]);
186
+ const result = actorEnvironment.getStore().check(operation, {
187
+ handlers,
188
+ defaultActorIdentifier: actorEnvironment.actorIdentifier,
189
+ getSourceForActor: actorIdentifier => {
190
+ return this.forActor(actorIdentifier).getStore().getSource();
191
+ },
192
+ getTargetForActor: actorIdentifier => {
193
+ let target = targets.get(actorIdentifier);
194
+ if (target == null) {
195
+ target = RelayRecordSource.create();
196
+ targets.set(actorIdentifier, target);
197
+ }
198
+ return target;
199
+ },
200
+ });
201
+ for (const [actorIdentifier, target] of targets) {
202
+ if (target.size() > 0) {
203
+ this._scheduleUpdates(() => {
204
+ const publishQueue = this.forActor(actorIdentifier).getPublishQueue();
205
+ publishQueue.commitSource(target);
206
+ publishQueue.run();
207
+ });
208
+ }
209
+ }
210
+
211
+ return result;
114
212
  }
115
213
 
116
214
  subscribe(
117
- actorIdentifier: ActorIdentifier,
215
+ actorEnvironment: IActorEnvironment,
118
216
  snapshot: Snapshot,
119
217
  callback: (snapshot: Snapshot) => void,
120
218
  ): Disposable {
121
- return todo('subscribe');
219
+ // TODO: make actor aware
220
+ return actorEnvironment.getStore().subscribe(snapshot, callback);
122
221
  }
123
222
 
124
223
  retain(
125
- actorIdentifier: ActorIdentifier,
224
+ actorEnvironment: IActorEnvironment,
126
225
  operation: OperationDescriptor,
127
226
  ): Disposable {
128
- return todo('retain');
227
+ // TODO: make actor aware
228
+ return actorEnvironment.getStore().retain(operation);
129
229
  }
130
230
 
131
231
  applyUpdate(
132
- actorIdentifier: ActorIdentifier,
232
+ actorEnvironment: IActorEnvironment,
133
233
  optimisticUpdate: OptimisticUpdateFunction,
134
234
  ): Disposable {
135
- return todo('applyUpdate');
235
+ const publishQueue = actorEnvironment.getPublishQueue();
236
+ const dispose = () => {
237
+ this._scheduleUpdates(() => {
238
+ publishQueue.revertUpdate(optimisticUpdate);
239
+ publishQueue.run();
240
+ });
241
+ };
242
+ this._scheduleUpdates(() => {
243
+ publishQueue.applyUpdate(optimisticUpdate);
244
+ publishQueue.run();
245
+ });
246
+ return {dispose};
247
+ }
248
+
249
+ revertUpdate(
250
+ actorEnvironment: IActorEnvironment,
251
+ update: OptimisticUpdateFunction,
252
+ ): void {
253
+ const publishQueue = actorEnvironment.getPublishQueue();
254
+ this._scheduleUpdates(() => {
255
+ publishQueue.revertUpdate(update);
256
+ publishQueue.run();
257
+ });
258
+ }
259
+
260
+ replaceUpdate(
261
+ actorEnvironment: IActorEnvironment,
262
+ update: OptimisticUpdateFunction,
263
+ replacement: OptimisticUpdateFunction,
264
+ ): void {
265
+ const publishQueue = actorEnvironment.getPublishQueue();
266
+ this._scheduleUpdates(() => {
267
+ publishQueue.revertUpdate(update);
268
+ publishQueue.applyUpdate(replacement);
269
+ publishQueue.run();
270
+ });
136
271
  }
137
272
 
138
- applyMutation(
139
- actorIdentifier: ActorIdentifier,
140
- optimisticConfig: OptimisticResponseConfig,
273
+ applyMutation<TMutation: MutationParameters>(
274
+ actorEnvironment: IActorEnvironment,
275
+ optimisticConfig: OptimisticResponseConfig<TMutation>,
141
276
  ): Disposable {
142
- return todo('applyMutation');
277
+ const subscription = this._execute(actorEnvironment, {
278
+ createSource: () => RelayObservable.create(_sink => {}),
279
+ isClientPayload: false,
280
+ operation: optimisticConfig.operation,
281
+ optimisticConfig,
282
+ updater: null,
283
+ }).subscribe({});
284
+ return {
285
+ dispose: () => subscription.unsubscribe(),
286
+ };
143
287
  }
144
288
 
145
- commitUpdate(actorIdentifier: ActorIdentifier, updater: StoreUpdater): void {
146
- return todo('commitUpdate');
289
+ commitUpdate(
290
+ actorEnvironment: IActorEnvironment,
291
+ updater: StoreUpdater,
292
+ ): void {
293
+ const publishQueue = actorEnvironment.getPublishQueue();
294
+ this._scheduleUpdates(() => {
295
+ publishQueue.commitUpdate(updater);
296
+ publishQueue.run();
297
+ });
147
298
  }
148
299
 
149
300
  commitPayload(
150
- actorIdentifier: ActorIdentifier,
301
+ actorEnvironment: IActorEnvironment,
151
302
  operationDescriptor: OperationDescriptor,
152
303
  payload: PayloadData,
153
304
  ): void {
154
- return todo('commitPayload');
305
+ this._execute(actorEnvironment, {
306
+ createSource: () => RelayObservable.from({data: payload}),
307
+ isClientPayload: true,
308
+ operation: operationDescriptor,
309
+ optimisticConfig: null,
310
+ updater: null,
311
+ }).subscribe({});
155
312
  }
156
313
 
157
314
  lookup(
158
- actorIdentifier: ActorIdentifier,
315
+ actorEnvironment: IActorEnvironment,
159
316
  selector: SingularReaderSelector,
160
317
  ): Snapshot {
161
- return todo('lookup');
318
+ // TODO: make actor aware
319
+ return actorEnvironment.getStore().lookup(selector);
162
320
  }
163
321
 
164
322
  execute(
165
- actorIdentifier: ActorIdentifier,
166
- config: {
323
+ actorEnvironment: IActorEnvironment,
324
+ {
325
+ operation,
326
+ }: {
167
327
  operation: OperationDescriptor,
168
- updater?: ?SelectorStoreUpdater,
169
328
  },
170
329
  ): RelayObservable<GraphQLResponse> {
171
- const {operation, updater} = config;
172
- return RelayObservable.create(sink => {
173
- const actorEnvironemnt = this.forActor(actorIdentifier);
174
- const source = actorEnvironemnt
175
- .getNetwork()
176
- .execute(
177
- operation.request.node.params,
178
- operation.request.variables,
179
- operation.request.cacheConfig || {},
180
- null,
181
- );
182
- const executor = OperationExecutor.execute({
183
- operation,
184
- operationExecutions: this._operationExecutions,
185
- operationLoader: null,
186
- optimisticConfig: null,
187
- publishQueue: new RelayPublishQueue(
188
- actorEnvironemnt.getStore(),
189
- this._handlerProvider,
190
- this._getDataID,
191
- ),
192
- reactFlightPayloadDeserializer: null,
193
- reactFlightServerErrorHandler: null,
194
- scheduler: null,
195
- sink,
196
- source,
197
- store: actorEnvironemnt.getStore(),
198
- updater,
199
- operationTracker: actorEnvironemnt.getOperationTracker(),
200
- getDataID: this._getDataID,
201
- treatMissingFieldsAsNull: this._treatMissingFieldsAsNull,
202
- shouldProcessClientComponents: false,
203
- });
204
- return () => executor.cancel();
330
+ return this._execute(actorEnvironment, {
331
+ createSource: () =>
332
+ actorEnvironment
333
+ .getNetwork()
334
+ .execute(
335
+ operation.request.node.params,
336
+ operation.request.variables,
337
+ operation.request.cacheConfig || {},
338
+ null,
339
+ ),
340
+ isClientPayload: false,
341
+ operation,
342
+ optimisticConfig: null,
343
+ updater: null,
205
344
  });
206
345
  }
207
346
 
208
- executeMutation(
209
- actorIdentifier: ActorIdentifier,
210
- config: ExecuteMutationConfig,
347
+ executeSubscription<TMutation: MutationParameters>(
348
+ actorEnvironment: IActorEnvironment,
349
+ {
350
+ operation,
351
+ updater,
352
+ }: {
353
+ operation: OperationDescriptor,
354
+ updater?: ?SelectorStoreUpdater<TMutation['response']>,
355
+ },
211
356
  ): RelayObservable<GraphQLResponse> {
212
- return todo('executeMutation');
357
+ return this._execute(actorEnvironment, {
358
+ createSource: () =>
359
+ actorEnvironment
360
+ .getNetwork()
361
+ .execute(
362
+ operation.request.node.params,
363
+ operation.request.variables,
364
+ operation.request.cacheConfig || {},
365
+ null,
366
+ ),
367
+ isClientPayload: false,
368
+ operation,
369
+ optimisticConfig: null,
370
+ updater,
371
+ });
372
+ }
373
+
374
+ executeMutation<TMutation: MutationParameters>(
375
+ actorEnvironment: IActorEnvironment,
376
+ {
377
+ operation,
378
+ optimisticResponse,
379
+ optimisticUpdater,
380
+ updater,
381
+ uploadables,
382
+ }: ExecuteMutationConfig<TMutation>,
383
+ ): RelayObservable<GraphQLResponse> {
384
+ let optimisticConfig;
385
+ if (optimisticResponse || optimisticUpdater) {
386
+ optimisticConfig = {
387
+ operation: operation,
388
+ response: optimisticResponse,
389
+ updater: optimisticUpdater,
390
+ };
391
+ }
392
+ return this._execute(actorEnvironment, {
393
+ createSource: () =>
394
+ actorEnvironment.getNetwork().execute(
395
+ operation.request.node.params,
396
+ operation.request.variables,
397
+ {
398
+ ...operation.request.cacheConfig,
399
+ force: true,
400
+ },
401
+ uploadables,
402
+ ),
403
+ isClientPayload: false,
404
+ operation,
405
+ optimisticConfig,
406
+ updater,
407
+ });
213
408
  }
214
409
 
215
410
  executeWithSource(
216
- actorIdentifier: ActorIdentifier,
411
+ actorEnvironment: IActorEnvironment,
217
412
  config: {
218
413
  operation: OperationDescriptor,
219
414
  source: RelayObservable<GraphQLResponse>,
220
415
  },
221
416
  ): RelayObservable<GraphQLResponse> {
222
- return todo('executeWithSource');
417
+ return this._execute(actorEnvironment, {
418
+ createSource: () => config.source,
419
+ isClientPayload: false,
420
+ operation: config.operation,
421
+ optimisticConfig: null,
422
+ updater: null,
423
+ });
223
424
  }
224
425
 
225
426
  isRequestActive(
226
- actorIdentifier: ActorIdentifier,
427
+ _actorEnvironment: IActorEnvironment,
227
428
  requestIdentifier: string,
228
429
  ): boolean {
229
- return todo('isRequestActive');
430
+ const activeState = this._operationExecutions.get(requestIdentifier);
431
+ return activeState === 'active';
432
+ }
433
+
434
+ isServer(): boolean {
435
+ return this._isServer;
436
+ }
437
+
438
+ _execute<TMutation: MutationParameters>(
439
+ actorEnvironment: IActorEnvironment,
440
+ {
441
+ createSource,
442
+ isClientPayload,
443
+ operation,
444
+ optimisticConfig,
445
+ updater,
446
+ }: {|
447
+ createSource: () => RelayObservable<GraphQLResponse>,
448
+ isClientPayload: boolean,
449
+ operation: OperationDescriptor,
450
+ optimisticConfig: ?OptimisticResponseConfig<TMutation>,
451
+ updater: ?SelectorStoreUpdater<TMutation['response']>,
452
+ |},
453
+ ): RelayObservable<GraphQLResponse> {
454
+ return RelayObservable.create(sink => {
455
+ const executor = OperationExecutor.execute({
456
+ actorIdentifier: actorEnvironment.actorIdentifier,
457
+ getDataID: this._getDataID,
458
+ isClientPayload,
459
+ operation,
460
+ operationExecutions: this._operationExecutions,
461
+ operationLoader: this._operationLoader,
462
+ operationTracker: actorEnvironment.getOperationTracker(),
463
+ optimisticConfig,
464
+ getPublishQueue: (actorIdentifier: ActorIdentifier) => {
465
+ return this.forActor(actorIdentifier).getPublishQueue();
466
+ },
467
+ reactFlightPayloadDeserializer: this._reactFlightPayloadDeserializer,
468
+ reactFlightServerErrorHandler: this._reactFlightServerErrorHandler,
469
+ scheduler: this._scheduler,
470
+ shouldProcessClientComponents: this._shouldProcessClientComponents,
471
+ sink,
472
+ // NOTE: Some product tests expect `Network.execute` to be called only
473
+ // when the Observable is executed.
474
+ source: createSource(),
475
+ getStore: (actorIdentifier: ActorIdentifier) => {
476
+ return this.forActor(actorIdentifier).getStore();
477
+ },
478
+ treatMissingFieldsAsNull: this._treatMissingFieldsAsNull,
479
+ updater,
480
+ log: this._logFn,
481
+ });
482
+ return () => executor.cancel();
483
+ });
484
+ }
485
+
486
+ _scheduleUpdates(task: () => void) {
487
+ const scheduler = this._scheduler;
488
+ if (scheduler != null) {
489
+ scheduler.schedule(task);
490
+ } else {
491
+ task();
492
+ }
493
+ }
494
+
495
+ commitMultiActorUpdate(updater: MultiActorStoreUpdater): void {
496
+ for (const [actorIdentifier, environment] of this._actorEnvironments) {
497
+ environment.commitUpdate(storeProxy => {
498
+ updater(actorIdentifier, environment, storeProxy);
499
+ });
500
+ }
230
501
  }
231
502
  }
232
503
 
504
+ function emptyFunction() {}
505
+
233
506
  module.exports = MultiActorEnvironment;