@ahoo-wang/wow-react 10.0.0-alpha.3 → 10.0.0-alpha.5
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/README.md +75 -71
- package/README.zh-CN.md +75 -73
- package/dist/hooks/useCountQuery.d.ts +17 -14
- package/dist/hooks/useFetcherCountQuery.d.ts +16 -15
- package/dist/hooks/useFetcherListQuery.d.ts +16 -14
- package/dist/hooks/useFetcherListStreamQuery.d.ts +19 -16
- package/dist/hooks/useFetcherPagedQuery.d.ts +15 -13
- package/dist/hooks/useFetcherSingleQuery.d.ts +16 -14
- package/dist/hooks/useListQuery.d.ts +16 -13
- package/dist/hooks/useListStreamQuery.d.ts +21 -17
- package/dist/hooks/usePagedQuery.d.ts +15 -12
- package/dist/hooks/useSingleQuery.d.ts +16 -13
- package/dist/index.es.js +38 -42
- package/dist/index.es.js.map +1 -1
- package/dist/internal/endpoint.d.ts +11 -14
- package/dist/internal/useEndpointRunner.d.ts +2 -2
- package/dist/internal/useListStream.d.ts +3 -3
- package/dist/internal/useQueryRunner.d.ts +1 -1
- package/dist/types.d.ts +22 -17
- package/package.json +9 -7
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import { FilterSingleQuery } from '@ahoo-wang/wow-client';
|
|
2
|
-
import { SingleQuery, SingleQueryRequest } from '@ahoo-wang/wow-client/legacy';
|
|
1
|
+
import { FilterSingleQuery } from '@ahoo-wang/wow-client/dsl';
|
|
3
2
|
import { QueryHookOptions, QueryHookReturn } from '../types.js';
|
|
4
3
|
/**
|
|
5
4
|
* Options of {@link useSingleQuery}: a single query and an `execute` that
|
|
@@ -10,7 +9,7 @@ import { QueryHookOptions, QueryHookReturn } from '../types.js';
|
|
|
10
9
|
* @template E - The error type, `Error` by default
|
|
11
10
|
* @template Q - The query type: `FilterSingleQuery` by default
|
|
12
11
|
*/
|
|
13
|
-
export interface UseSingleQueryOptions<R, FIELDS extends string = string, E = Error, Q extends
|
|
12
|
+
export interface UseSingleQueryOptions<R, FIELDS extends string = string, E = Error, Q extends FilterSingleQuery<FIELDS> = FilterSingleQuery<FIELDS>> extends QueryHookOptions<Q, R, E> {
|
|
14
13
|
}
|
|
15
14
|
/**
|
|
16
15
|
* What {@link useSingleQuery} returns: the item as `result`.
|
|
@@ -20,14 +19,14 @@ export interface UseSingleQueryOptions<R, FIELDS extends string = string, E = Er
|
|
|
20
19
|
* @template E - The error type, `Error` by default
|
|
21
20
|
* @template Q - The query type: `FilterSingleQuery` by default
|
|
22
21
|
*/
|
|
23
|
-
export interface UseSingleQueryReturn<R, FIELDS extends string = string, E = Error, Q extends
|
|
22
|
+
export interface UseSingleQueryReturn<R, FIELDS extends string = string, E = Error, Q extends FilterSingleQuery<FIELDS> = FilterSingleQuery<FIELDS>> extends QueryHookReturn<Q, R, E> {
|
|
24
23
|
}
|
|
25
24
|
/**
|
|
26
25
|
* Runs a single query through your own `execute` function and keeps the
|
|
27
26
|
* result as state: typically a query client's `single` or `singleState`.
|
|
28
27
|
*
|
|
29
|
-
* `execute` receives the query
|
|
30
|
-
* `
|
|
28
|
+
* `execute` receives the query and the request options of the run, its
|
|
29
|
+
* `signal` included; hand them on so that a newer query, `abort()`
|
|
31
30
|
* or an unmount cancels the request. The query runs on mount and whenever
|
|
32
31
|
* `query` or `setQuery()` changes it; set `autoExecute: false` to run it only
|
|
33
32
|
* through `execute()`.
|
|
@@ -44,21 +43,25 @@ export interface UseSingleQueryReturn<R, FIELDS extends string = string, E = Err
|
|
|
44
43
|
*
|
|
45
44
|
* @example
|
|
46
45
|
* ```tsx
|
|
47
|
-
* import {
|
|
46
|
+
* import type { SnapshotQueryClient, SnapshotRecord } from '@ahoo-wang/wow-client';
|
|
47
|
+
* import { filter, singleQuery } from '@ahoo-wang/wow-client/dsl';
|
|
48
48
|
* import { useSingleQuery } from '@ahoo-wang/wow-react';
|
|
49
49
|
*
|
|
50
50
|
* function OrderStatus({ client, id }: { client: SnapshotQueryClient<OrderState, OrderFields>; id: string }) {
|
|
51
|
-
* const { result, loading, error } = useSingleQuery<OrderState
|
|
51
|
+
* const { result, loading, error } = useSingleQuery<SnapshotRecord<OrderState>, OrderFields>({
|
|
52
52
|
* query: singleQuery({ filter: filter.id(id) }),
|
|
53
|
-
* execute: (query,
|
|
54
|
-
* client.
|
|
53
|
+
* execute: (query, options) =>
|
|
54
|
+
* client.single(query, options),
|
|
55
55
|
* });
|
|
56
56
|
* if (error) return <p role="alert">{error.message}</p>;
|
|
57
57
|
* if (loading || !result) return <p>Loading…</p>;
|
|
58
|
-
* return <p>{result.status}</p>;
|
|
58
|
+
* return <p>{result.state.status}</p>;
|
|
59
59
|
* }
|
|
60
60
|
* ```
|
|
61
61
|
*/
|
|
62
62
|
export declare function useSingleQuery<R, FIELDS extends string = string, E = Error>(options: UseSingleQueryOptions<R, FIELDS, E, FilterSingleQuery<FIELDS>>): UseSingleQueryReturn<R, FIELDS, E, FilterSingleQuery<FIELDS>>;
|
|
63
|
-
|
|
64
|
-
|
|
63
|
+
/**
|
|
64
|
+
* {@link useSingleQuery} typed by a query `Q` of your own, a subtype of
|
|
65
|
+
* `FilterSingleQuery`, such as one whose literal projection narrows the result.
|
|
66
|
+
*/
|
|
67
|
+
export declare function useSingleQuery<R, FIELDS extends string = string, E = Error, Q extends FilterSingleQuery<FIELDS> = FilterSingleQuery<FIELDS>>(options: UseSingleQueryOptions<R, FIELDS, E, Q>): UseSingleQueryReturn<R, FIELDS, E, Q>;
|
package/dist/index.es.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { dequal as e } from "dequal";
|
|
2
2
|
import { useCallback as t, useEffect as n, useLayoutEffect as r, useRef as i, useState as a } from "react";
|
|
3
3
|
import { c as o } from "react/compiler-runtime";
|
|
4
|
-
import { DEFAULT_FETCHER_NAME as s,
|
|
5
|
-
import {
|
|
4
|
+
import { DEFAULT_FETCHER_NAME as s, getFetcher as c } from "@ahoo-wang/fetcher";
|
|
5
|
+
import { QueryModelClient as l } from "@ahoo-wang/wow-client";
|
|
6
6
|
//#endregion
|
|
7
7
|
//#region src/internal/queryTransitions.ts
|
|
8
8
|
function initialQueryState(e) {
|
|
@@ -74,9 +74,12 @@ function useQueryRunner(e, o, s = !0) {
|
|
|
74
74
|
id: n,
|
|
75
75
|
controller: t
|
|
76
76
|
}, d((e) => queryTransition(e, { type: "start" }));
|
|
77
|
-
let { execute: r,
|
|
77
|
+
let { execute: r, requestOptions: i } = f.current;
|
|
78
78
|
try {
|
|
79
|
-
let a = await r(e,
|
|
79
|
+
let a = await r(e, {
|
|
80
|
+
...i,
|
|
81
|
+
signal: t.signal
|
|
82
|
+
});
|
|
80
83
|
t.signal.aborted ? g(n, { type: "abort" }) : g(n, {
|
|
81
84
|
type: "succeed",
|
|
82
85
|
result: s ? a : void 0
|
|
@@ -128,24 +131,17 @@ function useCountQuery(e) {
|
|
|
128
131
|
}
|
|
129
132
|
//#endregion
|
|
130
133
|
//#region src/internal/endpoint.ts
|
|
131
|
-
function
|
|
132
|
-
return (
|
|
133
|
-
body: n,
|
|
134
|
-
abortController: i
|
|
135
|
-
}, {
|
|
136
|
-
attributes: r,
|
|
137
|
-
resultExtractor: c
|
|
138
|
-
});
|
|
134
|
+
function clientOf({ url: e, fetcher: t }) {
|
|
135
|
+
return new l(e, { fetcher: c(t) });
|
|
139
136
|
}
|
|
140
|
-
function
|
|
141
|
-
return (n, r
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
});
|
|
137
|
+
function postQuery(e, t) {
|
|
138
|
+
return (n, r) => {
|
|
139
|
+
let i = clientOf(e);
|
|
140
|
+
return i[t].call(i, n, r);
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
function postQueryStream(e) {
|
|
144
|
+
return (t, n) => clientOf(e).listStream(t, n);
|
|
149
145
|
}
|
|
150
146
|
function endpointIdentity({ url: e, fetcher: t }) {
|
|
151
147
|
let n;
|
|
@@ -153,34 +149,34 @@ function endpointIdentity({ url: e, fetcher: t }) {
|
|
|
153
149
|
}
|
|
154
150
|
//#endregion
|
|
155
151
|
//#region src/internal/useEndpointRunner.ts
|
|
156
|
-
function useEndpointRunner(e) {
|
|
157
|
-
let
|
|
158
|
-
|
|
159
|
-
let a;
|
|
160
|
-
t[4] !== n || t[5] !== i ? (a = postQuery({
|
|
161
|
-
url: i,
|
|
162
|
-
fetcher: n
|
|
163
|
-
}), t[4] = n, t[5] = i, t[6] = a) : a = t[6];
|
|
152
|
+
function useEndpointRunner(e, t) {
|
|
153
|
+
let n = o(14), r, i, a;
|
|
154
|
+
n[0] === e ? (r = n[1], i = n[2], a = n[3]) : ({url: a, fetcher: r, ...i} = e, n[0] = e, n[1] = r, n[2] = i, n[3] = a);
|
|
164
155
|
let s;
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
}, t[
|
|
156
|
+
n[4] !== r || n[5] !== t || n[6] !== a ? (s = postQuery({
|
|
157
|
+
url: a,
|
|
158
|
+
fetcher: r
|
|
159
|
+
}, t), n[4] = r, n[5] = t, n[6] = a, n[7] = s) : s = n[7];
|
|
169
160
|
let c;
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
}
|
|
161
|
+
n[8] !== i || n[9] !== s ? (c = {
|
|
162
|
+
...i,
|
|
163
|
+
execute: s
|
|
164
|
+
}, n[8] = i, n[9] = s, n[10] = c) : c = n[10];
|
|
165
|
+
let l;
|
|
166
|
+
return n[11] !== r || n[12] !== a ? (l = endpointIdentity({
|
|
167
|
+
url: a,
|
|
168
|
+
fetcher: r
|
|
169
|
+
}), n[11] = r, n[12] = a, n[13] = l) : l = n[13], useQueryRunner(c, l);
|
|
174
170
|
}
|
|
175
171
|
//#endregion
|
|
176
172
|
//#region src/hooks/useFetcherCountQuery.ts
|
|
177
173
|
function useFetcherCountQuery(e) {
|
|
178
|
-
return useEndpointRunner(e);
|
|
174
|
+
return useEndpointRunner(e, "count");
|
|
179
175
|
}
|
|
180
176
|
//#endregion
|
|
181
177
|
//#region src/hooks/useFetcherListQuery.ts
|
|
182
178
|
function useFetcherListQuery(e) {
|
|
183
|
-
return useEndpointRunner(e);
|
|
179
|
+
return useEndpointRunner(e, "list");
|
|
184
180
|
}
|
|
185
181
|
async function readStreamRows(e, t, n) {
|
|
186
182
|
let r = e.getReader(), i = [], a = 0, o = -Infinity, s, flush = () => {
|
|
@@ -210,7 +206,7 @@ async function readStreamRows(e, t, n) {
|
|
|
210
206
|
//#region src/internal/useListStream.ts
|
|
211
207
|
function useListStream(e, t) {
|
|
212
208
|
let n = o(18), [r, i] = a(_temp), s = e.execute, t0;
|
|
213
|
-
n[0] === s ? t0 = n[1] : (t0 = async (e, t
|
|
209
|
+
n[0] === s ? t0 = n[1] : (t0 = async (e, t) => (i([]), readStreamRows(await s(e, t), t.signal, i)), n[0] = s, n[1] = t0);
|
|
214
210
|
let c;
|
|
215
211
|
n[2] !== e || n[3] !== t0 ? (c = {
|
|
216
212
|
...e,
|
|
@@ -261,12 +257,12 @@ function useFetcherListStreamQuery(e) {
|
|
|
261
257
|
//#endregion
|
|
262
258
|
//#region src/hooks/useFetcherPagedQuery.ts
|
|
263
259
|
function useFetcherPagedQuery(e) {
|
|
264
|
-
return useEndpointRunner(e);
|
|
260
|
+
return useEndpointRunner(e, "paged");
|
|
265
261
|
}
|
|
266
262
|
//#endregion
|
|
267
263
|
//#region src/hooks/useFetcherSingleQuery.ts
|
|
268
264
|
function useFetcherSingleQuery(e) {
|
|
269
|
-
return useEndpointRunner(e);
|
|
265
|
+
return useEndpointRunner(e, "single");
|
|
270
266
|
}
|
|
271
267
|
//#endregion
|
|
272
268
|
//#region src/hooks/useListQuery.ts
|