@robohall/react-query-factory 2.2.0 → 2.2.1
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.
- package/dist/index.js +17 -1
- package/dist/index.mjs +17 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -111,6 +111,21 @@ function buildCrawlingQueryFn(queryFn, getNextPageParam, initialPageParam, shoul
|
|
|
111
111
|
return pages;
|
|
112
112
|
};
|
|
113
113
|
}
|
|
114
|
+
function buildAutoConsumeQueryFn(queryFn, reduce) {
|
|
115
|
+
return async (params, context) => {
|
|
116
|
+
var _a;
|
|
117
|
+
const result = await queryFn(params, context);
|
|
118
|
+
if (!isAsyncIterable(result)) return result;
|
|
119
|
+
const pages = [];
|
|
120
|
+
let acc = void 0;
|
|
121
|
+
for await (const page of result) {
|
|
122
|
+
if ((_a = context.signal) == null ? void 0 : _a.aborted) break;
|
|
123
|
+
pages.push(page);
|
|
124
|
+
if (reduce) acc = reduce(acc, page);
|
|
125
|
+
}
|
|
126
|
+
return reduce ? acc !== void 0 ? acc : pages : pages;
|
|
127
|
+
};
|
|
128
|
+
}
|
|
114
129
|
function buildInfiniteCrawlingQueryFn(queryFn, getNextPageParam, shouldFetchNextPage, reduce) {
|
|
115
130
|
return async (params, crawlOptions, context) => {
|
|
116
131
|
var _a, _b, _c;
|
|
@@ -184,6 +199,7 @@ function buildFactory(cfg) {
|
|
|
184
199
|
shouldFetchNextPage,
|
|
185
200
|
reduce
|
|
186
201
|
) : void 0;
|
|
202
|
+
const autoConsumeFn = !hasCrawling && rawQueryFn !== void 0 ? buildAutoConsumeQueryFn(rawQueryFn, reduce) : void 0;
|
|
187
203
|
const infiniteCrawlingFn = hasInfiniteCrawling ? buildInfiniteCrawlingQueryFn(
|
|
188
204
|
rawQueryFn,
|
|
189
205
|
getNextPageParam,
|
|
@@ -200,7 +216,7 @@ function buildFactory(cfg) {
|
|
|
200
216
|
}) : void 0;
|
|
201
217
|
const factory = function(params, crawlOptions = {}) {
|
|
202
218
|
const queryKey = parentKey !== void 0 ? buildChildKey(parentKey, ownSegments, params, crawlOptions) : resolveKey(namespace, params, crawlOptions);
|
|
203
|
-
const resolvedQueryFn = crawlingFn ? (ctx) => crawlingFn(params, crawlOptions, ctx) :
|
|
219
|
+
const resolvedQueryFn = crawlingFn ? (ctx) => crawlingFn(params, crawlOptions, ctx) : autoConsumeFn ? (ctx) => autoConsumeFn(params, ctx) : void 0;
|
|
204
220
|
return {
|
|
205
221
|
...standardOptions,
|
|
206
222
|
queryKey,
|
package/dist/index.mjs
CHANGED
|
@@ -85,6 +85,21 @@ function buildCrawlingQueryFn(queryFn, getNextPageParam, initialPageParam, shoul
|
|
|
85
85
|
return pages;
|
|
86
86
|
};
|
|
87
87
|
}
|
|
88
|
+
function buildAutoConsumeQueryFn(queryFn, reduce) {
|
|
89
|
+
return async (params, context) => {
|
|
90
|
+
var _a;
|
|
91
|
+
const result = await queryFn(params, context);
|
|
92
|
+
if (!isAsyncIterable(result)) return result;
|
|
93
|
+
const pages = [];
|
|
94
|
+
let acc = void 0;
|
|
95
|
+
for await (const page of result) {
|
|
96
|
+
if ((_a = context.signal) == null ? void 0 : _a.aborted) break;
|
|
97
|
+
pages.push(page);
|
|
98
|
+
if (reduce) acc = reduce(acc, page);
|
|
99
|
+
}
|
|
100
|
+
return reduce ? acc !== void 0 ? acc : pages : pages;
|
|
101
|
+
};
|
|
102
|
+
}
|
|
88
103
|
function buildInfiniteCrawlingQueryFn(queryFn, getNextPageParam, shouldFetchNextPage, reduce) {
|
|
89
104
|
return async (params, crawlOptions, context) => {
|
|
90
105
|
var _a, _b, _c;
|
|
@@ -158,6 +173,7 @@ function buildFactory(cfg) {
|
|
|
158
173
|
shouldFetchNextPage,
|
|
159
174
|
reduce
|
|
160
175
|
) : void 0;
|
|
176
|
+
const autoConsumeFn = !hasCrawling && rawQueryFn !== void 0 ? buildAutoConsumeQueryFn(rawQueryFn, reduce) : void 0;
|
|
161
177
|
const infiniteCrawlingFn = hasInfiniteCrawling ? buildInfiniteCrawlingQueryFn(
|
|
162
178
|
rawQueryFn,
|
|
163
179
|
getNextPageParam,
|
|
@@ -174,7 +190,7 @@ function buildFactory(cfg) {
|
|
|
174
190
|
}) : void 0;
|
|
175
191
|
const factory = function(params, crawlOptions = {}) {
|
|
176
192
|
const queryKey = parentKey !== void 0 ? buildChildKey(parentKey, ownSegments, params, crawlOptions) : resolveKey(namespace, params, crawlOptions);
|
|
177
|
-
const resolvedQueryFn = crawlingFn ? (ctx) => crawlingFn(params, crawlOptions, ctx) :
|
|
193
|
+
const resolvedQueryFn = crawlingFn ? (ctx) => crawlingFn(params, crawlOptions, ctx) : autoConsumeFn ? (ctx) => autoConsumeFn(params, ctx) : void 0;
|
|
178
194
|
return {
|
|
179
195
|
...standardOptions,
|
|
180
196
|
queryKey,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@robohall/react-query-factory",
|
|
3
|
-
"version": "2.2.
|
|
3
|
+
"version": "2.2.1",
|
|
4
4
|
"description": "A factory abstraction for TanStack Query (React Query) with composable keys, crawling support, and automatic infinite query generation",
|
|
5
5
|
"author": "Robert Hall",
|
|
6
6
|
"license": "MIT",
|