relay-runtime 11.0.0 → 13.0.0-rc.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 (169) hide show
  1. package/handlers/RelayDefaultHandlerProvider.js.flow +2 -2
  2. package/handlers/connection/ConnectionHandler.js.flow +8 -10
  3. package/handlers/connection/MutationHandlers.js.flow +31 -7
  4. package/index.js +1 -1
  5. package/index.js.flow +60 -36
  6. package/lib/handlers/RelayDefaultHandlerProvider.js +1 -1
  7. package/lib/handlers/connection/ConnectionHandler.js +8 -8
  8. package/lib/handlers/connection/MutationHandlers.js +61 -5
  9. package/lib/index.js +58 -43
  10. package/lib/multi-actor-environment/ActorIdentifier.js +33 -0
  11. package/lib/multi-actor-environment/ActorSpecificEnvironment.js +152 -0
  12. package/lib/multi-actor-environment/ActorUtils.js +27 -0
  13. package/lib/multi-actor-environment/MultiActorEnvironment.js +419 -0
  14. package/lib/multi-actor-environment/MultiActorEnvironmentTypes.js +11 -0
  15. package/lib/multi-actor-environment/index.js +21 -0
  16. package/lib/mutations/RelayDeclarativeMutationConfig.js +4 -1
  17. package/lib/mutations/RelayRecordProxy.js +3 -2
  18. package/lib/mutations/RelayRecordSourceMutator.js +3 -2
  19. package/lib/mutations/RelayRecordSourceProxy.js +12 -4
  20. package/lib/mutations/RelayRecordSourceSelectorProxy.js +18 -5
  21. package/lib/mutations/applyOptimisticMutation.js +6 -6
  22. package/lib/mutations/commitMutation.js +14 -10
  23. package/lib/mutations/readUpdatableQuery_EXPERIMENTAL.js +238 -0
  24. package/lib/mutations/validateMutation.js +12 -5
  25. package/lib/network/ConvertToExecuteFunction.js +2 -1
  26. package/lib/network/RelayNetwork.js +3 -2
  27. package/lib/network/RelayQueryResponseCache.js +21 -4
  28. package/lib/network/wrapNetworkWithLogObserver.js +79 -0
  29. package/lib/query/GraphQLTag.js +3 -2
  30. package/lib/query/fetchQuery.js +6 -5
  31. package/lib/query/fetchQueryInternal.js +1 -1
  32. package/lib/query/fetchQuery_DEPRECATED.js +2 -1
  33. package/lib/store/ClientID.js +7 -1
  34. package/lib/store/DataChecker.js +141 -60
  35. package/lib/store/{RelayModernQueryExecutor.js → OperationExecutor.js} +532 -195
  36. package/lib/store/RelayConcreteVariables.js +24 -4
  37. package/lib/store/RelayModernEnvironment.js +175 -234
  38. package/lib/store/RelayModernFragmentSpecResolver.js +52 -26
  39. package/lib/store/RelayModernOperationDescriptor.js +2 -1
  40. package/lib/store/RelayModernRecord.js +47 -12
  41. package/lib/store/RelayModernSelector.js +14 -8
  42. package/lib/store/RelayModernStore.js +58 -29
  43. package/lib/store/RelayOperationTracker.js +34 -24
  44. package/lib/store/RelayPublishQueue.js +41 -13
  45. package/lib/store/RelayReader.js +287 -46
  46. package/lib/store/RelayRecordSource.js +87 -3
  47. package/lib/store/RelayReferenceMarker.js +55 -31
  48. package/lib/store/RelayResponseNormalizer.js +250 -108
  49. package/lib/store/RelayStoreReactFlightUtils.js +8 -12
  50. package/lib/store/RelayStoreSubscriptions.js +14 -9
  51. package/lib/store/RelayStoreUtils.js +11 -5
  52. package/lib/store/ResolverCache.js +213 -0
  53. package/lib/store/ResolverFragments.js +61 -0
  54. package/lib/store/cloneRelayHandleSourceField.js +5 -4
  55. package/lib/store/cloneRelayScalarHandleSourceField.js +5 -4
  56. package/lib/store/createRelayContext.js +4 -2
  57. package/lib/store/defaultGetDataID.js +3 -1
  58. package/lib/store/readInlineData.js +6 -2
  59. package/lib/subscription/requestSubscription.js +35 -9
  60. package/lib/util/RelayConcreteNode.js +4 -0
  61. package/lib/util/RelayFeatureFlags.js +11 -4
  62. package/lib/util/RelayProfiler.js +17 -187
  63. package/lib/util/RelayReplaySubject.js +22 -7
  64. package/lib/util/RelayRuntimeTypes.js +0 -6
  65. package/lib/util/StringInterner.js +71 -0
  66. package/lib/util/deepFreeze.js +1 -0
  67. package/lib/util/getFragmentIdentifier.js +15 -7
  68. package/lib/util/getOperation.js +2 -1
  69. package/lib/util/getPaginationMetadata.js +41 -0
  70. package/lib/util/getPaginationVariables.js +66 -0
  71. package/lib/util/getPendingOperationsForFragment.js +55 -0
  72. package/lib/util/getRefetchMetadata.js +36 -0
  73. package/lib/util/getRelayHandleKey.js +2 -2
  74. package/lib/util/getRequestIdentifier.js +2 -2
  75. package/lib/util/getValueAtPath.js +51 -0
  76. package/lib/util/isEmptyObject.js +1 -1
  77. package/lib/util/registerEnvironmentWithDevTools.js +26 -0
  78. package/lib/util/withDuration.js +31 -0
  79. package/multi-actor-environment/ActorIdentifier.js.flow +43 -0
  80. package/multi-actor-environment/ActorSpecificEnvironment.js.flow +225 -0
  81. package/multi-actor-environment/ActorUtils.js.flow +33 -0
  82. package/multi-actor-environment/MultiActorEnvironment.js.flow +506 -0
  83. package/multi-actor-environment/MultiActorEnvironmentTypes.js.flow +261 -0
  84. package/multi-actor-environment/index.js.flow +26 -0
  85. package/mutations/RelayDeclarativeMutationConfig.js.flow +32 -26
  86. package/mutations/RelayRecordProxy.js.flow +4 -5
  87. package/mutations/RelayRecordSourceMutator.js.flow +4 -6
  88. package/mutations/RelayRecordSourceProxy.js.flow +19 -10
  89. package/mutations/RelayRecordSourceSelectorProxy.js.flow +22 -7
  90. package/mutations/applyOptimisticMutation.js.flow +13 -14
  91. package/mutations/commitLocalUpdate.js.flow +1 -1
  92. package/mutations/commitMutation.js.flow +35 -46
  93. package/mutations/readUpdatableQuery_EXPERIMENTAL.js.flow +309 -0
  94. package/mutations/validateMutation.js.flow +28 -16
  95. package/network/ConvertToExecuteFunction.js.flow +2 -2
  96. package/network/RelayNetwork.js.flow +4 -5
  97. package/network/RelayNetworkTypes.js.flow +17 -8
  98. package/network/RelayObservable.js.flow +1 -1
  99. package/network/RelayQueryResponseCache.js.flow +34 -20
  100. package/network/wrapNetworkWithLogObserver.js.flow +100 -0
  101. package/package.json +3 -2
  102. package/query/GraphQLTag.js.flow +9 -9
  103. package/query/PreloadableQueryRegistry.js.flow +2 -1
  104. package/query/fetchQuery.js.flow +11 -13
  105. package/query/fetchQueryInternal.js.flow +6 -9
  106. package/query/fetchQuery_DEPRECATED.js.flow +6 -6
  107. package/relay-runtime.js +2 -2
  108. package/relay-runtime.min.js +2 -2
  109. package/store/ClientID.js.flow +14 -3
  110. package/store/DataChecker.js.flow +162 -67
  111. package/store/{RelayModernQueryExecutor.js.flow → OperationExecutor.js.flow} +616 -283
  112. package/store/RelayConcreteVariables.js.flow +27 -5
  113. package/store/RelayModernEnvironment.js.flow +176 -235
  114. package/store/RelayModernFragmentSpecResolver.js.flow +55 -31
  115. package/store/RelayModernOperationDescriptor.js.flow +12 -7
  116. package/store/RelayModernRecord.js.flow +67 -11
  117. package/store/RelayModernSelector.js.flow +24 -14
  118. package/store/RelayModernStore.js.flow +72 -36
  119. package/store/RelayOperationTracker.js.flow +59 -43
  120. package/store/RelayOptimisticRecordSource.js.flow +2 -2
  121. package/store/RelayPublishQueue.js.flow +79 -34
  122. package/store/RelayReader.js.flow +351 -72
  123. package/store/RelayRecordSource.js.flow +72 -6
  124. package/store/RelayReferenceMarker.js.flow +60 -33
  125. package/store/RelayResponseNormalizer.js.flow +288 -102
  126. package/store/RelayStoreReactFlightUtils.js.flow +9 -13
  127. package/store/RelayStoreSubscriptions.js.flow +19 -11
  128. package/store/RelayStoreTypes.js.flow +210 -44
  129. package/store/RelayStoreUtils.js.flow +25 -11
  130. package/store/ResolverCache.js.flow +249 -0
  131. package/store/ResolverFragments.js.flow +121 -0
  132. package/store/StoreInspector.js.flow +2 -2
  133. package/store/TypeID.js.flow +1 -1
  134. package/store/ViewerPattern.js.flow +2 -2
  135. package/store/cloneRelayHandleSourceField.js.flow +5 -6
  136. package/store/cloneRelayScalarHandleSourceField.js.flow +5 -6
  137. package/store/createFragmentSpecResolver.js.flow +3 -4
  138. package/store/createRelayContext.js.flow +3 -3
  139. package/store/defaultGetDataID.js.flow +3 -1
  140. package/store/normalizeRelayPayload.js.flow +6 -7
  141. package/store/readInlineData.js.flow +7 -8
  142. package/subscription/requestSubscription.js.flow +54 -27
  143. package/util/NormalizationNode.js.flow +16 -3
  144. package/util/ReaderNode.js.flow +38 -2
  145. package/util/RelayConcreteNode.js.flow +4 -0
  146. package/util/RelayFeatureFlags.js.flow +24 -8
  147. package/util/RelayProfiler.js.flow +22 -194
  148. package/util/RelayReplaySubject.js.flow +9 -9
  149. package/util/RelayRuntimeTypes.js.flow +73 -4
  150. package/util/StringInterner.js.flow +69 -0
  151. package/util/createPayloadFor3DField.js.flow +3 -3
  152. package/util/deepFreeze.js.flow +2 -1
  153. package/util/getFragmentIdentifier.js.flow +27 -15
  154. package/util/getOperation.js.flow +2 -2
  155. package/util/getPaginationMetadata.js.flow +72 -0
  156. package/util/getPaginationVariables.js.flow +108 -0
  157. package/util/getPendingOperationsForFragment.js.flow +62 -0
  158. package/util/getRefetchMetadata.js.flow +79 -0
  159. package/util/getRelayHandleKey.js.flow +1 -2
  160. package/util/getRequestIdentifier.js.flow +3 -3
  161. package/util/getValueAtPath.js.flow +46 -0
  162. package/util/isEmptyObject.js.flow +2 -1
  163. package/util/registerEnvironmentWithDevTools.js.flow +33 -0
  164. package/util/resolveImmediate.js.flow +1 -1
  165. package/util/withDuration.js.flow +32 -0
  166. package/lib/store/RelayRecordSourceMapImpl.js +0 -107
  167. package/lib/store/RelayStoreSubscriptionsUsingMapByID.js +0 -318
  168. package/store/RelayRecordSourceMapImpl.js.flow +0 -91
  169. package/store/RelayStoreSubscriptionsUsingMapByID.js.flow +0 -283
@@ -0,0 +1,506 @@
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
+ 'use strict';
13
+
14
+ import type {HandlerProvider} from '../handlers/RelayDefaultHandlerProvider';
15
+ import type {GraphQLResponse, PayloadData} from '../network/RelayNetworkTypes';
16
+ import type {INetwork} from '../network/RelayNetworkTypes';
17
+ import type {ActiveState, TaskScheduler} from '../store/OperationExecutor';
18
+ import type {GetDataID} from '../store/RelayResponseNormalizer';
19
+ import type {
20
+ ExecuteMutationConfig,
21
+ LogFunction,
22
+ MissingFieldHandler,
23
+ MutableRecordSource,
24
+ MutationParameters,
25
+ OperationAvailability,
26
+ OperationDescriptor,
27
+ OperationLoader,
28
+ OptimisticResponseConfig,
29
+ OptimisticUpdateFunction,
30
+ ReactFlightPayloadDeserializer,
31
+ ReactFlightServerErrorHandler,
32
+ RequiredFieldLogger,
33
+ SelectorStoreUpdater,
34
+ SingularReaderSelector,
35
+ Snapshot,
36
+ Store,
37
+ StoreUpdater,
38
+ } from '../store/RelayStoreTypes';
39
+ import type {Disposable} from '../util/RelayRuntimeTypes';
40
+ import type {RenderPolicy} from '../util/RelayRuntimeTypes';
41
+ import type {ActorIdentifier} from './ActorIdentifier';
42
+ import type {
43
+ IActorEnvironment,
44
+ IMultiActorEnvironment,
45
+ MultiActorStoreUpdater,
46
+ } from './MultiActorEnvironmentTypes';
47
+
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');
56
+
57
+ export type MultiActorEnvironmentConfig = $ReadOnly<{
58
+ createConfigNameForActor?: ?(actorIdentifier: ActorIdentifier) => string,
59
+ createNetworkForActor: (actorIdentifier: ActorIdentifier) => INetwork,
60
+ createStoreForActor?: ?(actorIdentifier: ActorIdentifier) => Store,
61
+ defaultRenderPolicy?: ?RenderPolicy,
62
+ getDataID?: GetDataID,
63
+ handlerProvider?: HandlerProvider,
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,
73
+ treatMissingFieldsAsNull?: boolean,
74
+ }>;
75
+
76
+ class MultiActorEnvironment implements IMultiActorEnvironment {
77
+ +_actorEnvironments: Map<ActorIdentifier, IActorEnvironment>;
78
+ +_createConfigNameForActor: ?(actorIdentifier: ActorIdentifier) => string;
79
+ +_createNetworkForActor: (actorIdentifier: ActorIdentifier) => INetwork;
80
+ +_createStoreForActor: ?(actorIdentifier: ActorIdentifier) => Store;
81
+ +_defaultRenderPolicy: RenderPolicy;
82
+ +_getDataID: GetDataID;
83
+ +_handlerProvider: HandlerProvider;
84
+ +_isServer: boolean;
85
+ +_logFn: LogFunction;
86
+ +_missingFieldHandlers: ?$ReadOnlyArray<MissingFieldHandler>;
87
+ +_operationExecutions: Map<string, ActiveState>;
88
+ +_operationLoader: ?OperationLoader;
89
+ +_reactFlightPayloadDeserializer: ?ReactFlightPayloadDeserializer;
90
+ +_reactFlightServerErrorHandler: ?ReactFlightServerErrorHandler;
91
+ +_requiredFieldLogger: RequiredFieldLogger;
92
+ +_scheduler: ?TaskScheduler;
93
+ +_shouldProcessClientComponents: ?boolean;
94
+ +_treatMissingFieldsAsNull: boolean;
95
+
96
+ constructor(config: MultiActorEnvironmentConfig) {
97
+ this._actorEnvironments = new Map();
98
+ this._operationLoader = config.operationLoader;
99
+ this._createNetworkForActor = config.createNetworkForActor;
100
+ this._scheduler = config.scheduler;
101
+ this._getDataID = config.getDataID ?? defaultGetDataID;
102
+ this._handlerProvider = config.handlerProvider
103
+ ? config.handlerProvider
104
+ : RelayDefaultHandlerProvider;
105
+ this._logFn = config.logFn ?? emptyFunction;
106
+ this._operationExecutions = new Map();
107
+ this._requiredFieldLogger =
108
+ config.requiredFieldLogger ?? defaultRequiredFieldLogger;
109
+ this._shouldProcessClientComponents = config.shouldProcessClientComponents;
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';
119
+ }
120
+
121
+ /**
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
124
+ * of the environment if we already created one for actor.
125
+ */
126
+ forActor(actorIdentifier: ActorIdentifier): IActorEnvironment {
127
+ const environment = this._actorEnvironments.get(actorIdentifier);
128
+ if (environment == null) {
129
+ const newEnvironment = new ActorSpecificEnvironment({
130
+ configName: this._createConfigNameForActor
131
+ ? this._createConfigNameForActor(actorIdentifier)
132
+ : null,
133
+ actorIdentifier,
134
+ multiActorEnvironment: this,
135
+ logFn: this._logFn,
136
+ requiredFieldLogger: this._requiredFieldLogger,
137
+ store:
138
+ this._createStoreForActor != null
139
+ ? this._createStoreForActor(actorIdentifier)
140
+ : new RelayModernStore(RelayRecordSource.create()),
141
+ network: this._createNetworkForActor(actorIdentifier),
142
+ handlerProvider: this._handlerProvider,
143
+ defaultRenderPolicy: this._defaultRenderPolicy,
144
+ });
145
+ this._actorEnvironments.set(actorIdentifier, newEnvironment);
146
+ return newEnvironment;
147
+ } else {
148
+ return environment;
149
+ }
150
+ }
151
+
152
+ check(
153
+ actorEnvironment: IActorEnvironment,
154
+ operation: OperationDescriptor,
155
+ ): OperationAvailability {
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;
212
+ }
213
+
214
+ subscribe(
215
+ actorEnvironment: IActorEnvironment,
216
+ snapshot: Snapshot,
217
+ callback: (snapshot: Snapshot) => void,
218
+ ): Disposable {
219
+ // TODO: make actor aware
220
+ return actorEnvironment.getStore().subscribe(snapshot, callback);
221
+ }
222
+
223
+ retain(
224
+ actorEnvironment: IActorEnvironment,
225
+ operation: OperationDescriptor,
226
+ ): Disposable {
227
+ // TODO: make actor aware
228
+ return actorEnvironment.getStore().retain(operation);
229
+ }
230
+
231
+ applyUpdate(
232
+ actorEnvironment: IActorEnvironment,
233
+ optimisticUpdate: OptimisticUpdateFunction,
234
+ ): Disposable {
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
+ });
271
+ }
272
+
273
+ applyMutation<TMutation: MutationParameters>(
274
+ actorEnvironment: IActorEnvironment,
275
+ optimisticConfig: OptimisticResponseConfig<TMutation>,
276
+ ): Disposable {
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
+ };
287
+ }
288
+
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
+ });
298
+ }
299
+
300
+ commitPayload(
301
+ actorEnvironment: IActorEnvironment,
302
+ operationDescriptor: OperationDescriptor,
303
+ payload: PayloadData,
304
+ ): void {
305
+ this._execute(actorEnvironment, {
306
+ createSource: () => RelayObservable.from({data: payload}),
307
+ isClientPayload: true,
308
+ operation: operationDescriptor,
309
+ optimisticConfig: null,
310
+ updater: null,
311
+ }).subscribe({});
312
+ }
313
+
314
+ lookup(
315
+ actorEnvironment: IActorEnvironment,
316
+ selector: SingularReaderSelector,
317
+ ): Snapshot {
318
+ // TODO: make actor aware
319
+ return actorEnvironment.getStore().lookup(selector);
320
+ }
321
+
322
+ execute(
323
+ actorEnvironment: IActorEnvironment,
324
+ {
325
+ operation,
326
+ }: {
327
+ operation: OperationDescriptor,
328
+ },
329
+ ): RelayObservable<GraphQLResponse> {
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,
344
+ });
345
+ }
346
+
347
+ executeSubscription<TMutation: MutationParameters>(
348
+ actorEnvironment: IActorEnvironment,
349
+ {
350
+ operation,
351
+ updater,
352
+ }: {
353
+ operation: OperationDescriptor,
354
+ updater?: ?SelectorStoreUpdater<TMutation['response']>,
355
+ },
356
+ ): RelayObservable<GraphQLResponse> {
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
+ });
408
+ }
409
+
410
+ executeWithSource(
411
+ actorEnvironment: IActorEnvironment,
412
+ config: {
413
+ operation: OperationDescriptor,
414
+ source: RelayObservable<GraphQLResponse>,
415
+ },
416
+ ): RelayObservable<GraphQLResponse> {
417
+ return this._execute(actorEnvironment, {
418
+ createSource: () => config.source,
419
+ isClientPayload: false,
420
+ operation: config.operation,
421
+ optimisticConfig: null,
422
+ updater: null,
423
+ });
424
+ }
425
+
426
+ isRequestActive(
427
+ _actorEnvironment: IActorEnvironment,
428
+ requestIdentifier: string,
429
+ ): boolean {
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
+ }
501
+ }
502
+ }
503
+
504
+ function emptyFunction() {}
505
+
506
+ module.exports = MultiActorEnvironment;