1 | import { __assign, __rest as __rest_1, __spreadArray } from "tslib";
|
2 | import { __rest } from "tslib";
|
3 | import { mergeDeep } from "../common/mergeDeep.js";
|
4 | export function concatPagination(keyArgs) {
|
5 | if (keyArgs === void 0) { keyArgs = false; }
|
6 | return {
|
7 | keyArgs: keyArgs,
|
8 | merge: function (existing, incoming) {
|
9 | return existing ? __spreadArray(__spreadArray([], existing, true), incoming, true) : incoming;
|
10 | },
|
11 | };
|
12 | }
|
13 | export function offsetLimitPagination(keyArgs) {
|
14 | if (keyArgs === void 0) { keyArgs = false; }
|
15 | return {
|
16 | keyArgs: keyArgs,
|
17 | merge: function (existing, incoming, _a) {
|
18 | var args = _a.args;
|
19 | var merged = existing ? existing.slice(0) : [];
|
20 | if (incoming) {
|
21 | if (args) {
|
22 | var _b = args.offset, offset = _b === void 0 ? 0 : _b;
|
23 | for (var i = 0; i < incoming.length; ++i) {
|
24 | merged[offset + i] = incoming[i];
|
25 | }
|
26 | }
|
27 | else {
|
28 | merged.push.apply(merged, incoming);
|
29 | }
|
30 | }
|
31 | return merged;
|
32 | },
|
33 | };
|
34 | }
|
35 | export function relayStylePagination(keyArgs) {
|
36 | if (keyArgs === void 0) { keyArgs = false; }
|
37 | return {
|
38 | keyArgs: keyArgs,
|
39 | read: function (existing, _a) {
|
40 | var canRead = _a.canRead, readField = _a.readField;
|
41 | if (!existing)
|
42 | return existing;
|
43 | var edges = [];
|
44 | var firstEdgeCursor = "";
|
45 | var lastEdgeCursor = "";
|
46 | existing.edges.forEach(function (edge) {
|
47 | if (canRead(readField("node", edge))) {
|
48 | edges.push(edge);
|
49 | if (edge.cursor) {
|
50 | firstEdgeCursor = firstEdgeCursor || edge.cursor || "";
|
51 | lastEdgeCursor = edge.cursor || lastEdgeCursor;
|
52 | }
|
53 | }
|
54 | });
|
55 | var _b = existing.pageInfo || {}, startCursor = _b.startCursor, endCursor = _b.endCursor;
|
56 | return __assign(__assign({}, getExtras(existing)), { edges: edges, pageInfo: __assign(__assign({}, existing.pageInfo), { startCursor: startCursor || firstEdgeCursor, endCursor: endCursor || lastEdgeCursor }) });
|
57 | },
|
58 | merge: function (existing, incoming, _a) {
|
59 | var args = _a.args, isReference = _a.isReference, readField = _a.readField;
|
60 | if (!existing) {
|
61 | existing = makeEmptyData();
|
62 | }
|
63 | if (!incoming) {
|
64 | return existing;
|
65 | }
|
66 | var incomingEdges = incoming.edges ? incoming.edges.map(function (edge) {
|
67 | if (isReference(edge = __assign({}, edge))) {
|
68 | edge.cursor = readField("cursor", edge);
|
69 | }
|
70 | return edge;
|
71 | }) : [];
|
72 | if (incoming.pageInfo) {
|
73 | var pageInfo_1 = incoming.pageInfo;
|
74 | var startCursor = pageInfo_1.startCursor, endCursor = pageInfo_1.endCursor;
|
75 | var firstEdge = incomingEdges[0];
|
76 | var lastEdge = incomingEdges[incomingEdges.length - 1];
|
77 | if (firstEdge && startCursor) {
|
78 | firstEdge.cursor = startCursor;
|
79 | }
|
80 | if (lastEdge && endCursor) {
|
81 | lastEdge.cursor = endCursor;
|
82 | }
|
83 | var firstCursor = firstEdge && firstEdge.cursor;
|
84 | if (firstCursor && !startCursor) {
|
85 | incoming = mergeDeep(incoming, {
|
86 | pageInfo: {
|
87 | startCursor: firstCursor,
|
88 | },
|
89 | });
|
90 | }
|
91 | var lastCursor = lastEdge && lastEdge.cursor;
|
92 | if (lastCursor && !endCursor) {
|
93 | incoming = mergeDeep(incoming, {
|
94 | pageInfo: {
|
95 | endCursor: lastCursor,
|
96 | },
|
97 | });
|
98 | }
|
99 | }
|
100 | var prefix = existing.edges;
|
101 | var suffix = [];
|
102 | if (args && args.after) {
|
103 | var index = prefix.findIndex(function (edge) { return edge.cursor === args.after; });
|
104 | if (index >= 0) {
|
105 | prefix = prefix.slice(0, index + 1);
|
106 | }
|
107 | }
|
108 | else if (args && args.before) {
|
109 | var index = prefix.findIndex(function (edge) { return edge.cursor === args.before; });
|
110 | suffix = index < 0 ? prefix : prefix.slice(index);
|
111 | prefix = [];
|
112 | }
|
113 | else if (incoming.edges) {
|
114 | prefix = [];
|
115 | }
|
116 | var edges = __spreadArray(__spreadArray(__spreadArray([], prefix, true), incomingEdges, true), suffix, true);
|
117 | var pageInfo = __assign(__assign({}, incoming.pageInfo), existing.pageInfo);
|
118 | if (incoming.pageInfo) {
|
119 | var _b = incoming.pageInfo, hasPreviousPage = _b.hasPreviousPage, hasNextPage = _b.hasNextPage, startCursor = _b.startCursor, endCursor = _b.endCursor, extras = __rest_1(_b, ["hasPreviousPage", "hasNextPage", "startCursor", "endCursor"]);
|
120 | Object.assign(pageInfo, extras);
|
121 | if (!prefix.length) {
|
122 | if (void 0 !== hasPreviousPage)
|
123 | pageInfo.hasPreviousPage = hasPreviousPage;
|
124 | if (void 0 !== startCursor)
|
125 | pageInfo.startCursor = startCursor;
|
126 | }
|
127 | if (!suffix.length) {
|
128 | if (void 0 !== hasNextPage)
|
129 | pageInfo.hasNextPage = hasNextPage;
|
130 | if (void 0 !== endCursor)
|
131 | pageInfo.endCursor = endCursor;
|
132 | }
|
133 | }
|
134 | return __assign(__assign(__assign({}, getExtras(existing)), getExtras(incoming)), { edges: edges, pageInfo: pageInfo });
|
135 | },
|
136 | };
|
137 | }
|
138 | var getExtras = function (obj) { return __rest(obj, notExtras); };
|
139 | var notExtras = ["edges", "pageInfo"];
|
140 | function makeEmptyData() {
|
141 | return {
|
142 | edges: [],
|
143 | pageInfo: {
|
144 | hasPreviousPage: false,
|
145 | hasNextPage: true,
|
146 | startCursor: "",
|
147 | endCursor: "",
|
148 | },
|
149 | };
|
150 | }
|
151 |
|
\ | No newline at end of file |