@ahoo-wang/wow-react 10.0.0-alpha.6 → 10.0.0-alpha.7
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 +138 -105
- package/README.zh-CN.md +115 -85
- package/dist/hooks/useAggregateQuery.d.ts +69 -0
- package/dist/hooks/useCountQuery.d.ts +5 -8
- package/dist/hooks/useCursorQuery.d.ts +90 -0
- package/dist/hooks/useListQuery.d.ts +7 -10
- package/dist/hooks/usePagedQuery.d.ts +8 -11
- package/dist/hooks/useSingleQuery.d.ts +14 -13
- package/dist/hooks/useStreamQuery.d.ts +109 -0
- package/dist/index.d.ts +3 -6
- package/dist/index.es.js +158 -178
- package/dist/index.es.js.map +1 -1
- package/dist/internal/queryTransitions.d.ts +6 -6
- package/dist/internal/readStreamRows.d.ts +5 -3
- package/dist/internal/useQueryRunner.d.ts +11 -9
- package/dist/types.d.ts +34 -22
- package/package.json +7 -8
- package/dist/hooks/useFetcherCountQuery.d.ts +0 -66
- package/dist/hooks/useFetcherListQuery.d.ts +0 -80
- package/dist/hooks/useFetcherListStreamQuery.d.ts +0 -66
- package/dist/hooks/useFetcherPagedQuery.d.ts +0 -75
- package/dist/hooks/useFetcherSingleQuery.d.ts +0 -71
- package/dist/hooks/useListStreamQuery.d.ts +0 -93
- package/dist/internal/endpoint.d.ts +0 -38
- package/dist/internal/useEndpointRunner.d.ts +0 -8
- package/dist/internal/useListStream.d.ts +0 -11
package/README.md
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
# `@ahoo-wang/wow-react`
|
|
2
2
|
|
|
3
3
|
React hooks for [Wow](https://github.com/Ahoo-Wang/Wow) queries: single, list,
|
|
4
|
-
paged, count and list-stream. They keep a query, its result, loading and error
|
|
4
|
+
paged, count, aggregate, cursor and list-stream. They keep a query, its result, loading and error
|
|
5
5
|
as React state, on the query types of `@ahoo-wang/wow-client`.
|
|
6
6
|
|
|
7
7
|
## Requirements
|
|
@@ -44,10 +44,7 @@ supplies the route, the headers and the response handling, and nothing is
|
|
|
44
44
|
hand-written:
|
|
45
45
|
|
|
46
46
|
```tsx
|
|
47
|
-
import type {
|
|
48
|
-
SnapshotQueryClient,
|
|
49
|
-
SnapshotRecord,
|
|
50
|
-
} from '@ahoo-wang/wow-client';
|
|
47
|
+
import type { SnapshotQueryClient, Snapshot } from '@ahoo-wang/wow-client';
|
|
51
48
|
import { filter, pagedQuery } from '@ahoo-wang/wow-client/dsl';
|
|
52
49
|
import { usePagedQuery } from '@ahoo-wang/wow-react';
|
|
53
50
|
|
|
@@ -65,7 +62,7 @@ export function PaidOrders({
|
|
|
65
62
|
page: number;
|
|
66
63
|
}) {
|
|
67
64
|
const { result, loading, error } = usePagedQuery<
|
|
68
|
-
|
|
65
|
+
Snapshot<OrderState>,
|
|
69
66
|
OrderFields
|
|
70
67
|
>({
|
|
71
68
|
query: pagedQuery({
|
|
@@ -95,78 +92,98 @@ request. Hand a method over through an arrow function, as above: a method
|
|
|
95
92
|
passed alone loses its `this`. A snapshot query filters the snapshot record,
|
|
96
93
|
whose state lives under `state`, so a state field is `state.status`, not
|
|
97
94
|
`status`. `requestOptions` adds the scope, the space or more headers to every
|
|
98
|
-
run.
|
|
95
|
+
run. Create the client once, outside the component or memoized, not inside
|
|
96
|
+
`execute`: one client serves every request.
|
|
99
97
|
|
|
100
|
-
|
|
101
|
-
an effect with an `AbortController` and abort it on cleanup.
|
|
98
|
+
## Reading page by page
|
|
102
99
|
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
## With an endpoint URL
|
|
110
|
-
|
|
111
|
-
The `useFetcher*Query` hooks POST the query to a query route through a
|
|
112
|
-
Fetcher instead:
|
|
100
|
+
`useCursorQuery` reads a forward-only cursor query and keeps the rows of every
|
|
101
|
+
page read; `fetchNext()` reads the next page and appends it. The hook owns the
|
|
102
|
+
cursor, so its query has none (a `cursor` there does not compile): every run
|
|
103
|
+
starts at the first page, and `execute` receives the query with the cursor of
|
|
104
|
+
the page it reads.
|
|
113
105
|
|
|
114
106
|
```tsx
|
|
115
|
-
import {
|
|
116
|
-
import
|
|
117
|
-
import {
|
|
118
|
-
import { useFetcherPagedQuery } from '@ahoo-wang/wow-react';
|
|
107
|
+
import type { SnapshotQueryClient, Snapshot } from '@ahoo-wang/wow-client';
|
|
108
|
+
import { filter } from '@ahoo-wang/wow-client/dsl';
|
|
109
|
+
import { useCursorQuery } from '@ahoo-wang/wow-react';
|
|
119
110
|
|
|
120
|
-
|
|
111
|
+
interface OrderState {
|
|
112
|
+
status: string;
|
|
113
|
+
}
|
|
114
|
+
type OrderFields = 'state.status';
|
|
121
115
|
|
|
122
|
-
export function
|
|
123
|
-
|
|
124
|
-
|
|
116
|
+
export function PaidOrderPages({
|
|
117
|
+
client,
|
|
118
|
+
}: {
|
|
119
|
+
client: SnapshotQueryClient<OrderState, OrderFields>;
|
|
120
|
+
}) {
|
|
121
|
+
const { items, hasMore, loading, error, fetchNext } = useCursorQuery<
|
|
122
|
+
Snapshot<OrderState>,
|
|
123
|
+
OrderFields
|
|
125
124
|
>({
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
initialQuery: pagedQuery({
|
|
129
|
-
filter: filter.eq('state.status', 'PAID'),
|
|
130
|
-
pagination: { index: 1, size: 20 },
|
|
131
|
-
}),
|
|
125
|
+
query: { filter: filter.eq('state.status', 'PAID'), size: 50 },
|
|
126
|
+
execute: (query, options) => client.cursor(query, options),
|
|
132
127
|
});
|
|
133
|
-
|
|
128
|
+
|
|
129
|
+
if (error) return <p role="alert">{error.message}</p>;
|
|
130
|
+
return (
|
|
131
|
+
<>
|
|
132
|
+
<ul>
|
|
133
|
+
{items.map(order => (
|
|
134
|
+
<li key={order.aggregateId}>{order.state.status}</li>
|
|
135
|
+
))}
|
|
136
|
+
</ul>
|
|
137
|
+
{hasMore && (
|
|
138
|
+
<button disabled={loading} onClick={() => void fetchNext()}>
|
|
139
|
+
More
|
|
140
|
+
</button>
|
|
141
|
+
)}
|
|
142
|
+
</>
|
|
143
|
+
);
|
|
134
144
|
}
|
|
135
145
|
```
|
|
136
146
|
|
|
137
|
-
`
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
`list`, `paged`, `count`) in the query's `kind`.
|
|
147
|
+
`fetchNext()` does nothing while a page is loading or after the last page. A
|
|
148
|
+
failed `fetchNext()` keeps the rows and the cursor, so calling it again
|
|
149
|
+
retries that page; a new query and `execute()` start again from the first
|
|
150
|
+
page, and `reset()` empties the rows.
|
|
142
151
|
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
count as one endpoint even when their interceptors differ (for example one per
|
|
146
|
-
tenant), so switching between them does not run the query again. Give each a
|
|
147
|
-
name, or pass the name of a registered Fetcher, to tell them apart.
|
|
152
|
+
`useAggregateQuery` runs an aggregation, typically `client.aggregate`, and
|
|
153
|
+
keeps its rows as `result`, one per group.
|
|
148
154
|
|
|
149
155
|
## Streaming a list
|
|
150
156
|
|
|
151
|
-
`
|
|
152
|
-
|
|
153
|
-
|
|
157
|
+
`useStreamQuery` reads a list as server-sent events and keeps the records as
|
|
158
|
+
they arrive. Pass a client's `streamRows` as `execute`: it sends
|
|
159
|
+
`Accept: text/event-stream`, resumes a stream the service cuts off where it
|
|
160
|
+
stopped, and answers each record with its position, which the hook keeps as
|
|
161
|
+
`position`.
|
|
154
162
|
|
|
155
163
|
```tsx
|
|
156
|
-
import type {
|
|
164
|
+
import type { SnapshotQueryClient, Snapshot } from '@ahoo-wang/wow-client';
|
|
157
165
|
import { filter, listQuery } from '@ahoo-wang/wow-client/dsl';
|
|
158
|
-
import {
|
|
166
|
+
import { useStreamQuery } from '@ahoo-wang/wow-react';
|
|
167
|
+
|
|
168
|
+
interface OrderState {
|
|
169
|
+
status: string;
|
|
170
|
+
}
|
|
171
|
+
type OrderFields = 'state.status';
|
|
159
172
|
|
|
160
|
-
export function PaidOrderFeed(
|
|
161
|
-
|
|
162
|
-
|
|
173
|
+
export function PaidOrderFeed({
|
|
174
|
+
client,
|
|
175
|
+
}: {
|
|
176
|
+
client: SnapshotQueryClient<OrderState, OrderFields>;
|
|
177
|
+
}) {
|
|
178
|
+
const { items, done, loading, error, abort } = useStreamQuery<
|
|
179
|
+
Snapshot<OrderState>,
|
|
180
|
+
OrderFields
|
|
163
181
|
>({
|
|
164
|
-
fetcher,
|
|
165
|
-
url: '/shop/order/snapshot/query',
|
|
166
182
|
initialQuery: listQuery({
|
|
167
183
|
filter: filter.eq('state.status', 'PAID'),
|
|
168
184
|
limit: 100,
|
|
169
185
|
}),
|
|
186
|
+
execute: (query, options) => client.streamRows(query, options),
|
|
170
187
|
});
|
|
171
188
|
|
|
172
189
|
if (error) return <p role="alert">{error.message}</p>;
|
|
@@ -184,31 +201,38 @@ export function PaidOrderFeed() {
|
|
|
184
201
|
}
|
|
185
202
|
```
|
|
186
203
|
|
|
187
|
-
| Field
|
|
188
|
-
|
|
|
189
|
-
| `items`
|
|
190
|
-
| `
|
|
191
|
-
| `
|
|
192
|
-
| `
|
|
193
|
-
| `
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
204
|
+
| Field | Meaning |
|
|
205
|
+
| ---------- | ------------------------------------------------------------------------------------------------------------------------------- |
|
|
206
|
+
| `items` | The records of the current query received so far, in order. A new query starts empty; `reset()` empties it. |
|
|
207
|
+
| `position` | The position of the last record received that had one; a new query and `reset()` clear it. |
|
|
208
|
+
| `done` | The stream ended normally: `items` holds every record. The same as `status === 'success'`. |
|
|
209
|
+
| `loading` | From the request until the stream ends, fails or is aborted. |
|
|
210
|
+
| `problem` | The `WowProblemError` of a failed request, or of the error event the service sends midway. |
|
|
211
|
+
| `abort()` | Stops the stream and keeps the rows received. `reset()` stops it and empties `items`. `execute()` runs the current query again. |
|
|
212
|
+
|
|
213
|
+
The hook owns the stream: it reads it, renders the records at most about once
|
|
214
|
+
a frame (every 16 ms) however many arrive, and cancels it when a newer query
|
|
215
|
+
starts, on `abort()` or `reset()`, and on unmount. Components never hold a
|
|
216
|
+
reader, so the hook is safe under StrictMode.
|
|
217
|
+
|
|
218
|
+
To go on where a stream stopped after the component remounts (a page change,
|
|
219
|
+
a reload), keep `position` somewhere that outlives it and hand it back as
|
|
220
|
+
`requestOptions: { resume: { lastEventId: saved } }`; the first request then
|
|
221
|
+
asks for the records after it.
|
|
201
222
|
|
|
202
223
|
## State and errors
|
|
203
224
|
|
|
204
|
-
Every hook runs its query on mount and whenever `query`
|
|
205
|
-
or `setQuery()` changes it
|
|
206
|
-
`
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
225
|
+
Every hook runs its query on mount and whenever `query` or `requestOptions`
|
|
226
|
+
(both compared by content) or `setQuery()` changes it; set
|
|
227
|
+
`autoExecute: false` to run it only through `execute()`. `requestOptions`
|
|
228
|
+
counts because the scope, the space and the headers can change the answer: a
|
|
229
|
+
header whose value changes on every render runs the query again on every
|
|
230
|
+
render, so keep such values stable. A newer query aborts the request in
|
|
231
|
+
flight, so a late response never overwrites a newer result. A controlled
|
|
232
|
+
`query` set back to `undefined` (`id ? singleQuery(…) : undefined`) aborts the
|
|
233
|
+
request in flight and goes `idle`, keeping the last result; nothing runs until
|
|
234
|
+
a query is given again. `onSuccess` and `onError` are called with the result
|
|
235
|
+
and the error.
|
|
212
236
|
|
|
213
237
|
A failed request and `abort()` keep the last `result`, so a failed refresh does
|
|
214
238
|
not blank the screen; `reset()` aborts the request in flight and clears
|
|
@@ -217,40 +241,49 @@ server too, as `loading`, and sends no request during server rendering. There
|
|
|
217
241
|
is no `initialResult` option: server-rendered HTML holds the loading state, and
|
|
218
242
|
the query runs once the page hydrates.
|
|
219
243
|
|
|
220
|
-
Every hook returns `status`, `loading`, `result` (`items`
|
|
221
|
-
|
|
222
|
-
option and return types of each hook extend
|
|
223
|
-
`QueryHookReturn`, which this package declares and
|
|
224
|
-
and `
|
|
225
|
-
`'idle' | 'loading' | 'success' | 'error'`, so a
|
|
226
|
-
can write a literal.
|
|
227
|
-
|
|
228
|
-
`error` is
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
) {
|
|
241
|
-
|
|
244
|
+
Every hook returns `status`, `loading`, `result` (`items` for the stream and
|
|
245
|
+
cursor hooks), `error`, `problem`, `execute`, `abort`, `reset`, `getQuery` and
|
|
246
|
+
`setQuery`. The option and return types of each hook extend
|
|
247
|
+
`QueryHookOptions` and `QueryHookReturn`, which this package declares and
|
|
248
|
+
exports with `QueryStatus`, `QueryExecutor` and `StreamExecutor`. `status` is
|
|
249
|
+
the plain string union `'idle' | 'loading' | 'success' | 'error'`, so a
|
|
250
|
+
component's props or a test can write a literal.
|
|
251
|
+
|
|
252
|
+
`error` is an `Error`: what the run failed with, a thrown value that is not an
|
|
253
|
+
`Error` wrapped in one with the value as its `cause`. `problem` is that same
|
|
254
|
+
error when a Wow service answered with a problem, a `WowProblemError` with the
|
|
255
|
+
service's `code`, its `status` and retry hint, and `undefined` for a failure no
|
|
256
|
+
service answered, such as a network error:
|
|
257
|
+
|
|
258
|
+
```tsx
|
|
259
|
+
import type { SnapshotQueryClient } from '@ahoo-wang/wow-client';
|
|
260
|
+
import { FrameworkErrorCodes } from '@ahoo-wang/wow-client';
|
|
261
|
+
import { filter } from '@ahoo-wang/wow-client/dsl';
|
|
262
|
+
import { useCountQuery } from '@ahoo-wang/wow-react';
|
|
263
|
+
|
|
264
|
+
export function OrderCount({ client }: { client: SnapshotQueryClient }) {
|
|
265
|
+
const { result, problem, error } = useCountQuery({
|
|
266
|
+
query: { filter: filter.matchAll() },
|
|
267
|
+
execute: (query, options) => client.count(query, options),
|
|
268
|
+
});
|
|
269
|
+
if (problem?.code === FrameworkErrorCodes.QUERY_BUSY)
|
|
270
|
+
return <p>Busy, retry in {problem.retry.afterMs} ms</p>;
|
|
271
|
+
if (error) return <p role="alert">{error.message}</p>;
|
|
272
|
+
return <p>{result?.count ?? '…'} orders</p>;
|
|
242
273
|
}
|
|
243
274
|
```
|
|
244
275
|
|
|
245
276
|
## Hooks
|
|
246
277
|
|
|
247
|
-
| Hook
|
|
248
|
-
|
|
|
249
|
-
| `useSingleQuery
|
|
250
|
-
| `useListQuery
|
|
251
|
-
| `usePagedQuery
|
|
252
|
-
| `useCountQuery
|
|
253
|
-
| `
|
|
278
|
+
| Hook | Query | Result |
|
|
279
|
+
| ------------------- | ----------------------------- | ---------------------------------------------------- |
|
|
280
|
+
| `useSingleQuery` | `singleQuery(…)` | `result`: one item |
|
|
281
|
+
| `useListQuery` | `listQuery(…)` | `result`: the rows |
|
|
282
|
+
| `usePagedQuery` | `pagedQuery(…)` | `result`: `{ total, exact, list }` |
|
|
283
|
+
| `useCountQuery` | `{ filter, limit? }` | `result`: `{ count, exact }` |
|
|
284
|
+
| `useAggregateQuery` | `{ groupBy?, metrics, … }` | `result`: the rows, one per group |
|
|
285
|
+
| `useCursorQuery` | `{ filter, sort?, size? }` | `items` of every page read, `hasMore`, `fetchNext()` |
|
|
286
|
+
| `useStreamQuery` | `listQuery(…)`, as SSE events | `items` as they arrive, `position`, then `done` |
|
|
254
287
|
|
|
255
288
|
The queries are built with the DSL of `@ahoo-wang/wow-client/dsl`.
|
|
256
289
|
|
package/README.zh-CN.md
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
# `@ahoo-wang/wow-react`
|
|
2
2
|
|
|
3
|
-
面向 [Wow](https://github.com/Ahoo-Wang/Wow) 查询的 React hook
|
|
4
|
-
|
|
3
|
+
面向 [Wow](https://github.com/Ahoo-Wang/Wow) 查询的 React hook:单条、列表、分页、计数、
|
|
4
|
+
聚合、游标和列表流。它们把查询、结果、加载中与错误保存为 React 状态,建立在
|
|
5
5
|
`@ahoo-wang/wow-client` 的查询类型之上。
|
|
6
6
|
|
|
7
7
|
## 环境要求
|
|
@@ -37,10 +37,7 @@ pnpm add react react-dom @ahoo-wang/fetcher @ahoo-wang/fetcher-eventstream \
|
|
|
37
37
|
最后两个参数恰好就是它们,于是路由、请求头和响应处理都由客户端负责,不用手写:
|
|
38
38
|
|
|
39
39
|
```tsx
|
|
40
|
-
import type {
|
|
41
|
-
SnapshotQueryClient,
|
|
42
|
-
SnapshotRecord,
|
|
43
|
-
} from '@ahoo-wang/wow-client';
|
|
40
|
+
import type { SnapshotQueryClient, Snapshot } from '@ahoo-wang/wow-client';
|
|
44
41
|
import { filter, pagedQuery } from '@ahoo-wang/wow-client/dsl';
|
|
45
42
|
import { usePagedQuery } from '@ahoo-wang/wow-react';
|
|
46
43
|
|
|
@@ -58,7 +55,7 @@ export function PaidOrders({
|
|
|
58
55
|
page: number;
|
|
59
56
|
}) {
|
|
60
57
|
const { result, loading, error } = usePagedQuery<
|
|
61
|
-
|
|
58
|
+
Snapshot<OrderState>,
|
|
62
59
|
OrderFields
|
|
63
60
|
>({
|
|
64
61
|
query: pagedQuery({
|
|
@@ -86,69 +83,90 @@ export function PaidOrders({
|
|
|
86
83
|
包一层箭头函数再交出去:单独交出的方法会丢掉 `this`。
|
|
87
84
|
快照查询过滤的是快照记录,聚合状态在它的 `state` 字段下,所以状态字段写作
|
|
88
85
|
`state.status`,而不是 `status`。`requestOptions` 为每次运行加上作用域、空间或更多请求头。
|
|
86
|
+
客户端在组件之外创建一次(或用 memo 保存),不要在 `execute` 里创建:一个客户端服务所有请求。
|
|
89
87
|
|
|
90
|
-
|
|
91
|
-
`client.aggregate(query, { signal })`,并在清理函数里中止它。
|
|
88
|
+
## 逐页读取
|
|
92
89
|
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
## 配合端点 URL
|
|
98
|
-
|
|
99
|
-
`useFetcher*Query` 这组 hook 则通过 Fetcher 把查询 POST 到一个查询路由:
|
|
90
|
+
`useCursorQuery` 读取只进的游标查询,保存已读各页的行;`fetchNext()` 读取下一页并追加到后面。
|
|
91
|
+
游标归 hook 所有,所以它的查询里没有游标(在那里写 `cursor` 无法通过编译):每次运行都从第一页
|
|
92
|
+
开始,`execute` 收到的查询带着它要读的那一页的游标。
|
|
100
93
|
|
|
101
94
|
```tsx
|
|
102
|
-
import {
|
|
103
|
-
import
|
|
104
|
-
import {
|
|
105
|
-
import { useFetcherPagedQuery } from '@ahoo-wang/wow-react';
|
|
95
|
+
import type { SnapshotQueryClient, Snapshot } from '@ahoo-wang/wow-client';
|
|
96
|
+
import { filter } from '@ahoo-wang/wow-client/dsl';
|
|
97
|
+
import { useCursorQuery } from '@ahoo-wang/wow-react';
|
|
106
98
|
|
|
107
|
-
|
|
99
|
+
interface OrderState {
|
|
100
|
+
status: string;
|
|
101
|
+
}
|
|
102
|
+
type OrderFields = 'state.status';
|
|
108
103
|
|
|
109
|
-
export function
|
|
110
|
-
|
|
111
|
-
|
|
104
|
+
export function PaidOrderPages({
|
|
105
|
+
client,
|
|
106
|
+
}: {
|
|
107
|
+
client: SnapshotQueryClient<OrderState, OrderFields>;
|
|
108
|
+
}) {
|
|
109
|
+
const { items, hasMore, loading, error, fetchNext } = useCursorQuery<
|
|
110
|
+
Snapshot<OrderState>,
|
|
111
|
+
OrderFields
|
|
112
112
|
>({
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
initialQuery: pagedQuery({
|
|
116
|
-
filter: filter.eq('state.status', 'PAID'),
|
|
117
|
-
pagination: { index: 1, size: 20 },
|
|
118
|
-
}),
|
|
113
|
+
query: { filter: filter.eq('state.status', 'PAID'), size: 50 },
|
|
114
|
+
execute: (query, options) => client.cursor(query, options),
|
|
119
115
|
});
|
|
120
|
-
|
|
116
|
+
|
|
117
|
+
if (error) return <p role="alert">{error.message}</p>;
|
|
118
|
+
return (
|
|
119
|
+
<>
|
|
120
|
+
<ul>
|
|
121
|
+
{items.map(order => (
|
|
122
|
+
<li key={order.aggregateId}>{order.state.status}</li>
|
|
123
|
+
))}
|
|
124
|
+
</ul>
|
|
125
|
+
{hasMore && (
|
|
126
|
+
<button disabled={loading} onClick={() => void fetchNext()}>
|
|
127
|
+
更多
|
|
128
|
+
</button>
|
|
129
|
+
)}
|
|
130
|
+
</>
|
|
131
|
+
);
|
|
121
132
|
}
|
|
122
133
|
```
|
|
123
134
|
|
|
124
|
-
`
|
|
125
|
-
|
|
126
|
-
`/{context}/{aggregate}/events/query`;hook 在查询的 `kind` 里写明操作(`single`、`list`、`paged`、`count`)。
|
|
135
|
+
页面加载中或读完最后一页之后,`fetchNext()` 什么也不做。`fetchNext()` 失败时保留已有的行和游标,
|
|
136
|
+
再调用一次就重试那一页;新的查询和 `execute()` 从第一页重新开始,`reset()` 清空已有的行。
|
|
127
137
|
|
|
128
|
-
|
|
129
|
-
两个 `baseURL` 相同的无名 Fetcher 即使拦截器不同(例如按租户各建一个),也被视为同一个端点,
|
|
130
|
-
在它们之间切换不会重新查询。给每个 Fetcher 起名,或者传已注册 Fetcher 的名字,才能区分它们。
|
|
138
|
+
`useAggregateQuery` 运行一个聚合(通常是 `client.aggregate`),把它的行保存为 `result`,每组一行。
|
|
131
139
|
|
|
132
140
|
## 流式读取列表
|
|
133
141
|
|
|
134
|
-
`
|
|
135
|
-
|
|
142
|
+
`useStreamQuery` 以服务端推送事件(SSE)读取列表,记录一到就保存下来。把客户端的
|
|
143
|
+
`streamRows` 作为 `execute` 传入:它发送 `Accept: text/event-stream`,服务端中途切断的流
|
|
144
|
+
会从断点续传,并为每条记录带上位置,hook 把它保存为 `position`。
|
|
136
145
|
|
|
137
146
|
```tsx
|
|
138
|
-
import type {
|
|
147
|
+
import type { SnapshotQueryClient, Snapshot } from '@ahoo-wang/wow-client';
|
|
139
148
|
import { filter, listQuery } from '@ahoo-wang/wow-client/dsl';
|
|
140
|
-
import {
|
|
149
|
+
import { useStreamQuery } from '@ahoo-wang/wow-react';
|
|
150
|
+
|
|
151
|
+
interface OrderState {
|
|
152
|
+
status: string;
|
|
153
|
+
}
|
|
154
|
+
type OrderFields = 'state.status';
|
|
141
155
|
|
|
142
|
-
export function PaidOrderFeed(
|
|
143
|
-
|
|
144
|
-
|
|
156
|
+
export function PaidOrderFeed({
|
|
157
|
+
client,
|
|
158
|
+
}: {
|
|
159
|
+
client: SnapshotQueryClient<OrderState, OrderFields>;
|
|
160
|
+
}) {
|
|
161
|
+
const { items, done, loading, error, abort } = useStreamQuery<
|
|
162
|
+
Snapshot<OrderState>,
|
|
163
|
+
OrderFields
|
|
145
164
|
>({
|
|
146
|
-
fetcher,
|
|
147
|
-
url: '/shop/order/snapshot/query',
|
|
148
165
|
initialQuery: listQuery({
|
|
149
166
|
filter: filter.eq('state.status', 'PAID'),
|
|
150
167
|
limit: 100,
|
|
151
168
|
}),
|
|
169
|
+
execute: (query, options) => client.streamRows(query, options),
|
|
152
170
|
});
|
|
153
171
|
|
|
154
172
|
if (error) return <p role="alert">{error.message}</p>;
|
|
@@ -166,65 +184,77 @@ export function PaidOrderFeed() {
|
|
|
166
184
|
}
|
|
167
185
|
```
|
|
168
186
|
|
|
169
|
-
| 字段
|
|
170
|
-
|
|
|
171
|
-
| `items`
|
|
172
|
-
| `
|
|
173
|
-
| `
|
|
174
|
-
| `
|
|
175
|
-
| `
|
|
187
|
+
| 字段 | 含义 |
|
|
188
|
+
| ---------- | ---------------------------------------------------------------------------------------- |
|
|
189
|
+
| `items` | 当前查询到目前为止收到的记录,按到达顺序。新查询从空列表开始;`reset()` 清空它。 |
|
|
190
|
+
| `position` | 最后一条带位置的记录的位置;新查询和 `reset()` 清除它。 |
|
|
191
|
+
| `done` | 流正常结束,`items` 已包含全部记录。等同于 `status === 'success'`。 |
|
|
192
|
+
| `loading` | 从发出请求直到流结束、出错或被中止。 |
|
|
193
|
+
| `problem` | 请求失败、或服务端在流中途发来错误事件时的 `WowProblemError`。 |
|
|
194
|
+
| `abort()` | 停止读取流并保留已收到的行。`reset()` 停止并清空 `items`。`execute()` 重新执行当前查询。 |
|
|
176
195
|
|
|
177
|
-
流由 hook
|
|
196
|
+
流由 hook 自己持有:它负责读取,无论到达多少记录,最多约每帧(16 毫秒)渲染一次,并在新查询开始、
|
|
178
197
|
调用 `abort()` 或 `reset()`、组件卸载时取消流。组件不接触 reader,所以在 StrictMode 下
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
198
|
+
也是安全的。
|
|
199
|
+
|
|
200
|
+
组件重新挂载后(切换页面、刷新)要从流停下的地方接着读,就把 `position` 存在比组件活得久的
|
|
201
|
+
地方,再以 `requestOptions: { resume: { lastEventId: saved } }` 传回;第一个请求就会请求它之后的记录。
|
|
182
202
|
|
|
183
203
|
## 状态与错误
|
|
184
204
|
|
|
185
|
-
每个 hook 在挂载时执行查询,之后 `query`
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
`id ? singleQuery(…) : undefined
|
|
190
|
-
|
|
205
|
+
每个 hook 在挂载时执行查询,之后 `query` 或 `requestOptions`(都按内容比较)或 `setQuery()`
|
|
206
|
+
改变时再次执行;设置 `autoExecute: false` 则只在调用 `execute()` 时执行。`requestOptions`
|
|
207
|
+
也算在内,是因为作用域、空间和请求头都可能改变答案:每次渲染都变的请求头值会让查询每次渲染都
|
|
208
|
+
重新执行,所以要让这样的值保持稳定。新的查询会中止正在进行的请求,所以迟到的响应不会覆盖较新的
|
|
209
|
+
结果。受控的 `query` 重新变为 `undefined` 时(如 `id ? singleQuery(…) : undefined`),中止进行中的
|
|
210
|
+
请求并回到 `idle`,保留上一次结果;再给出查询之前不会执行。`onSuccess`、`onError` 分别以结果和
|
|
211
|
+
错误为参数调用。
|
|
191
212
|
|
|
192
213
|
请求失败和 `abort()` 都保留上一次的 `result`,刷新失败不会让界面变空白;`reset()`
|
|
193
214
|
会中止进行中的请求,并清空 `result` 与 `error`。挂载即执行的 hook 首帧(服务端也一样)
|
|
194
215
|
就是 `loading`,服务端渲染时不发请求。没有 `initialResult` 选项:服务端渲染出的 HTML
|
|
195
216
|
是加载状态,页面水合之后才执行查询。
|
|
196
217
|
|
|
197
|
-
每个 hook 都返回 `status`、`loading`、`result
|
|
198
|
-
`
|
|
218
|
+
每个 hook 都返回 `status`、`loading`、`result`(流式与游标 hook 为 `items`)、`error`、
|
|
219
|
+
`problem`、`execute`、`abort`、`reset`、`getQuery` 和 `setQuery`。各 hook 的选项与返回
|
|
199
220
|
类型都扩展本包声明并导出的 `QueryHookOptions` 与 `QueryHookReturn`,同时导出的还有
|
|
200
|
-
`QueryStatus` 与 `
|
|
221
|
+
`QueryStatus`、`QueryExecutor` 与 `StreamExecutor`。`status` 是普通的字符串联合类型
|
|
201
222
|
`'idle' | 'loading' | 'success' | 'error'`,组件 props 或测试里可以直接写字面量。
|
|
202
223
|
|
|
203
|
-
`error`
|
|
204
|
-
|
|
205
|
-
`code`
|
|
206
|
-
|
|
207
|
-
```ts
|
|
208
|
-
import { FrameworkErrorCodes, WowProblemError } from '@ahoo-wang/wow-client';
|
|
224
|
+
`error` 是一个 `Error`:本次运行失败的原因;抛出的值不是 `Error` 时,会被包进一个 `Error`,
|
|
225
|
+
原值作为它的 `cause`。Wow 服务以 problem 应答时,`problem` 就是这个错误,一个带着服务的
|
|
226
|
+
`code`、`status` 和重试提示的 `WowProblemError`;没有服务应答的失败(例如网络错误)时它是
|
|
227
|
+
`undefined`:
|
|
209
228
|
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
229
|
+
```tsx
|
|
230
|
+
import type { SnapshotQueryClient } from '@ahoo-wang/wow-client';
|
|
231
|
+
import { FrameworkErrorCodes } from '@ahoo-wang/wow-client';
|
|
232
|
+
import { filter } from '@ahoo-wang/wow-client/dsl';
|
|
233
|
+
import { useCountQuery } from '@ahoo-wang/wow-react';
|
|
234
|
+
|
|
235
|
+
export function OrderCount({ client }: { client: SnapshotQueryClient }) {
|
|
236
|
+
const { result, problem, error } = useCountQuery({
|
|
237
|
+
query: { filter: filter.matchAll() },
|
|
238
|
+
execute: (query, options) => client.count(query, options),
|
|
239
|
+
});
|
|
240
|
+
if (problem?.code === FrameworkErrorCodes.QUERY_BUSY)
|
|
241
|
+
return <p>繁忙,{problem.retry.afterMs} 毫秒后重试</p>;
|
|
242
|
+
if (error) return <p role="alert">{error.message}</p>;
|
|
243
|
+
return <p>共 {result?.count ?? '…'} 个订单</p>;
|
|
216
244
|
}
|
|
217
245
|
```
|
|
218
246
|
|
|
219
247
|
## Hook 一览
|
|
220
248
|
|
|
221
|
-
| Hook
|
|
222
|
-
|
|
|
223
|
-
| `useSingleQuery
|
|
224
|
-
| `useListQuery
|
|
225
|
-
| `usePagedQuery
|
|
226
|
-
| `useCountQuery
|
|
227
|
-
| `
|
|
249
|
+
| Hook | 查询 | 结果 |
|
|
250
|
+
| ------------------- | --------------------------- | -------------------------------------------- |
|
|
251
|
+
| `useSingleQuery` | `singleQuery(…)` | `result`:一条 |
|
|
252
|
+
| `useListQuery` | `listQuery(…)` | `result`:若干行 |
|
|
253
|
+
| `usePagedQuery` | `pagedQuery(…)` | `result`:`{ total, exact, list }` |
|
|
254
|
+
| `useCountQuery` | `{ filter, limit? }` | `result`:`{ count, exact }` |
|
|
255
|
+
| `useAggregateQuery` | `{ groupBy?, metrics, … }` | `result`:若干行,每组一行 |
|
|
256
|
+
| `useCursorQuery` | `{ filter, sort?, size? }` | 已读各页的 `items`、`hasMore`、`fetchNext()` |
|
|
257
|
+
| `useStreamQuery` | `listQuery(…)`,以 SSE 事件 | `items` 陆续到达、`position`,结束后 `done` |
|
|
228
258
|
|
|
229
259
|
查询用 `@ahoo-wang/wow-client/dsl` 的 DSL 构造。
|
|
230
260
|
|