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,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;
@@ -21,7 +21,7 @@ function resolveImmediate(callback: () => void) {
21
21
  resolvedPromise.then(callback).catch(throwNext);
22
22
  }
23
23
 
24
- function throwNext(error) {
24
+ function throwNext(error: $FlowFixMe) {
25
25
  setTimeout(() => {
26
26
  throw error;
27
27
  }, 0);
@@ -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;
@@ -1,318 +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 RelayFeatureFlags = require('../util/RelayFeatureFlags');
18
-
19
- var RelayReader = require('./RelayReader');
20
-
21
- var deepFreeze = require('../util/deepFreeze');
22
-
23
- var recycleNodesInto = require('../util/recycleNodesInto');
24
-
25
- var RelayStoreSubscriptionsUsingMapByID = /*#__PURE__*/function () {
26
- function RelayStoreSubscriptionsUsingMapByID(log) {
27
- this._notifiedRevision = 0;
28
- this._snapshotRevision = 0;
29
- this._subscriptionsByDataId = new Map();
30
- this._staleSubscriptions = new Set();
31
- this.__log = log;
32
- }
33
-
34
- var _proto = RelayStoreSubscriptionsUsingMapByID.prototype;
35
-
36
- _proto.subscribe = function subscribe(snapshot, callback) {
37
- var _this = this;
38
-
39
- var subscription = {
40
- backup: null,
41
- callback: callback,
42
- notifiedRevision: this._notifiedRevision,
43
- snapshotRevision: this._snapshotRevision,
44
- snapshot: snapshot
45
- };
46
-
47
- var dispose = function dispose() {
48
- var _iterator = (0, _createForOfIteratorHelper2["default"])(snapshot.seenRecords),
49
- _step;
50
-
51
- try {
52
- for (_iterator.s(); !(_step = _iterator.n()).done;) {
53
- var dataId = _step.value;
54
-
55
- var subscriptionsForDataId = _this._subscriptionsByDataId.get(dataId);
56
-
57
- if (subscriptionsForDataId != null) {
58
- subscriptionsForDataId["delete"](subscription);
59
-
60
- if (subscriptionsForDataId.size === 0) {
61
- _this._subscriptionsByDataId["delete"](dataId);
62
- }
63
- }
64
- }
65
- } catch (err) {
66
- _iterator.e(err);
67
- } finally {
68
- _iterator.f();
69
- }
70
- };
71
-
72
- var _iterator2 = (0, _createForOfIteratorHelper2["default"])(snapshot.seenRecords),
73
- _step2;
74
-
75
- try {
76
- for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
77
- var dataId = _step2.value;
78
-
79
- var subscriptionsForDataId = this._subscriptionsByDataId.get(dataId);
80
-
81
- if (subscriptionsForDataId != null) {
82
- subscriptionsForDataId.add(subscription);
83
- } else {
84
- this._subscriptionsByDataId.set(dataId, new Set([subscription]));
85
- }
86
- }
87
- } catch (err) {
88
- _iterator2.e(err);
89
- } finally {
90
- _iterator2.f();
91
- }
92
-
93
- return {
94
- dispose: dispose
95
- };
96
- };
97
-
98
- _proto.snapshotSubscriptions = function snapshotSubscriptions(source) {
99
- var _this2 = this;
100
-
101
- this._snapshotRevision++;
102
-
103
- this._subscriptionsByDataId.forEach(function (subscriptions) {
104
- subscriptions.forEach(function (subscription) {
105
- if (subscription.snapshotRevision === _this2._snapshotRevision) {
106
- return;
107
- }
108
-
109
- subscription.snapshotRevision = _this2._snapshotRevision; // Backup occurs after writing a new "final" payload(s) and before (re)applying
110
- // optimistic changes. Each subscription's `snapshot` represents what was *last
111
- // published to the subscriber*, which notably may include previous optimistic
112
- // updates. Therefore a subscription can be in any of the following states:
113
- // - stale=true: This subscription was restored to a different value than
114
- // `snapshot`. That means this subscription has changes relative to its base,
115
- // but its base has changed (we just applied a final payload): recompute
116
- // a backup so that we can later restore to the state the subscription
117
- // should be in.
118
- // - stale=false: This subscription was restored to the same value than
119
- // `snapshot`. That means this subscription does *not* have changes relative
120
- // to its base, so the current `snapshot` is valid to use as a backup.
121
-
122
- if (!_this2._staleSubscriptions.has(subscription)) {
123
- subscription.backup = subscription.snapshot;
124
- return;
125
- }
126
-
127
- var snapshot = subscription.snapshot;
128
- var backup = RelayReader.read(source, snapshot.selector);
129
- var nextData = recycleNodesInto(snapshot.data, backup.data);
130
- backup.data = nextData; // backup owns the snapshot and can safely mutate
131
-
132
- subscription.backup = backup;
133
- });
134
- });
135
- };
136
-
137
- _proto.restoreSubscriptions = function restoreSubscriptions() {
138
- var _this3 = this;
139
-
140
- this._snapshotRevision++;
141
-
142
- this._subscriptionsByDataId.forEach(function (subscriptions) {
143
- subscriptions.forEach(function (subscription) {
144
- if (subscription.snapshotRevision === _this3._snapshotRevision) {
145
- return;
146
- }
147
-
148
- subscription.snapshotRevision = _this3._snapshotRevision;
149
- var backup = subscription.backup;
150
- subscription.backup = null;
151
-
152
- if (backup) {
153
- if (backup.data !== subscription.snapshot.data) {
154
- _this3._staleSubscriptions.add(subscription);
155
- }
156
-
157
- var prevSeenRecords = subscription.snapshot.seenRecords;
158
- subscription.snapshot = {
159
- data: subscription.snapshot.data,
160
- isMissingData: backup.isMissingData,
161
- seenRecords: backup.seenRecords,
162
- selector: backup.selector,
163
- missingRequiredFields: backup.missingRequiredFields
164
- };
165
-
166
- _this3._updateSubscriptionsMap(subscription, prevSeenRecords);
167
- } else {
168
- _this3._staleSubscriptions.add(subscription);
169
- }
170
- });
171
- });
172
- };
173
-
174
- _proto.updateSubscriptions = function updateSubscriptions(source, updatedRecordIDs, updatedOwners, sourceOperation) {
175
- var _this4 = this;
176
-
177
- this._notifiedRevision++;
178
- updatedRecordIDs.forEach(function (updatedRecordId) {
179
- var subcriptionsForDataId = _this4._subscriptionsByDataId.get(updatedRecordId);
180
-
181
- if (subcriptionsForDataId == null) {
182
- return;
183
- }
184
-
185
- subcriptionsForDataId.forEach(function (subscription) {
186
- if (subscription.notifiedRevision === _this4._notifiedRevision) {
187
- return;
188
- }
189
-
190
- var owner = _this4._updateSubscription(source, subscription, false, sourceOperation);
191
-
192
- if (owner != null) {
193
- updatedOwners.push(owner);
194
- }
195
- });
196
- });
197
-
198
- this._staleSubscriptions.forEach(function (subscription) {
199
- if (subscription.notifiedRevision === _this4._notifiedRevision) {
200
- return;
201
- }
202
-
203
- var owner = _this4._updateSubscription(source, subscription, true, sourceOperation);
204
-
205
- if (owner != null) {
206
- updatedOwners.push(owner);
207
- }
208
- });
209
-
210
- this._staleSubscriptions.clear();
211
- }
212
- /**
213
- * Notifies the callback for the subscription if the data for the associated
214
- * snapshot has changed.
215
- * Additionally, updates the subscription snapshot with the latest snapshot,
216
- * amarks it as not stale, and updates the subscription tracking for any
217
- * any new ids observed in the latest data snapshot.
218
- * Returns the owner (RequestDescriptor) if the subscription was affected by the
219
- * latest update, or null if it was not affected.
220
- */
221
- ;
222
-
223
- _proto._updateSubscription = function _updateSubscription(source, subscription, stale, sourceOperation) {
224
- var backup = subscription.backup,
225
- callback = subscription.callback,
226
- snapshot = subscription.snapshot;
227
- var nextSnapshot = stale && backup != null ? backup : RelayReader.read(source, snapshot.selector);
228
- var nextData = recycleNodesInto(snapshot.data, nextSnapshot.data);
229
- nextSnapshot = {
230
- data: nextData,
231
- isMissingData: nextSnapshot.isMissingData,
232
- seenRecords: nextSnapshot.seenRecords,
233
- selector: nextSnapshot.selector,
234
- missingRequiredFields: nextSnapshot.missingRequiredFields
235
- };
236
-
237
- if (process.env.NODE_ENV !== "production") {
238
- deepFreeze(nextSnapshot);
239
- }
240
-
241
- var prevSeenRecords = subscription.snapshot.seenRecords;
242
- subscription.snapshot = nextSnapshot;
243
- subscription.notifiedRevision = this._notifiedRevision;
244
-
245
- this._updateSubscriptionsMap(subscription, prevSeenRecords);
246
-
247
- if (nextSnapshot.data !== snapshot.data) {
248
- if (this.__log && RelayFeatureFlags.ENABLE_NOTIFY_SUBSCRIPTION) {
249
- this.__log({
250
- name: 'store.notify.subscription',
251
- sourceOperation: sourceOperation,
252
- snapshot: snapshot,
253
- nextSnapshot: nextSnapshot
254
- });
255
- }
256
-
257
- callback(nextSnapshot);
258
- return snapshot.selector.owner;
259
- }
260
- }
261
- /**
262
- * Updates the Map that tracks subscriptions by id.
263
- * Given an updated subscription and the records that where seen
264
- * on the previous subscription snapshot, updates our tracking
265
- * to track the subscription for the newly and no longer seen ids.
266
- */
267
- ;
268
-
269
- _proto._updateSubscriptionsMap = function _updateSubscriptionsMap(subscription, prevSeenRecords) {
270
- var _iterator3 = (0, _createForOfIteratorHelper2["default"])(prevSeenRecords),
271
- _step3;
272
-
273
- try {
274
- for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
275
- var dataId = _step3.value;
276
-
277
- var subscriptionsForDataId = this._subscriptionsByDataId.get(dataId);
278
-
279
- if (subscriptionsForDataId != null) {
280
- subscriptionsForDataId["delete"](subscription);
281
-
282
- if (subscriptionsForDataId.size === 0) {
283
- this._subscriptionsByDataId["delete"](dataId);
284
- }
285
- }
286
- }
287
- } catch (err) {
288
- _iterator3.e(err);
289
- } finally {
290
- _iterator3.f();
291
- }
292
-
293
- var _iterator4 = (0, _createForOfIteratorHelper2["default"])(subscription.snapshot.seenRecords),
294
- _step4;
295
-
296
- try {
297
- for (_iterator4.s(); !(_step4 = _iterator4.n()).done;) {
298
- var _dataId = _step4.value;
299
-
300
- var _subscriptionsForDataId = this._subscriptionsByDataId.get(_dataId);
301
-
302
- if (_subscriptionsForDataId != null) {
303
- _subscriptionsForDataId.add(subscription);
304
- } else {
305
- this._subscriptionsByDataId.set(_dataId, new Set([subscription]));
306
- }
307
- }
308
- } catch (err) {
309
- _iterator4.e(err);
310
- } finally {
311
- _iterator4.f();
312
- }
313
- };
314
-
315
- return RelayStoreSubscriptionsUsingMapByID;
316
- }();
317
-
318
- module.exports = RelayStoreSubscriptionsUsingMapByID;
@@ -1,91 +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
- * @flow strict-local
8
- * @format
9
- */
10
-
11
- // flowlint ambiguous-object-type:error
12
-
13
- 'use strict';
14
-
15
- const RelayRecordState = require('./RelayRecordState');
16
-
17
- import type {DataID} from '../util/RelayRuntimeTypes';
18
- import type {RecordState} from './RelayRecordState';
19
- import type {
20
- MutableRecordSource,
21
- Record,
22
- RecordObjectMap,
23
- } from './RelayStoreTypes';
24
-
25
- const {EXISTENT, NONEXISTENT, UNKNOWN} = RelayRecordState;
26
-
27
- /**
28
- * An implementation of the `MutableRecordSource` interface (defined in
29
- * `RelayStoreTypes`) that holds all records in memory (JS Map).
30
- */
31
- class RelayMapRecordSourceMapImpl implements MutableRecordSource {
32
- _records: Map<DataID, ?Record>;
33
-
34
- constructor(records?: RecordObjectMap) {
35
- this._records = new Map();
36
- if (records != null) {
37
- Object.keys(records).forEach(key => {
38
- this._records.set(key, records[key]);
39
- });
40
- }
41
- }
42
-
43
- clear(): void {
44
- this._records = new Map();
45
- }
46
-
47
- delete(dataID: DataID): void {
48
- this._records.set(dataID, null);
49
- }
50
-
51
- get(dataID: DataID): ?Record {
52
- return this._records.get(dataID);
53
- }
54
-
55
- getRecordIDs(): Array<DataID> {
56
- return Array.from(this._records.keys());
57
- }
58
-
59
- getStatus(dataID: DataID): RecordState {
60
- if (!this._records.has(dataID)) {
61
- return UNKNOWN;
62
- }
63
- return this._records.get(dataID) == null ? NONEXISTENT : EXISTENT;
64
- }
65
-
66
- has(dataID: DataID): boolean {
67
- return this._records.has(dataID);
68
- }
69
-
70
- remove(dataID: DataID): void {
71
- this._records.delete(dataID);
72
- }
73
-
74
- set(dataID: DataID, record: Record): void {
75
- this._records.set(dataID, record);
76
- }
77
-
78
- size(): number {
79
- return this._records.size;
80
- }
81
-
82
- toJSON(): {[DataID]: ?Record, ...} {
83
- const obj = {};
84
- for (const [key, value] of this._records) {
85
- obj[key] = value;
86
- }
87
- return obj;
88
- }
89
- }
90
-
91
- module.exports = RelayMapRecordSourceMapImpl;