@tanstack/query-core 5.76.2 → 5.77.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.
- package/build/legacy/streamedQuery.cjs +6 -4
- package/build/legacy/streamedQuery.cjs.map +1 -1
- package/build/legacy/streamedQuery.d.cts +7 -2
- package/build/legacy/streamedQuery.d.ts +7 -2
- package/build/legacy/streamedQuery.js +6 -4
- package/build/legacy/streamedQuery.js.map +1 -1
- package/build/modern/streamedQuery.cjs +6 -4
- package/build/modern/streamedQuery.cjs.map +1 -1
- package/build/modern/streamedQuery.d.cts +7 -2
- package/build/modern/streamedQuery.d.ts +7 -2
- package/build/modern/streamedQuery.js +6 -4
- package/build/modern/streamedQuery.js.map +1 -1
- package/package.json +1 -1
- package/src/streamedQuery.ts +11 -4
|
@@ -23,9 +23,11 @@ __export(streamedQuery_exports, {
|
|
|
23
23
|
streamedQuery: () => streamedQuery
|
|
24
24
|
});
|
|
25
25
|
module.exports = __toCommonJS(streamedQuery_exports);
|
|
26
|
+
var import_utils = require("./utils.cjs");
|
|
26
27
|
function streamedQuery({
|
|
27
28
|
queryFn,
|
|
28
|
-
refetchMode = "reset"
|
|
29
|
+
refetchMode = "reset",
|
|
30
|
+
maxChunks
|
|
29
31
|
}) {
|
|
30
32
|
return async (context) => {
|
|
31
33
|
const query = context.client.getQueryCache().find({ queryKey: context.queryKey, exact: true });
|
|
@@ -38,7 +40,7 @@ function streamedQuery({
|
|
|
38
40
|
fetchStatus: "fetching"
|
|
39
41
|
});
|
|
40
42
|
}
|
|
41
|
-
|
|
43
|
+
let result = [];
|
|
42
44
|
const stream = await queryFn(context);
|
|
43
45
|
for await (const chunk of stream) {
|
|
44
46
|
if (context.signal.aborted) {
|
|
@@ -48,11 +50,11 @@ function streamedQuery({
|
|
|
48
50
|
context.client.setQueryData(
|
|
49
51
|
context.queryKey,
|
|
50
52
|
(prev = []) => {
|
|
51
|
-
return
|
|
53
|
+
return (0, import_utils.addToEnd)(prev, chunk, maxChunks);
|
|
52
54
|
}
|
|
53
55
|
);
|
|
54
56
|
}
|
|
55
|
-
result.
|
|
57
|
+
result = (0, import_utils.addToEnd)(result, chunk, maxChunks);
|
|
56
58
|
}
|
|
57
59
|
if (isRefetch && refetchMode === "replace" && !context.signal.aborted) {
|
|
58
60
|
context.client.setQueryData(context.queryKey, result);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/streamedQuery.ts"],"sourcesContent":["import type { QueryFunction, QueryFunctionContext, QueryKey } from './types'\n\n/**\n * This is a helper function to create a query function that streams data from an AsyncIterable.\n * Data will be an Array of all the chunks received.\n * The query will be in a 'pending' state until the first chunk of data is received, but will go to 'success' after that.\n * The query will stay in fetchStatus 'fetching' until the stream ends.\n * @param queryFn - The function that returns an AsyncIterable to stream data from.\n * @param refetchMode - Defines how re-fetches are handled.\n * Defaults to `'reset'`, erases all data and puts the query back into `pending` state.\n * Set to `'append'` to append new data to the existing data.\n * Set to `'replace'` to write
|
|
1
|
+
{"version":3,"sources":["../../src/streamedQuery.ts"],"sourcesContent":["import { addToEnd } from './utils'\nimport type { QueryFunction, QueryFunctionContext, QueryKey } from './types'\n\n/**\n * This is a helper function to create a query function that streams data from an AsyncIterable.\n * Data will be an Array of all the chunks received.\n * The query will be in a 'pending' state until the first chunk of data is received, but will go to 'success' after that.\n * The query will stay in fetchStatus 'fetching' until the stream ends.\n * @param queryFn - The function that returns an AsyncIterable to stream data from.\n * @param refetchMode - Defines how re-fetches are handled.\n * Defaults to `'reset'`, erases all data and puts the query back into `pending` state.\n * Set to `'append'` to append new data to the existing data.\n * Set to `'replace'` to write all data to the cache once the stream ends.\n * @param maxChunks - The maximum number of chunks to keep in the cache.\n * Defaults to `undefined`, meaning all chunks will be kept.\n * If `undefined` or `0`, the number of chunks is unlimited.\n * If the number of chunks exceeds this number, the oldest chunk will be removed.\n */\nexport function streamedQuery<\n TQueryFnData = unknown,\n TQueryKey extends QueryKey = QueryKey,\n>({\n queryFn,\n refetchMode = 'reset',\n maxChunks,\n}: {\n queryFn: (\n context: QueryFunctionContext<TQueryKey>,\n ) => AsyncIterable<TQueryFnData> | Promise<AsyncIterable<TQueryFnData>>\n refetchMode?: 'append' | 'reset' | 'replace'\n maxChunks?: number\n}): QueryFunction<Array<TQueryFnData>, TQueryKey> {\n return async (context) => {\n const query = context.client\n .getQueryCache()\n .find({ queryKey: context.queryKey, exact: true })\n const isRefetch = !!query && query.state.data !== undefined\n\n if (isRefetch && refetchMode === 'reset') {\n query.setState({\n status: 'pending',\n data: undefined,\n error: null,\n fetchStatus: 'fetching',\n })\n }\n\n let result: Array<TQueryFnData> = []\n const stream = await queryFn(context)\n\n for await (const chunk of stream) {\n if (context.signal.aborted) {\n break\n }\n\n // don't append to the cache directly when replace-refetching\n if (!isRefetch || refetchMode !== 'replace') {\n context.client.setQueryData<Array<TQueryFnData>>(\n context.queryKey,\n (prev = []) => {\n return addToEnd(prev, chunk, maxChunks)\n },\n )\n }\n result = addToEnd(result, chunk, maxChunks)\n }\n\n // finalize result: replace-refetching needs to write to the cache\n if (isRefetch && refetchMode === 'replace' && !context.signal.aborted) {\n context.client.setQueryData<Array<TQueryFnData>>(context.queryKey, result)\n }\n\n return context.client.getQueryData(context.queryKey)!\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,mBAAyB;AAkBlB,SAAS,cAGd;AAAA,EACA;AAAA,EACA,cAAc;AAAA,EACd;AACF,GAMkD;AAChD,SAAO,OAAO,YAAY;AACxB,UAAM,QAAQ,QAAQ,OACnB,cAAc,EACd,KAAK,EAAE,UAAU,QAAQ,UAAU,OAAO,KAAK,CAAC;AACnD,UAAM,YAAY,CAAC,CAAC,SAAS,MAAM,MAAM,SAAS;AAElD,QAAI,aAAa,gBAAgB,SAAS;AACxC,YAAM,SAAS;AAAA,QACb,QAAQ;AAAA,QACR,MAAM;AAAA,QACN,OAAO;AAAA,QACP,aAAa;AAAA,MACf,CAAC;AAAA,IACH;AAEA,QAAI,SAA8B,CAAC;AACnC,UAAM,SAAS,MAAM,QAAQ,OAAO;AAEpC,qBAAiB,SAAS,QAAQ;AAChC,UAAI,QAAQ,OAAO,SAAS;AAC1B;AAAA,MACF;AAGA,UAAI,CAAC,aAAa,gBAAgB,WAAW;AAC3C,gBAAQ,OAAO;AAAA,UACb,QAAQ;AAAA,UACR,CAAC,OAAO,CAAC,MAAM;AACb,uBAAO,uBAAS,MAAM,OAAO,SAAS;AAAA,UACxC;AAAA,QACF;AAAA,MACF;AACA,mBAAS,uBAAS,QAAQ,OAAO,SAAS;AAAA,IAC5C;AAGA,QAAI,aAAa,gBAAgB,aAAa,CAAC,QAAQ,OAAO,SAAS;AACrE,cAAQ,OAAO,aAAkC,QAAQ,UAAU,MAAM;AAAA,IAC3E;AAEA,WAAO,QAAQ,OAAO,aAAa,QAAQ,QAAQ;AAAA,EACrD;AACF;","names":[]}
|
|
@@ -11,11 +11,16 @@ import './subscribable.cjs';
|
|
|
11
11
|
* @param refetchMode - Defines how re-fetches are handled.
|
|
12
12
|
* Defaults to `'reset'`, erases all data and puts the query back into `pending` state.
|
|
13
13
|
* Set to `'append'` to append new data to the existing data.
|
|
14
|
-
* Set to `'replace'` to write
|
|
14
|
+
* Set to `'replace'` to write all data to the cache once the stream ends.
|
|
15
|
+
* @param maxChunks - The maximum number of chunks to keep in the cache.
|
|
16
|
+
* Defaults to `undefined`, meaning all chunks will be kept.
|
|
17
|
+
* If `undefined` or `0`, the number of chunks is unlimited.
|
|
18
|
+
* If the number of chunks exceeds this number, the oldest chunk will be removed.
|
|
15
19
|
*/
|
|
16
|
-
declare function streamedQuery<TQueryFnData = unknown, TQueryKey extends QueryKey = QueryKey>({ queryFn, refetchMode, }: {
|
|
20
|
+
declare function streamedQuery<TQueryFnData = unknown, TQueryKey extends QueryKey = QueryKey>({ queryFn, refetchMode, maxChunks, }: {
|
|
17
21
|
queryFn: (context: QueryFunctionContext<TQueryKey>) => AsyncIterable<TQueryFnData> | Promise<AsyncIterable<TQueryFnData>>;
|
|
18
22
|
refetchMode?: 'append' | 'reset' | 'replace';
|
|
23
|
+
maxChunks?: number;
|
|
19
24
|
}): QueryFunction<Array<TQueryFnData>, TQueryKey>;
|
|
20
25
|
|
|
21
26
|
export { streamedQuery };
|
|
@@ -11,11 +11,16 @@ import './subscribable.js';
|
|
|
11
11
|
* @param refetchMode - Defines how re-fetches are handled.
|
|
12
12
|
* Defaults to `'reset'`, erases all data and puts the query back into `pending` state.
|
|
13
13
|
* Set to `'append'` to append new data to the existing data.
|
|
14
|
-
* Set to `'replace'` to write
|
|
14
|
+
* Set to `'replace'` to write all data to the cache once the stream ends.
|
|
15
|
+
* @param maxChunks - The maximum number of chunks to keep in the cache.
|
|
16
|
+
* Defaults to `undefined`, meaning all chunks will be kept.
|
|
17
|
+
* If `undefined` or `0`, the number of chunks is unlimited.
|
|
18
|
+
* If the number of chunks exceeds this number, the oldest chunk will be removed.
|
|
15
19
|
*/
|
|
16
|
-
declare function streamedQuery<TQueryFnData = unknown, TQueryKey extends QueryKey = QueryKey>({ queryFn, refetchMode, }: {
|
|
20
|
+
declare function streamedQuery<TQueryFnData = unknown, TQueryKey extends QueryKey = QueryKey>({ queryFn, refetchMode, maxChunks, }: {
|
|
17
21
|
queryFn: (context: QueryFunctionContext<TQueryKey>) => AsyncIterable<TQueryFnData> | Promise<AsyncIterable<TQueryFnData>>;
|
|
18
22
|
refetchMode?: 'append' | 'reset' | 'replace';
|
|
23
|
+
maxChunks?: number;
|
|
19
24
|
}): QueryFunction<Array<TQueryFnData>, TQueryKey>;
|
|
20
25
|
|
|
21
26
|
export { streamedQuery };
|
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import "./chunk-PXG64RU4.js";
|
|
2
2
|
|
|
3
3
|
// src/streamedQuery.ts
|
|
4
|
+
import { addToEnd } from "./utils.js";
|
|
4
5
|
function streamedQuery({
|
|
5
6
|
queryFn,
|
|
6
|
-
refetchMode = "reset"
|
|
7
|
+
refetchMode = "reset",
|
|
8
|
+
maxChunks
|
|
7
9
|
}) {
|
|
8
10
|
return async (context) => {
|
|
9
11
|
const query = context.client.getQueryCache().find({ queryKey: context.queryKey, exact: true });
|
|
@@ -16,7 +18,7 @@ function streamedQuery({
|
|
|
16
18
|
fetchStatus: "fetching"
|
|
17
19
|
});
|
|
18
20
|
}
|
|
19
|
-
|
|
21
|
+
let result = [];
|
|
20
22
|
const stream = await queryFn(context);
|
|
21
23
|
for await (const chunk of stream) {
|
|
22
24
|
if (context.signal.aborted) {
|
|
@@ -26,11 +28,11 @@ function streamedQuery({
|
|
|
26
28
|
context.client.setQueryData(
|
|
27
29
|
context.queryKey,
|
|
28
30
|
(prev = []) => {
|
|
29
|
-
return prev
|
|
31
|
+
return addToEnd(prev, chunk, maxChunks);
|
|
30
32
|
}
|
|
31
33
|
);
|
|
32
34
|
}
|
|
33
|
-
result
|
|
35
|
+
result = addToEnd(result, chunk, maxChunks);
|
|
34
36
|
}
|
|
35
37
|
if (isRefetch && refetchMode === "replace" && !context.signal.aborted) {
|
|
36
38
|
context.client.setQueryData(context.queryKey, result);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/streamedQuery.ts"],"sourcesContent":["import type { QueryFunction, QueryFunctionContext, QueryKey } from './types'\n\n/**\n * This is a helper function to create a query function that streams data from an AsyncIterable.\n * Data will be an Array of all the chunks received.\n * The query will be in a 'pending' state until the first chunk of data is received, but will go to 'success' after that.\n * The query will stay in fetchStatus 'fetching' until the stream ends.\n * @param queryFn - The function that returns an AsyncIterable to stream data from.\n * @param refetchMode - Defines how re-fetches are handled.\n * Defaults to `'reset'`, erases all data and puts the query back into `pending` state.\n * Set to `'append'` to append new data to the existing data.\n * Set to `'replace'` to write
|
|
1
|
+
{"version":3,"sources":["../../src/streamedQuery.ts"],"sourcesContent":["import { addToEnd } from './utils'\nimport type { QueryFunction, QueryFunctionContext, QueryKey } from './types'\n\n/**\n * This is a helper function to create a query function that streams data from an AsyncIterable.\n * Data will be an Array of all the chunks received.\n * The query will be in a 'pending' state until the first chunk of data is received, but will go to 'success' after that.\n * The query will stay in fetchStatus 'fetching' until the stream ends.\n * @param queryFn - The function that returns an AsyncIterable to stream data from.\n * @param refetchMode - Defines how re-fetches are handled.\n * Defaults to `'reset'`, erases all data and puts the query back into `pending` state.\n * Set to `'append'` to append new data to the existing data.\n * Set to `'replace'` to write all data to the cache once the stream ends.\n * @param maxChunks - The maximum number of chunks to keep in the cache.\n * Defaults to `undefined`, meaning all chunks will be kept.\n * If `undefined` or `0`, the number of chunks is unlimited.\n * If the number of chunks exceeds this number, the oldest chunk will be removed.\n */\nexport function streamedQuery<\n TQueryFnData = unknown,\n TQueryKey extends QueryKey = QueryKey,\n>({\n queryFn,\n refetchMode = 'reset',\n maxChunks,\n}: {\n queryFn: (\n context: QueryFunctionContext<TQueryKey>,\n ) => AsyncIterable<TQueryFnData> | Promise<AsyncIterable<TQueryFnData>>\n refetchMode?: 'append' | 'reset' | 'replace'\n maxChunks?: number\n}): QueryFunction<Array<TQueryFnData>, TQueryKey> {\n return async (context) => {\n const query = context.client\n .getQueryCache()\n .find({ queryKey: context.queryKey, exact: true })\n const isRefetch = !!query && query.state.data !== undefined\n\n if (isRefetch && refetchMode === 'reset') {\n query.setState({\n status: 'pending',\n data: undefined,\n error: null,\n fetchStatus: 'fetching',\n })\n }\n\n let result: Array<TQueryFnData> = []\n const stream = await queryFn(context)\n\n for await (const chunk of stream) {\n if (context.signal.aborted) {\n break\n }\n\n // don't append to the cache directly when replace-refetching\n if (!isRefetch || refetchMode !== 'replace') {\n context.client.setQueryData<Array<TQueryFnData>>(\n context.queryKey,\n (prev = []) => {\n return addToEnd(prev, chunk, maxChunks)\n },\n )\n }\n result = addToEnd(result, chunk, maxChunks)\n }\n\n // finalize result: replace-refetching needs to write to the cache\n if (isRefetch && refetchMode === 'replace' && !context.signal.aborted) {\n context.client.setQueryData<Array<TQueryFnData>>(context.queryKey, result)\n }\n\n return context.client.getQueryData(context.queryKey)!\n }\n}\n"],"mappings":";;;AAAA,SAAS,gBAAgB;AAkBlB,SAAS,cAGd;AAAA,EACA;AAAA,EACA,cAAc;AAAA,EACd;AACF,GAMkD;AAChD,SAAO,OAAO,YAAY;AACxB,UAAM,QAAQ,QAAQ,OACnB,cAAc,EACd,KAAK,EAAE,UAAU,QAAQ,UAAU,OAAO,KAAK,CAAC;AACnD,UAAM,YAAY,CAAC,CAAC,SAAS,MAAM,MAAM,SAAS;AAElD,QAAI,aAAa,gBAAgB,SAAS;AACxC,YAAM,SAAS;AAAA,QACb,QAAQ;AAAA,QACR,MAAM;AAAA,QACN,OAAO;AAAA,QACP,aAAa;AAAA,MACf,CAAC;AAAA,IACH;AAEA,QAAI,SAA8B,CAAC;AACnC,UAAM,SAAS,MAAM,QAAQ,OAAO;AAEpC,qBAAiB,SAAS,QAAQ;AAChC,UAAI,QAAQ,OAAO,SAAS;AAC1B;AAAA,MACF;AAGA,UAAI,CAAC,aAAa,gBAAgB,WAAW;AAC3C,gBAAQ,OAAO;AAAA,UACb,QAAQ;AAAA,UACR,CAAC,OAAO,CAAC,MAAM;AACb,mBAAO,SAAS,MAAM,OAAO,SAAS;AAAA,UACxC;AAAA,QACF;AAAA,MACF;AACA,eAAS,SAAS,QAAQ,OAAO,SAAS;AAAA,IAC5C;AAGA,QAAI,aAAa,gBAAgB,aAAa,CAAC,QAAQ,OAAO,SAAS;AACrE,cAAQ,OAAO,aAAkC,QAAQ,UAAU,MAAM;AAAA,IAC3E;AAEA,WAAO,QAAQ,OAAO,aAAa,QAAQ,QAAQ;AAAA,EACrD;AACF;","names":[]}
|
|
@@ -23,9 +23,11 @@ __export(streamedQuery_exports, {
|
|
|
23
23
|
streamedQuery: () => streamedQuery
|
|
24
24
|
});
|
|
25
25
|
module.exports = __toCommonJS(streamedQuery_exports);
|
|
26
|
+
var import_utils = require("./utils.cjs");
|
|
26
27
|
function streamedQuery({
|
|
27
28
|
queryFn,
|
|
28
|
-
refetchMode = "reset"
|
|
29
|
+
refetchMode = "reset",
|
|
30
|
+
maxChunks
|
|
29
31
|
}) {
|
|
30
32
|
return async (context) => {
|
|
31
33
|
const query = context.client.getQueryCache().find({ queryKey: context.queryKey, exact: true });
|
|
@@ -38,7 +40,7 @@ function streamedQuery({
|
|
|
38
40
|
fetchStatus: "fetching"
|
|
39
41
|
});
|
|
40
42
|
}
|
|
41
|
-
|
|
43
|
+
let result = [];
|
|
42
44
|
const stream = await queryFn(context);
|
|
43
45
|
for await (const chunk of stream) {
|
|
44
46
|
if (context.signal.aborted) {
|
|
@@ -48,11 +50,11 @@ function streamedQuery({
|
|
|
48
50
|
context.client.setQueryData(
|
|
49
51
|
context.queryKey,
|
|
50
52
|
(prev = []) => {
|
|
51
|
-
return
|
|
53
|
+
return (0, import_utils.addToEnd)(prev, chunk, maxChunks);
|
|
52
54
|
}
|
|
53
55
|
);
|
|
54
56
|
}
|
|
55
|
-
result.
|
|
57
|
+
result = (0, import_utils.addToEnd)(result, chunk, maxChunks);
|
|
56
58
|
}
|
|
57
59
|
if (isRefetch && refetchMode === "replace" && !context.signal.aborted) {
|
|
58
60
|
context.client.setQueryData(context.queryKey, result);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/streamedQuery.ts"],"sourcesContent":["import type { QueryFunction, QueryFunctionContext, QueryKey } from './types'\n\n/**\n * This is a helper function to create a query function that streams data from an AsyncIterable.\n * Data will be an Array of all the chunks received.\n * The query will be in a 'pending' state until the first chunk of data is received, but will go to 'success' after that.\n * The query will stay in fetchStatus 'fetching' until the stream ends.\n * @param queryFn - The function that returns an AsyncIterable to stream data from.\n * @param refetchMode - Defines how re-fetches are handled.\n * Defaults to `'reset'`, erases all data and puts the query back into `pending` state.\n * Set to `'append'` to append new data to the existing data.\n * Set to `'replace'` to write
|
|
1
|
+
{"version":3,"sources":["../../src/streamedQuery.ts"],"sourcesContent":["import { addToEnd } from './utils'\nimport type { QueryFunction, QueryFunctionContext, QueryKey } from './types'\n\n/**\n * This is a helper function to create a query function that streams data from an AsyncIterable.\n * Data will be an Array of all the chunks received.\n * The query will be in a 'pending' state until the first chunk of data is received, but will go to 'success' after that.\n * The query will stay in fetchStatus 'fetching' until the stream ends.\n * @param queryFn - The function that returns an AsyncIterable to stream data from.\n * @param refetchMode - Defines how re-fetches are handled.\n * Defaults to `'reset'`, erases all data and puts the query back into `pending` state.\n * Set to `'append'` to append new data to the existing data.\n * Set to `'replace'` to write all data to the cache once the stream ends.\n * @param maxChunks - The maximum number of chunks to keep in the cache.\n * Defaults to `undefined`, meaning all chunks will be kept.\n * If `undefined` or `0`, the number of chunks is unlimited.\n * If the number of chunks exceeds this number, the oldest chunk will be removed.\n */\nexport function streamedQuery<\n TQueryFnData = unknown,\n TQueryKey extends QueryKey = QueryKey,\n>({\n queryFn,\n refetchMode = 'reset',\n maxChunks,\n}: {\n queryFn: (\n context: QueryFunctionContext<TQueryKey>,\n ) => AsyncIterable<TQueryFnData> | Promise<AsyncIterable<TQueryFnData>>\n refetchMode?: 'append' | 'reset' | 'replace'\n maxChunks?: number\n}): QueryFunction<Array<TQueryFnData>, TQueryKey> {\n return async (context) => {\n const query = context.client\n .getQueryCache()\n .find({ queryKey: context.queryKey, exact: true })\n const isRefetch = !!query && query.state.data !== undefined\n\n if (isRefetch && refetchMode === 'reset') {\n query.setState({\n status: 'pending',\n data: undefined,\n error: null,\n fetchStatus: 'fetching',\n })\n }\n\n let result: Array<TQueryFnData> = []\n const stream = await queryFn(context)\n\n for await (const chunk of stream) {\n if (context.signal.aborted) {\n break\n }\n\n // don't append to the cache directly when replace-refetching\n if (!isRefetch || refetchMode !== 'replace') {\n context.client.setQueryData<Array<TQueryFnData>>(\n context.queryKey,\n (prev = []) => {\n return addToEnd(prev, chunk, maxChunks)\n },\n )\n }\n result = addToEnd(result, chunk, maxChunks)\n }\n\n // finalize result: replace-refetching needs to write to the cache\n if (isRefetch && refetchMode === 'replace' && !context.signal.aborted) {\n context.client.setQueryData<Array<TQueryFnData>>(context.queryKey, result)\n }\n\n return context.client.getQueryData(context.queryKey)!\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,mBAAyB;AAkBlB,SAAS,cAGd;AAAA,EACA;AAAA,EACA,cAAc;AAAA,EACd;AACF,GAMkD;AAChD,SAAO,OAAO,YAAY;AACxB,UAAM,QAAQ,QAAQ,OACnB,cAAc,EACd,KAAK,EAAE,UAAU,QAAQ,UAAU,OAAO,KAAK,CAAC;AACnD,UAAM,YAAY,CAAC,CAAC,SAAS,MAAM,MAAM,SAAS;AAElD,QAAI,aAAa,gBAAgB,SAAS;AACxC,YAAM,SAAS;AAAA,QACb,QAAQ;AAAA,QACR,MAAM;AAAA,QACN,OAAO;AAAA,QACP,aAAa;AAAA,MACf,CAAC;AAAA,IACH;AAEA,QAAI,SAA8B,CAAC;AACnC,UAAM,SAAS,MAAM,QAAQ,OAAO;AAEpC,qBAAiB,SAAS,QAAQ;AAChC,UAAI,QAAQ,OAAO,SAAS;AAC1B;AAAA,MACF;AAGA,UAAI,CAAC,aAAa,gBAAgB,WAAW;AAC3C,gBAAQ,OAAO;AAAA,UACb,QAAQ;AAAA,UACR,CAAC,OAAO,CAAC,MAAM;AACb,uBAAO,uBAAS,MAAM,OAAO,SAAS;AAAA,UACxC;AAAA,QACF;AAAA,MACF;AACA,mBAAS,uBAAS,QAAQ,OAAO,SAAS;AAAA,IAC5C;AAGA,QAAI,aAAa,gBAAgB,aAAa,CAAC,QAAQ,OAAO,SAAS;AACrE,cAAQ,OAAO,aAAkC,QAAQ,UAAU,MAAM;AAAA,IAC3E;AAEA,WAAO,QAAQ,OAAO,aAAa,QAAQ,QAAQ;AAAA,EACrD;AACF;","names":[]}
|
|
@@ -11,11 +11,16 @@ import './subscribable.cjs';
|
|
|
11
11
|
* @param refetchMode - Defines how re-fetches are handled.
|
|
12
12
|
* Defaults to `'reset'`, erases all data and puts the query back into `pending` state.
|
|
13
13
|
* Set to `'append'` to append new data to the existing data.
|
|
14
|
-
* Set to `'replace'` to write
|
|
14
|
+
* Set to `'replace'` to write all data to the cache once the stream ends.
|
|
15
|
+
* @param maxChunks - The maximum number of chunks to keep in the cache.
|
|
16
|
+
* Defaults to `undefined`, meaning all chunks will be kept.
|
|
17
|
+
* If `undefined` or `0`, the number of chunks is unlimited.
|
|
18
|
+
* If the number of chunks exceeds this number, the oldest chunk will be removed.
|
|
15
19
|
*/
|
|
16
|
-
declare function streamedQuery<TQueryFnData = unknown, TQueryKey extends QueryKey = QueryKey>({ queryFn, refetchMode, }: {
|
|
20
|
+
declare function streamedQuery<TQueryFnData = unknown, TQueryKey extends QueryKey = QueryKey>({ queryFn, refetchMode, maxChunks, }: {
|
|
17
21
|
queryFn: (context: QueryFunctionContext<TQueryKey>) => AsyncIterable<TQueryFnData> | Promise<AsyncIterable<TQueryFnData>>;
|
|
18
22
|
refetchMode?: 'append' | 'reset' | 'replace';
|
|
23
|
+
maxChunks?: number;
|
|
19
24
|
}): QueryFunction<Array<TQueryFnData>, TQueryKey>;
|
|
20
25
|
|
|
21
26
|
export { streamedQuery };
|
|
@@ -11,11 +11,16 @@ import './subscribable.js';
|
|
|
11
11
|
* @param refetchMode - Defines how re-fetches are handled.
|
|
12
12
|
* Defaults to `'reset'`, erases all data and puts the query back into `pending` state.
|
|
13
13
|
* Set to `'append'` to append new data to the existing data.
|
|
14
|
-
* Set to `'replace'` to write
|
|
14
|
+
* Set to `'replace'` to write all data to the cache once the stream ends.
|
|
15
|
+
* @param maxChunks - The maximum number of chunks to keep in the cache.
|
|
16
|
+
* Defaults to `undefined`, meaning all chunks will be kept.
|
|
17
|
+
* If `undefined` or `0`, the number of chunks is unlimited.
|
|
18
|
+
* If the number of chunks exceeds this number, the oldest chunk will be removed.
|
|
15
19
|
*/
|
|
16
|
-
declare function streamedQuery<TQueryFnData = unknown, TQueryKey extends QueryKey = QueryKey>({ queryFn, refetchMode, }: {
|
|
20
|
+
declare function streamedQuery<TQueryFnData = unknown, TQueryKey extends QueryKey = QueryKey>({ queryFn, refetchMode, maxChunks, }: {
|
|
17
21
|
queryFn: (context: QueryFunctionContext<TQueryKey>) => AsyncIterable<TQueryFnData> | Promise<AsyncIterable<TQueryFnData>>;
|
|
18
22
|
refetchMode?: 'append' | 'reset' | 'replace';
|
|
23
|
+
maxChunks?: number;
|
|
19
24
|
}): QueryFunction<Array<TQueryFnData>, TQueryKey>;
|
|
20
25
|
|
|
21
26
|
export { streamedQuery };
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
// src/streamedQuery.ts
|
|
2
|
+
import { addToEnd } from "./utils.js";
|
|
2
3
|
function streamedQuery({
|
|
3
4
|
queryFn,
|
|
4
|
-
refetchMode = "reset"
|
|
5
|
+
refetchMode = "reset",
|
|
6
|
+
maxChunks
|
|
5
7
|
}) {
|
|
6
8
|
return async (context) => {
|
|
7
9
|
const query = context.client.getQueryCache().find({ queryKey: context.queryKey, exact: true });
|
|
@@ -14,7 +16,7 @@ function streamedQuery({
|
|
|
14
16
|
fetchStatus: "fetching"
|
|
15
17
|
});
|
|
16
18
|
}
|
|
17
|
-
|
|
19
|
+
let result = [];
|
|
18
20
|
const stream = await queryFn(context);
|
|
19
21
|
for await (const chunk of stream) {
|
|
20
22
|
if (context.signal.aborted) {
|
|
@@ -24,11 +26,11 @@ function streamedQuery({
|
|
|
24
26
|
context.client.setQueryData(
|
|
25
27
|
context.queryKey,
|
|
26
28
|
(prev = []) => {
|
|
27
|
-
return prev
|
|
29
|
+
return addToEnd(prev, chunk, maxChunks);
|
|
28
30
|
}
|
|
29
31
|
);
|
|
30
32
|
}
|
|
31
|
-
result
|
|
33
|
+
result = addToEnd(result, chunk, maxChunks);
|
|
32
34
|
}
|
|
33
35
|
if (isRefetch && refetchMode === "replace" && !context.signal.aborted) {
|
|
34
36
|
context.client.setQueryData(context.queryKey, result);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/streamedQuery.ts"],"sourcesContent":["import type { QueryFunction, QueryFunctionContext, QueryKey } from './types'\n\n/**\n * This is a helper function to create a query function that streams data from an AsyncIterable.\n * Data will be an Array of all the chunks received.\n * The query will be in a 'pending' state until the first chunk of data is received, but will go to 'success' after that.\n * The query will stay in fetchStatus 'fetching' until the stream ends.\n * @param queryFn - The function that returns an AsyncIterable to stream data from.\n * @param refetchMode - Defines how re-fetches are handled.\n * Defaults to `'reset'`, erases all data and puts the query back into `pending` state.\n * Set to `'append'` to append new data to the existing data.\n * Set to `'replace'` to write
|
|
1
|
+
{"version":3,"sources":["../../src/streamedQuery.ts"],"sourcesContent":["import { addToEnd } from './utils'\nimport type { QueryFunction, QueryFunctionContext, QueryKey } from './types'\n\n/**\n * This is a helper function to create a query function that streams data from an AsyncIterable.\n * Data will be an Array of all the chunks received.\n * The query will be in a 'pending' state until the first chunk of data is received, but will go to 'success' after that.\n * The query will stay in fetchStatus 'fetching' until the stream ends.\n * @param queryFn - The function that returns an AsyncIterable to stream data from.\n * @param refetchMode - Defines how re-fetches are handled.\n * Defaults to `'reset'`, erases all data and puts the query back into `pending` state.\n * Set to `'append'` to append new data to the existing data.\n * Set to `'replace'` to write all data to the cache once the stream ends.\n * @param maxChunks - The maximum number of chunks to keep in the cache.\n * Defaults to `undefined`, meaning all chunks will be kept.\n * If `undefined` or `0`, the number of chunks is unlimited.\n * If the number of chunks exceeds this number, the oldest chunk will be removed.\n */\nexport function streamedQuery<\n TQueryFnData = unknown,\n TQueryKey extends QueryKey = QueryKey,\n>({\n queryFn,\n refetchMode = 'reset',\n maxChunks,\n}: {\n queryFn: (\n context: QueryFunctionContext<TQueryKey>,\n ) => AsyncIterable<TQueryFnData> | Promise<AsyncIterable<TQueryFnData>>\n refetchMode?: 'append' | 'reset' | 'replace'\n maxChunks?: number\n}): QueryFunction<Array<TQueryFnData>, TQueryKey> {\n return async (context) => {\n const query = context.client\n .getQueryCache()\n .find({ queryKey: context.queryKey, exact: true })\n const isRefetch = !!query && query.state.data !== undefined\n\n if (isRefetch && refetchMode === 'reset') {\n query.setState({\n status: 'pending',\n data: undefined,\n error: null,\n fetchStatus: 'fetching',\n })\n }\n\n let result: Array<TQueryFnData> = []\n const stream = await queryFn(context)\n\n for await (const chunk of stream) {\n if (context.signal.aborted) {\n break\n }\n\n // don't append to the cache directly when replace-refetching\n if (!isRefetch || refetchMode !== 'replace') {\n context.client.setQueryData<Array<TQueryFnData>>(\n context.queryKey,\n (prev = []) => {\n return addToEnd(prev, chunk, maxChunks)\n },\n )\n }\n result = addToEnd(result, chunk, maxChunks)\n }\n\n // finalize result: replace-refetching needs to write to the cache\n if (isRefetch && refetchMode === 'replace' && !context.signal.aborted) {\n context.client.setQueryData<Array<TQueryFnData>>(context.queryKey, result)\n }\n\n return context.client.getQueryData(context.queryKey)!\n }\n}\n"],"mappings":";AAAA,SAAS,gBAAgB;AAkBlB,SAAS,cAGd;AAAA,EACA;AAAA,EACA,cAAc;AAAA,EACd;AACF,GAMkD;AAChD,SAAO,OAAO,YAAY;AACxB,UAAM,QAAQ,QAAQ,OACnB,cAAc,EACd,KAAK,EAAE,UAAU,QAAQ,UAAU,OAAO,KAAK,CAAC;AACnD,UAAM,YAAY,CAAC,CAAC,SAAS,MAAM,MAAM,SAAS;AAElD,QAAI,aAAa,gBAAgB,SAAS;AACxC,YAAM,SAAS;AAAA,QACb,QAAQ;AAAA,QACR,MAAM;AAAA,QACN,OAAO;AAAA,QACP,aAAa;AAAA,MACf,CAAC;AAAA,IACH;AAEA,QAAI,SAA8B,CAAC;AACnC,UAAM,SAAS,MAAM,QAAQ,OAAO;AAEpC,qBAAiB,SAAS,QAAQ;AAChC,UAAI,QAAQ,OAAO,SAAS;AAC1B;AAAA,MACF;AAGA,UAAI,CAAC,aAAa,gBAAgB,WAAW;AAC3C,gBAAQ,OAAO;AAAA,UACb,QAAQ;AAAA,UACR,CAAC,OAAO,CAAC,MAAM;AACb,mBAAO,SAAS,MAAM,OAAO,SAAS;AAAA,UACxC;AAAA,QACF;AAAA,MACF;AACA,eAAS,SAAS,QAAQ,OAAO,SAAS;AAAA,IAC5C;AAGA,QAAI,aAAa,gBAAgB,aAAa,CAAC,QAAQ,OAAO,SAAS;AACrE,cAAQ,OAAO,aAAkC,QAAQ,UAAU,MAAM;AAAA,IAC3E;AAEA,WAAO,QAAQ,OAAO,aAAa,QAAQ,QAAQ;AAAA,EACrD;AACF;","names":[]}
|
package/package.json
CHANGED
package/src/streamedQuery.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { addToEnd } from './utils'
|
|
1
2
|
import type { QueryFunction, QueryFunctionContext, QueryKey } from './types'
|
|
2
3
|
|
|
3
4
|
/**
|
|
@@ -9,7 +10,11 @@ import type { QueryFunction, QueryFunctionContext, QueryKey } from './types'
|
|
|
9
10
|
* @param refetchMode - Defines how re-fetches are handled.
|
|
10
11
|
* Defaults to `'reset'`, erases all data and puts the query back into `pending` state.
|
|
11
12
|
* Set to `'append'` to append new data to the existing data.
|
|
12
|
-
* Set to `'replace'` to write
|
|
13
|
+
* Set to `'replace'` to write all data to the cache once the stream ends.
|
|
14
|
+
* @param maxChunks - The maximum number of chunks to keep in the cache.
|
|
15
|
+
* Defaults to `undefined`, meaning all chunks will be kept.
|
|
16
|
+
* If `undefined` or `0`, the number of chunks is unlimited.
|
|
17
|
+
* If the number of chunks exceeds this number, the oldest chunk will be removed.
|
|
13
18
|
*/
|
|
14
19
|
export function streamedQuery<
|
|
15
20
|
TQueryFnData = unknown,
|
|
@@ -17,11 +22,13 @@ export function streamedQuery<
|
|
|
17
22
|
>({
|
|
18
23
|
queryFn,
|
|
19
24
|
refetchMode = 'reset',
|
|
25
|
+
maxChunks,
|
|
20
26
|
}: {
|
|
21
27
|
queryFn: (
|
|
22
28
|
context: QueryFunctionContext<TQueryKey>,
|
|
23
29
|
) => AsyncIterable<TQueryFnData> | Promise<AsyncIterable<TQueryFnData>>
|
|
24
30
|
refetchMode?: 'append' | 'reset' | 'replace'
|
|
31
|
+
maxChunks?: number
|
|
25
32
|
}): QueryFunction<Array<TQueryFnData>, TQueryKey> {
|
|
26
33
|
return async (context) => {
|
|
27
34
|
const query = context.client
|
|
@@ -38,7 +45,7 @@ export function streamedQuery<
|
|
|
38
45
|
})
|
|
39
46
|
}
|
|
40
47
|
|
|
41
|
-
|
|
48
|
+
let result: Array<TQueryFnData> = []
|
|
42
49
|
const stream = await queryFn(context)
|
|
43
50
|
|
|
44
51
|
for await (const chunk of stream) {
|
|
@@ -51,11 +58,11 @@ export function streamedQuery<
|
|
|
51
58
|
context.client.setQueryData<Array<TQueryFnData>>(
|
|
52
59
|
context.queryKey,
|
|
53
60
|
(prev = []) => {
|
|
54
|
-
return prev
|
|
61
|
+
return addToEnd(prev, chunk, maxChunks)
|
|
55
62
|
},
|
|
56
63
|
)
|
|
57
64
|
}
|
|
58
|
-
result
|
|
65
|
+
result = addToEnd(result, chunk, maxChunks)
|
|
59
66
|
}
|
|
60
67
|
|
|
61
68
|
// finalize result: replace-refetching needs to write to the cache
|