react-relay 2.0.0-rc.2 → 5.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 (166) hide show
  1. package/index.js +2 -2
  2. package/lib/ReactRelayContext.js +6 -3
  3. package/lib/ReactRelayFragmentContainer.js +48 -46
  4. package/lib/ReactRelayFragmentMockRenderer.js +5 -1
  5. package/lib/ReactRelayPaginationContainer.js +102 -68
  6. package/lib/ReactRelayQueryFetcher.js +54 -28
  7. package/lib/ReactRelayQueryRenderer.js +28 -20
  8. package/lib/ReactRelayRefetchContainer.js +70 -58
  9. package/lib/ReactRelayTestMocker.js +56 -43
  10. package/lib/ReactRelayTypes.js +1 -1
  11. package/lib/RelayContext.js +8 -2
  12. package/lib/assertFragmentMap.js +9 -7
  13. package/lib/buildReactRelayContainer.js +32 -23
  14. package/lib/index.js +41 -0
  15. package/lib/isRelayEnvironment.js +1 -1
  16. package/lib/isRelayVariables.js +1 -1
  17. package/lib/readContext.js +7 -4
  18. package/package.json +4 -4
  19. package/react-relay.js +2 -2
  20. package/react-relay.min.js +2 -2
  21. package/classic.js +0 -10
  22. package/compat.js +0 -10
  23. package/lib/ConcreteQuery.js +0 -10
  24. package/lib/GraphQLQueryRunner.js +0 -264
  25. package/lib/GraphQLRange.js +0 -1124
  26. package/lib/GraphQLSegment.js +0 -743
  27. package/lib/GraphQLStoreChangeEmitter.js +0 -141
  28. package/lib/GraphQLStoreQueryResolver.js +0 -288
  29. package/lib/GraphQLStoreRangeUtils.js +0 -126
  30. package/lib/QueryBuilder.js +0 -228
  31. package/lib/ReactRelayClassicExports.js +0 -29
  32. package/lib/ReactRelayCompatContainerBuilder.js +0 -175
  33. package/lib/ReactRelayCompatPublic.js +0 -28
  34. package/lib/ReactRelayContainerProfiler.js +0 -30
  35. package/lib/ReactRelayFragmentContainer-flowtest.js +0 -201
  36. package/lib/ReactRelayPaginationContainer-flowtest.js +0 -213
  37. package/lib/ReactRelayPublic.js +0 -29
  38. package/lib/ReactRelayRefetchContainer-flowtest.js +0 -191
  39. package/lib/RelayCacheProcessor.js +0 -196
  40. package/lib/RelayChangeTracker.js +0 -83
  41. package/lib/RelayClassicContainerUtils.js +0 -49
  42. package/lib/RelayClassicCore.js +0 -34
  43. package/lib/RelayClassicRecordState.js +0 -31
  44. package/lib/RelayCompatContainer.js +0 -25
  45. package/lib/RelayCompatEnvironment.js +0 -27
  46. package/lib/RelayCompatMutations.js +0 -133
  47. package/lib/RelayCompatPaginationContainer.js +0 -27
  48. package/lib/RelayCompatRefetchContainer.js +0 -27
  49. package/lib/RelayCompatTypes.js +0 -10
  50. package/lib/RelayContainer.js +0 -889
  51. package/lib/RelayContainerComparators.js +0 -75
  52. package/lib/RelayContainerProxy.js +0 -21
  53. package/lib/RelayDefaultNetworkLayer.js +0 -192
  54. package/lib/RelayEnvironment.js +0 -449
  55. package/lib/RelayEnvironmentTypes.js +0 -10
  56. package/lib/RelayFetchMode.js +0 -17
  57. package/lib/RelayFragmentPointer.js +0 -161
  58. package/lib/RelayFragmentReference.js +0 -249
  59. package/lib/RelayFragmentSpecResolver.js +0 -305
  60. package/lib/RelayGraphQLMutation.js +0 -288
  61. package/lib/RelayGraphQLTag.js +0 -42
  62. package/lib/RelayInternalTypes.js +0 -15
  63. package/lib/RelayInternals.js +0 -24
  64. package/lib/RelayMetaRoute.js +0 -35
  65. package/lib/RelayMockRenderer.js +0 -71
  66. package/lib/RelayModern-flowtest.js +0 -342
  67. package/lib/RelayModernFlowtest_badref.graphql.js +0 -10
  68. package/lib/RelayModernFlowtest_notref.graphql.js +0 -10
  69. package/lib/RelayModernFlowtest_user.graphql.js +0 -10
  70. package/lib/RelayModernFlowtest_users.graphql.js +0 -10
  71. package/lib/RelayMutation.js +0 -322
  72. package/lib/RelayMutationDebugPrinter.js +0 -47
  73. package/lib/RelayMutationQuery.js +0 -558
  74. package/lib/RelayMutationQueue.js +0 -530
  75. package/lib/RelayMutationRequest.js +0 -103
  76. package/lib/RelayMutationTracker.js +0 -113
  77. package/lib/RelayMutationTransaction.js +0 -92
  78. package/lib/RelayMutationTransactionStatus.js +0 -55
  79. package/lib/RelayNetworkDebug.js +0 -131
  80. package/lib/RelayNetworkLayer.js +0 -185
  81. package/lib/RelayNodeInterface.js +0 -104
  82. package/lib/RelayOperationDescriptor.js +0 -40
  83. package/lib/RelayOptimisticMutationUtils.js +0 -208
  84. package/lib/RelayPendingQueryTracker.js +0 -166
  85. package/lib/RelayPropTypes.js +0 -57
  86. package/lib/RelayPublic.js +0 -44
  87. package/lib/RelayQL.js +0 -128
  88. package/lib/RelayQuery.js +0 -1584
  89. package/lib/RelayQueryCaching.js +0 -33
  90. package/lib/RelayQueryConfig.js +0 -58
  91. package/lib/RelayQueryPath.js +0 -204
  92. package/lib/RelayQueryRequest.js +0 -103
  93. package/lib/RelayQueryTracker.js +0 -86
  94. package/lib/RelayQueryTransform.js +0 -91
  95. package/lib/RelayQueryVisitor.js +0 -93
  96. package/lib/RelayQueryWriter.js +0 -573
  97. package/lib/RelayReadyState.js +0 -83
  98. package/lib/RelayReadyStateRenderer.js +0 -145
  99. package/lib/RelayRecord.js +0 -75
  100. package/lib/RelayRecordStatusMap.js +0 -57
  101. package/lib/RelayRecordStore.js +0 -433
  102. package/lib/RelayRecordWriter.js +0 -601
  103. package/lib/RelayRefQueryDescriptor.js +0 -27
  104. package/lib/RelayRenderer.js +0 -268
  105. package/lib/RelayRootContainer.js +0 -125
  106. package/lib/RelayRoute.js +0 -92
  107. package/lib/RelayRouteFragment.js +0 -44
  108. package/lib/RelaySelector.js +0 -201
  109. package/lib/RelayShallowMock.js +0 -69
  110. package/lib/RelayStaticContainer.js +0 -38
  111. package/lib/RelayStore.js +0 -12
  112. package/lib/RelayStoreConstants.js +0 -20
  113. package/lib/RelayStoreData.js +0 -660
  114. package/lib/RelayTaskQueue.js +0 -189
  115. package/lib/RelayTypes.js +0 -13
  116. package/lib/RelayVariable.js +0 -32
  117. package/lib/RelayVariables.js +0 -92
  118. package/lib/buildRQL.js +0 -160
  119. package/lib/callsFromGraphQL.js +0 -74
  120. package/lib/callsToGraphQL.js +0 -34
  121. package/lib/checkRelayQueryData.js +0 -250
  122. package/lib/createRelayQuery.js +0 -17
  123. package/lib/dedent.js +0 -49
  124. package/lib/diffRelayQuery.js +0 -757
  125. package/lib/directivesToGraphQL.js +0 -39
  126. package/lib/filterRelayQuery.js +0 -29
  127. package/lib/findRelayQueryLeaves.js +0 -293
  128. package/lib/flattenRelayQuery.js +0 -121
  129. package/lib/flattenSplitRelayQueries.js +0 -44
  130. package/lib/forEachRootCallArg.js +0 -40
  131. package/lib/fromGraphQL.js +0 -48
  132. package/lib/generateClientEdgeID.js +0 -22
  133. package/lib/generateClientID.js +0 -27
  134. package/lib/generateConcreteFragmentID.js +0 -30
  135. package/lib/generateForceIndex.js +0 -24
  136. package/lib/generateRQLFieldAlias.js +0 -39
  137. package/lib/getRangeBehavior.js +0 -58
  138. package/lib/getRelayQueries.js +0 -83
  139. package/lib/intersectRelayQuery.js +0 -149
  140. package/lib/isClassicRelayContext.js +0 -20
  141. package/lib/isClassicRelayEnvironment.js +0 -20
  142. package/lib/isCompatibleRelayFragmentType.js +0 -27
  143. package/lib/isRelayContainer.js +0 -16
  144. package/lib/makeLegacyStringishComponentRef.js +0 -52
  145. package/lib/printRelayOSSQuery.js +0 -329
  146. package/lib/printRelayQuery.js +0 -26
  147. package/lib/rangeOperationToMetadataKey.js +0 -25
  148. package/lib/readRelayQueryData.js +0 -551
  149. package/lib/relayUnstableBatchedUpdates.js +0 -12
  150. package/lib/relayUnstableBatchedUpdates.native.js +0 -11
  151. package/lib/restoreRelayCacheData.js +0 -188
  152. package/lib/serializeRelayQueryCall.js +0 -42
  153. package/lib/splitDeferredRelayQueries.js +0 -298
  154. package/lib/stableStringify.js +0 -85
  155. package/lib/testEditDistance.js +0 -112
  156. package/lib/throwFailedPromise.js +0 -26
  157. package/lib/toGraphQL.js +0 -94
  158. package/lib/transformRelayQueryPayload.js +0 -145
  159. package/lib/validateMutationConfig.js +0 -117
  160. package/lib/validateRelayReadQuery.js +0 -112
  161. package/lib/writeRelayQueryPayload.js +0 -44
  162. package/lib/writeRelayUpdatePayload.js +0 -513
  163. package/react-relay-classic.js +0 -4
  164. package/react-relay-classic.min.js +0 -9
  165. package/react-relay-compat.js +0 -4
  166. package/react-relay-compat.min.js +0 -9
@@ -1,743 +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
- * @format
8
- */
9
- 'use strict';
10
-
11
- /**
12
- * Represents one contiguous segment of edges within a `GraphQLRange`. Has
13
- * methods for adding/removing edges (`appendEdge`, `prependEdge`, `removeEdge`)
14
- * and working with cursors (`getFirstCursor`, `getLastCursor` etc)
15
- *
16
- * Edges are never actually deleted from segments; they are merely marked as
17
- * being deleted. As such, `GraphQLSegment` offers both a `getCount` method
18
- * (returning the number of non-deleted edges) and a `getLength` method (which
19
- * returns the total number, including deleted edges).
20
- *
21
- * Used mostly as an implementation detail internal to `GraphQLRange`.
22
- *
23
- * @internal
24
- */
25
- var GraphQLSegment =
26
- /*#__PURE__*/
27
- function () {
28
- function GraphQLSegment() {
29
- // We use a map rather than an array because indices can become negative
30
- // when prepending.
31
- this._indexToMetadataMap = {}; // We keep track of past indices to ensure we can delete them completely.
32
-
33
- this._idToIndicesMap = {};
34
- this._cursorToIndexMap = {};
35
- this._count = 0;
36
- this._minIndex = null;
37
- this._maxIndex = null;
38
- }
39
- /**
40
- * @param {string} cursor
41
- * @return {?number}
42
- */
43
-
44
-
45
- var _proto = GraphQLSegment.prototype;
46
-
47
- _proto._getIndexForCursor = function _getIndexForCursor(cursor) {
48
- return this._cursorToIndexMap[cursor];
49
- };
50
- /**
51
- * @param {string} id
52
- * @return {?number}
53
- */
54
-
55
-
56
- _proto._getIndexForID = function _getIndexForID(id) {
57
- var indices = this._idToIndicesMap[id];
58
- return indices && indices[0];
59
- };
60
- /**
61
- * @return {?string} cursor for first non-deleted edge
62
- */
63
-
64
-
65
- _proto.getFirstCursor = function getFirstCursor() {
66
- if (this.getLength()) {
67
- for (var ii = this._minIndex; ii <= this._maxIndex; ii++) {
68
- var metadata = this._indexToMetadataMap[ii];
69
-
70
- if (!metadata.deleted) {
71
- return metadata.cursor;
72
- }
73
- }
74
- }
75
- };
76
- /**
77
- * @return {?string} cursor for last non-deleted edge
78
- */
79
-
80
-
81
- _proto.getLastCursor = function getLastCursor() {
82
- if (this.getLength()) {
83
- for (var ii = this._maxIndex; ii >= this._minIndex; ii--) {
84
- var metadata = this._indexToMetadataMap[ii];
85
-
86
- if (!metadata.deleted) {
87
- return metadata.cursor;
88
- }
89
- }
90
- }
91
- };
92
- /**
93
- * @return {boolean} returns whether cursor is before the first non-deleted
94
- * cursor inclusively.
95
- */
96
-
97
-
98
- _proto.isFirstCursor = function isFirstCursor(cursor) {
99
- if (this.getLength()) {
100
- for (var ii = this._minIndex; ii <= this._maxIndex; ii++) {
101
- var metadata = this._indexToMetadataMap[ii];
102
-
103
- if (!metadata.deleted) {
104
- return metadata.cursor === cursor;
105
- } else if (metadata.cursor === cursor) {
106
- return true;
107
- }
108
- }
109
-
110
- return false;
111
- }
112
- };
113
- /**
114
- * @return {boolean} returns whether cursor is after the last non-deleted
115
- * cursor inclusively.
116
- */
117
-
118
-
119
- _proto.isLastCursor = function isLastCursor(cursor) {
120
- if (this.getLength()) {
121
- for (var ii = this._maxIndex; ii >= this._minIndex; ii--) {
122
- var metadata = this._indexToMetadataMap[ii];
123
-
124
- if (!metadata.deleted) {
125
- return metadata.cursor === cursor;
126
- } else if (metadata.cursor === cursor) {
127
- return true;
128
- }
129
- }
130
-
131
- return false;
132
- }
133
- };
134
- /**
135
- * @return {?string} id for first non-deleted edge
136
- */
137
-
138
-
139
- _proto.getFirstID = function getFirstID() {
140
- if (this.getLength()) {
141
- for (var ii = this._minIndex; ii <= this._maxIndex; ii++) {
142
- var metadata = this._indexToMetadataMap[ii];
143
-
144
- if (!metadata.deleted) {
145
- return metadata.edgeID;
146
- }
147
- }
148
- }
149
- };
150
- /**
151
- * @return {?string} id for last non-deleted edge
152
- */
153
-
154
-
155
- _proto.getLastID = function getLastID() {
156
- if (this.getLength()) {
157
- for (var ii = this._maxIndex; ii >= this._minIndex; ii--) {
158
- var metadata = this._indexToMetadataMap[ii];
159
-
160
- if (!metadata.deleted) {
161
- return metadata.edgeID;
162
- }
163
- }
164
- }
165
- };
166
- /**
167
- * @param {number} index
168
- * @return {?object} Returns the not-deleted edge at index
169
- */
170
-
171
-
172
- _proto._getEdgeAtIndex = function _getEdgeAtIndex(index) {
173
- var edge = this._indexToMetadataMap[index];
174
- return edge && !edge.deleted ? edge : null;
175
- };
176
- /**
177
- * Returns whether there is a non-deleted edge for id
178
- * @param {string} id
179
- * @return {boolean}
180
- */
181
-
182
-
183
- _proto.containsEdgeWithID = function containsEdgeWithID(id) {
184
- var index = this._getIndexForID(id);
185
-
186
- if (index === undefined) {
187
- return false;
188
- }
189
-
190
- return !!this._getEdgeAtIndex(index);
191
- };
192
- /**
193
- * Returns whether there is a non-deleted edge for cursor
194
- * @param {string} cursor
195
- * @param {?boolean} includeDeleted
196
- * @return {boolean}
197
- */
198
-
199
-
200
- _proto.containsEdgeWithCursor = function containsEdgeWithCursor(cursor) {
201
- var includeDeleted = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
202
-
203
- var index = this._getIndexForCursor(cursor);
204
-
205
- if (index === undefined) {
206
- return false;
207
- } else if (includeDeleted) {
208
- return true;
209
- }
210
-
211
- return !!this._getEdgeAtIndex(index);
212
- };
213
- /**
214
- * Returns up to count number of ids and cursors that is after input cursor
215
- * @param {number} count
216
- * @param {?string} cursor
217
- * @return {object} object with arrays of ids and cursors
218
- */
219
-
220
-
221
- _proto.getMetadataAfterCursor = function getMetadataAfterCursor(count, cursor) {
222
- if (!this.getLength()) {
223
- return {
224
- edgeIDs: [],
225
- cursors: []
226
- };
227
- }
228
-
229
- var currentIndex = this._minIndex;
230
-
231
- if (cursor) {
232
- var index = this._getIndexForCursor(cursor);
233
-
234
- if (index === undefined) {
235
- console.warn('This segment does not have a cursor %s', cursor);
236
- return {
237
- edgeIDs: [],
238
- cursors: []
239
- };
240
- }
241
-
242
- currentIndex = index + 1;
243
- }
244
-
245
- var total = 0;
246
- var edgeIDs = [];
247
- var cursors = [];
248
-
249
- while (currentIndex <= this._maxIndex && total < count) {
250
- var metadata = this._indexToMetadataMap[currentIndex];
251
-
252
- if (!metadata.deleted) {
253
- edgeIDs.push(metadata.edgeID);
254
- cursors.push(metadata.cursor);
255
- total++;
256
- }
257
-
258
- currentIndex++;
259
- }
260
-
261
- return {
262
- edgeIDs: edgeIDs,
263
- cursors: cursors
264
- };
265
- };
266
- /**
267
- * Returns up to count number of ids and cursors that is before index
268
- * @param {number} count
269
- * @param {?string} cursor
270
- * @return {object} object with arrays of ids and cursors
271
- */
272
-
273
-
274
- _proto.getMetadataBeforeCursor = function getMetadataBeforeCursor(count, cursor) {
275
- if (!this.getLength()) {
276
- return {
277
- edgeIDs: [],
278
- cursors: []
279
- };
280
- }
281
-
282
- var currentIndex = this._maxIndex;
283
-
284
- if (cursor) {
285
- var index = this._getIndexForCursor(cursor);
286
-
287
- if (index === undefined) {
288
- console.warn('This segment does not have a cursor %s', cursor);
289
- return {
290
- edgeIDs: [],
291
- cursors: []
292
- };
293
- }
294
-
295
- currentIndex = index - 1;
296
- }
297
-
298
- var total = 0;
299
- var edgeIDs = [];
300
- var cursors = [];
301
-
302
- while (currentIndex >= this._minIndex && total < count) {
303
- var metadata = this._indexToMetadataMap[currentIndex];
304
-
305
- if (!metadata.deleted) {
306
- edgeIDs.push(metadata.edgeID);
307
- cursors.push(metadata.cursor);
308
- total++;
309
- }
310
-
311
- currentIndex--;
312
- } // Reverse edges because larger index were added first
313
-
314
-
315
- return {
316
- edgeIDs: edgeIDs.reverse(),
317
- cursors: cursors.reverse()
318
- };
319
- };
320
- /**
321
- * @param {object} edge
322
- * @param {number} index
323
- */
324
-
325
-
326
- _proto._addEdgeAtIndex = function _addEdgeAtIndex(edge, index) {
327
- var edgeID = require("./RelayRecord").getDataIDForObject(edge);
328
-
329
- var cursor = edge.cursor;
330
-
331
- var idIndex = this._getIndexForID(edgeID); // If the id is has an index and is not deleted
332
-
333
-
334
- if (idIndex !== undefined && this._getEdgeAtIndex(idIndex)) {
335
- console.warn('Attempted to add an ID already in GraphQLSegment: %s', edgeID);
336
- return;
337
- }
338
-
339
- if (this.getLength() === 0) {
340
- this._minIndex = index;
341
- this._maxIndex = index;
342
- } else if (this._minIndex === index + 1) {
343
- this._minIndex = index;
344
- } else if (this._maxIndex === index - 1) {
345
- this._maxIndex = index;
346
- } else {
347
- console.warn("Attempted to add noncontiguous index to GraphQLSegment: ".concat(index, " to ") + "(".concat(this._minIndex, ", ").concat(this._maxIndex, ")"));
348
- return;
349
- }
350
-
351
- this._indexToMetadataMap[index] = {
352
- edgeID: edgeID,
353
- cursor: cursor,
354
- deleted: false
355
- };
356
- this._idToIndicesMap[edgeID] = this._idToIndicesMap[edgeID] || [];
357
-
358
- this._idToIndicesMap[edgeID].unshift(index);
359
-
360
- this._count++;
361
-
362
- if (cursor) {
363
- this._cursorToIndexMap[cursor] = index;
364
- }
365
- };
366
- /**
367
- * @param {object} edge should have cursor and a node with id
368
- */
369
-
370
-
371
- _proto.prependEdge = function prependEdge(edge) {
372
- this._addEdgeAtIndex(edge, this._minIndex !== null ? this._minIndex - 1 : 0);
373
- };
374
- /**
375
- * @param {object} edge should have cursor and a node with id
376
- */
377
-
378
-
379
- _proto.appendEdge = function appendEdge(edge) {
380
- this._addEdgeAtIndex(edge, this._maxIndex !== null ? this._maxIndex + 1 : 0);
381
- };
382
- /**
383
- * Mark the currently valid edge with given id to be deleted.
384
- *
385
- * @param {string} id the id of the edge to be removed
386
- */
387
-
388
-
389
- _proto.removeEdge = function removeEdge(id) {
390
- var index = this._getIndexForID(id);
391
-
392
- if (index === undefined) {
393
- console.warn('Attempted to remove edge with ID that was never in GraphQLSegment: ' + id);
394
- return;
395
- }
396
-
397
- var data = this._indexToMetadataMap[index];
398
-
399
- if (data.deleted) {
400
- console.warn('Attempted to remove edge with ID that was already removed: ' + id);
401
- return;
402
- }
403
-
404
- data.deleted = true;
405
- this._count--;
406
- };
407
- /**
408
- * Mark all edges with given id to be deleted. This is used by
409
- * delete mutations to ensure both the current and past edges are no longer
410
- * accessible.
411
- *
412
- * @param {string} id the id of the edge to be removed
413
- */
414
-
415
-
416
- _proto.removeAllEdges = function removeAllEdges(id) {
417
- var indices = this._idToIndicesMap[id];
418
-
419
- if (!indices) {
420
- return;
421
- }
422
-
423
- for (var ii = 0; ii < indices.length; ii++) {
424
- var data = this._indexToMetadataMap[indices[ii]];
425
-
426
- if (!data.deleted) {
427
- data.deleted = true;
428
- this._count--;
429
- }
430
- }
431
- };
432
- /**
433
- * @param {array} edges
434
- * @param {?string} cursor
435
- */
436
-
437
-
438
- _proto.addEdgesAfterCursor = function addEdgesAfterCursor(edges, cursor) {
439
- if (!edges.length) {
440
- return;
441
- } // Default adding after with no cursor to -1
442
- // So the first element in the segment is stored at index 0
443
-
444
-
445
- var index = -1;
446
-
447
- if (cursor) {
448
- index = this._getIndexForCursor(cursor);
449
-
450
- if (index === undefined) {
451
- console.warn('This segment does not have a cursor %s', cursor);
452
- return;
453
- }
454
- }
455
-
456
- while (this._maxIndex !== null && index < this._maxIndex) {
457
- var data = this._indexToMetadataMap[index + 1]; // Skip over elements that have been deleted
458
- // so we can add new edges on the end.
459
-
460
- if (data.deleted) {
461
- index++;
462
- } else {
463
- console.warn('Attempted to do an overwrite to GraphQLSegment: ' + 'last index is ' + this._maxIndex + ' trying to add edges before ' + index);
464
- return;
465
- }
466
- }
467
-
468
- var startIndex = index + 1;
469
-
470
- for (var ii = 0; ii < edges.length; ii++) {
471
- var edge = edges[ii];
472
-
473
- this._addEdgeAtIndex(edge, startIndex + ii);
474
- }
475
- };
476
- /**
477
- * @param {array} edges - should be in increasing order of index
478
- * @param {?string} cursor
479
- */
480
-
481
-
482
- _proto.addEdgesBeforeCursor = function addEdgesBeforeCursor(edges, cursor) {
483
- if (!edges.length) {
484
- return;
485
- } // Default adding before with no cursor to 1
486
- // So the first element in the segment is stored at index 0
487
-
488
-
489
- var index = 1;
490
-
491
- if (cursor) {
492
- index = this._getIndexForCursor(cursor);
493
-
494
- if (index === undefined) {
495
- console.warn('This segment does not have a cursor %s', cursor);
496
- return;
497
- }
498
- }
499
-
500
- while (this._minIndex !== null && index > this._minIndex) {
501
- var data = this._indexToMetadataMap[index - 1]; // Skip over elements that have been deleted
502
- // so we can add new edges in the front.
503
-
504
- if (data.deleted) {
505
- index--;
506
- } else {
507
- console.warn('Attempted to do an overwrite to GraphQLSegment: ' + 'first index is ' + this._minIndex + ' trying to add edges after ' + index);
508
- return;
509
- }
510
- } // Edges must be added in reverse order since the
511
- // segment must be continuous at all times.
512
-
513
-
514
- var startIndex = index - 1;
515
-
516
- for (var ii = 0; ii < edges.length; ii++) {
517
- // Iterates from edges.length - 1 to 0
518
- var edge = edges[edges.length - ii - 1];
519
-
520
- this._addEdgeAtIndex(edge, startIndex - ii);
521
- }
522
- };
523
- /**
524
- * This is the total length of the segment including the deleted edges.
525
- * Non-zero length guarantees value max and min indices.
526
- * DO NOT USE THIS TO DETERMINE THE TOTAL NUMBER OF EDGES; use `getCount`
527
- * instead.
528
- * @return {number}
529
- */
530
-
531
-
532
- _proto.getLength = function getLength() {
533
- if (this._minIndex === null && this._maxIndex === null) {
534
- return 0;
535
- }
536
-
537
- return this._maxIndex - this._minIndex + 1;
538
- };
539
- /**
540
- * Returns the total number of non-deleted edges in the segment.
541
- *
542
- * @return {number}
543
- */
544
-
545
-
546
- _proto.getCount = function getCount() {
547
- return this._count;
548
- };
549
- /**
550
- * In the event of a failed `concatSegment` operation, rollback internal
551
- * properties to their former values.
552
- *
553
- * @param {object} cursorRollbackMap
554
- * @param {object} idRollbackMap
555
- * @param {object} counters
556
- */
557
-
558
-
559
- _proto._rollback = function _rollback(cursorRollbackMap, idRollbackMap, counters) {
560
- Object.assign(this._cursorToIndexMap, cursorRollbackMap);
561
- Object.assign(this._idToIndicesMap, idRollbackMap); // no need to reset _indexToMetadataMap; resetting counters is enough
562
-
563
- this._count = counters.count;
564
- this._maxIndex = counters.maxIndex;
565
- this._minIndex = counters.minIndex;
566
- };
567
- /**
568
- * @return {object} Captured counter state.
569
- */
570
-
571
-
572
- _proto._getCounterState = function _getCounterState() {
573
- return {
574
- count: this._count,
575
- maxIndex: this._maxIndex,
576
- minIndex: this._minIndex
577
- };
578
- };
579
- /**
580
- * Copies over content of the input segment and add to the current
581
- * segment.
582
- * @param {GraphQLSegment} segment - the segment to be copied over
583
- * @return {boolean} whether or not we successfully concatenated the segments
584
- */
585
-
586
-
587
- _proto.concatSegment = function concatSegment(segment) {
588
- if (!segment.getLength()) {
589
- return true;
590
- }
591
-
592
- var idRollbackMap = {};
593
- var cursorRollbackMap = {};
594
-
595
- var counterState = this._getCounterState();
596
-
597
- var newEdges = segment._indexToMetadataMap;
598
-
599
- for (var ii = segment._minIndex; ii <= segment._maxIndex; ii++) {
600
- var index = void 0;
601
-
602
- if (this.getLength()) {
603
- index = this._maxIndex + 1;
604
- } else {
605
- index = 0;
606
- this._minIndex = 0;
607
- }
608
-
609
- this._maxIndex = index;
610
- var newEdge = newEdges[ii];
611
-
612
- var idIndex = this._getIndexForID(newEdge.edgeID);
613
-
614
- if (!idRollbackMap.hasOwnProperty(newEdge.edgeID)) {
615
- if (this._idToIndicesMap[newEdge.edgeID]) {
616
- idRollbackMap[newEdge.edgeID] = this._idToIndicesMap[newEdge.edgeID].slice();
617
- } else {
618
- idRollbackMap[newEdge.edgeID] = undefined;
619
- }
620
- } // Check for id collision. Can't have same id twice
621
-
622
-
623
- if (idIndex !== undefined) {
624
- var idEdge = this._indexToMetadataMap[idIndex];
625
-
626
- if (idEdge.deleted && !newEdge.deleted) {
627
- // We want to map to most recent edge. Only write to the front of map
628
- // if existing edge with id is deleted or have an older deletion
629
- // time.
630
- this._idToIndicesMap[newEdge.edgeID].unshift(index);
631
- } else if (!newEdge.deleted) {
632
- console.warn('Attempt to concat an ID already in GraphQLSegment: %s', newEdge.edgeID);
633
-
634
- this._rollback(cursorRollbackMap, idRollbackMap, counterState);
635
-
636
- return false;
637
- } else {
638
- // We want to keep track of past edges as well. Write these indices
639
- // to the end of the array.
640
- this._idToIndicesMap[newEdge.edgeID] = this._idToIndicesMap[newEdge.edgeID] || [];
641
-
642
- this._idToIndicesMap[newEdge.edgeID].push(index);
643
- }
644
- } else {
645
- this._idToIndicesMap[newEdge.edgeID] = this._idToIndicesMap[newEdge.edgeID] || [];
646
-
647
- this._idToIndicesMap[newEdge.edgeID].unshift(index);
648
- }
649
-
650
- var cursorIndex = this._getIndexForCursor(newEdge.cursor); // Check for cursor collision. Can't have same cursor twice
651
-
652
-
653
- if (cursorIndex !== undefined) {
654
- var cursorEdge = this._indexToMetadataMap[cursorIndex];
655
-
656
- if (cursorEdge.deleted && !newEdge.deleted) {
657
- // We want to map to most recent edge. Only write in the cursor map if
658
- // existing edge with cursor is deleted or have and older deletion
659
- // time.
660
- cursorRollbackMap[newEdge.cursor] = this._cursorToIndexMap[newEdge.cursor];
661
- this._cursorToIndexMap[newEdge.cursor] = index;
662
- } else if (!newEdge.deleted) {
663
- console.warn('Attempt to concat a cursor already in GraphQLSegment: %s', newEdge.cursor);
664
-
665
- this._rollback(cursorRollbackMap, idRollbackMap, counterState);
666
-
667
- return false;
668
- }
669
- } else if (newEdge.cursor) {
670
- cursorRollbackMap[newEdge.cursor] = this._cursorToIndexMap[newEdge.cursor];
671
- this._cursorToIndexMap[newEdge.cursor] = index;
672
- }
673
-
674
- if (!newEdge.deleted) {
675
- this._count++;
676
- }
677
-
678
- this._indexToMetadataMap[index] = Object.assign({}, newEdge);
679
- }
680
-
681
- return true;
682
- };
683
-
684
- _proto.toJSON = function toJSON() {
685
- return [this._indexToMetadataMap, this._idToIndicesMap, this._cursorToIndexMap, this._minIndex, this._maxIndex, this._count];
686
- };
687
-
688
- GraphQLSegment.fromJSON = function fromJSON(descriptor) {
689
- var indexToMetadataMap = descriptor[0],
690
- idToIndicesMap = descriptor[1],
691
- cursorToIndexMap = descriptor[2],
692
- minIndex = descriptor[3],
693
- maxIndex = descriptor[4],
694
- count = descriptor[5];
695
- var segment = new GraphQLSegment();
696
- segment._indexToMetadataMap = indexToMetadataMap;
697
- segment._idToIndicesMap = idToIndicesMap;
698
- segment._cursorToIndexMap = cursorToIndexMap;
699
- segment._minIndex = minIndex;
700
- segment._maxIndex = maxIndex;
701
- segment._count = count;
702
- return segment;
703
- };
704
-
705
- _proto.__debug = function __debug() {
706
- return {
707
- metadata: this._indexToMetadataMap,
708
- idToIndices: this._idToIndicesMap,
709
- cursorToIndex: this._cursorToIndexMap
710
- };
711
- };
712
- /**
713
- * Returns a list of all IDs that were registered for this segment.
714
- */
715
-
716
-
717
- _proto.getEdgeIDs = function getEdgeIDs() {
718
- var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {
719
- includeDeleted: false
720
- },
721
- includeDeleted = _ref.includeDeleted;
722
-
723
- var edgeIds = [];
724
-
725
- if (this.getLength() > 0) {
726
- for (var ii = this._minIndex; ii <= this._maxIndex; ii++) {
727
- var _this$_indexToMetadat = this._indexToMetadataMap[ii],
728
- deleted = _this$_indexToMetadat.deleted,
729
- edgeID = _this$_indexToMetadat.edgeID;
730
-
731
- if (includeDeleted || !deleted) {
732
- edgeIds.push(edgeID);
733
- }
734
- }
735
- }
736
-
737
- return edgeIds;
738
- };
739
-
740
- return GraphQLSegment;
741
- }();
742
-
743
- module.exports = GraphQLSegment;