@molecule/app-query-react 1.0.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/LICENSE +115 -0
- package/README.md +157 -0
- package/dist/index.d.ts +51 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +50 -0
- package/dist/usePrefetch.d.ts +36 -0
- package/dist/usePrefetch.d.ts.map +1 -0
- package/dist/usePrefetch.js +39 -0
- package/dist/useQuery.d.ts +18 -0
- package/dist/useQuery.d.ts.map +1 -0
- package/dist/useQuery.js +45 -0
- package/package.json +57 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work.
|
|
38
|
+
|
|
39
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
40
|
+
form, that is based on (or derived from) the Work and for which the
|
|
41
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
42
|
+
represent, as a whole, an original work of authorship.
|
|
43
|
+
|
|
44
|
+
"Contribution" shall mean any work of authorship, including the
|
|
45
|
+
original version of the Work and any modifications or additions
|
|
46
|
+
to that Work, that is intentionally submitted to the Licensor for
|
|
47
|
+
inclusion in the Work by the copyright owner or by an individual or
|
|
48
|
+
Legal Entity authorized to submit on behalf of the copyright owner.
|
|
49
|
+
|
|
50
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
51
|
+
on behalf of whom a Contribution has been received by the Licensor and
|
|
52
|
+
subsequently incorporated within the Work.
|
|
53
|
+
|
|
54
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
55
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
56
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
57
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
58
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
59
|
+
Work and such Derivative Works in Source or Object form.
|
|
60
|
+
|
|
61
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
62
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
63
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
64
|
+
patent license to make, have made, use, offer to sell, sell, import,
|
|
65
|
+
and otherwise transfer the Work.
|
|
66
|
+
|
|
67
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
68
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
69
|
+
modifications, and in Source or Object form, provided that You
|
|
70
|
+
meet the following conditions:
|
|
71
|
+
|
|
72
|
+
(a) You must give any other recipients of the Work or
|
|
73
|
+
Derivative Works a copy of this License; and
|
|
74
|
+
|
|
75
|
+
(b) You must cause any modified files to carry prominent notices
|
|
76
|
+
stating that You changed the files; and
|
|
77
|
+
|
|
78
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
79
|
+
that You distribute, all copyright, patent, trademark, and
|
|
80
|
+
attribution notices from the Source form of the Work,
|
|
81
|
+
excluding those notices that do not pertain to any part of
|
|
82
|
+
the Derivative Works; and
|
|
83
|
+
|
|
84
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
85
|
+
distribution, then any Derivative Works that You distribute must
|
|
86
|
+
include a readable copy of the attribution notices contained
|
|
87
|
+
within such NOTICE file.
|
|
88
|
+
|
|
89
|
+
5. Submission of Contributions.
|
|
90
|
+
|
|
91
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
92
|
+
names, trademarks, service marks, or product names of the Licensor.
|
|
93
|
+
|
|
94
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
95
|
+
agreed to in writing, Licensor provides the Work on an "AS IS" BASIS,
|
|
96
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND.
|
|
97
|
+
|
|
98
|
+
8. Limitation of Liability. In no event and under no legal theory shall
|
|
99
|
+
any Contributor be liable to You for damages.
|
|
100
|
+
|
|
101
|
+
9. Accepting Warranty or Additional Liability.
|
|
102
|
+
|
|
103
|
+
Copyright 2026 Molecule Dev, Inc.
|
|
104
|
+
|
|
105
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
106
|
+
you may not use this file except in compliance with the License.
|
|
107
|
+
You may obtain a copy of the License at
|
|
108
|
+
|
|
109
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
110
|
+
|
|
111
|
+
Unless required by applicable law or agreed to in writing, software
|
|
112
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
113
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
114
|
+
See the License for the specific language governing permissions and
|
|
115
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
<!--
|
|
2
|
+
AUTO-GENERATED — DO NOT EDIT THIS FILE.
|
|
3
|
+
Generated by `mlcl sync-docs` from the package's src/index.ts JSDoc + mlcl/registry.json.
|
|
4
|
+
Edits here are overwritten on the next commit (molecule's pre-commit hook regenerates).
|
|
5
|
+
To change this document, edit the module-level JSDoc in src/index.ts.
|
|
6
|
+
Generated: 2026-09-28T07:09:02.750Z
|
|
7
|
+
-->
|
|
8
|
+
|
|
9
|
+
# @molecule/app-query-react
|
|
10
|
+
|
|
11
|
+
> **Auto-generated, AI-first package reference** for the [molecule.dev](https://molecule.dev) ecosystem.
|
|
12
|
+
> It is written to be read by coding agents as much as by people, and is generated from this
|
|
13
|
+
> package's source — edit `src/index.ts` JSDoc, not this file.
|
|
14
|
+
|
|
15
|
+
React hooks for `@molecule/app-query`.
|
|
16
|
+
|
|
17
|
+
{@link useQuery} observes one document from the app's shared cache and
|
|
18
|
+
returns cached data on the first render; {@link usePrefetch} and
|
|
19
|
+
{@link intentPrefetchProps} warm a document when a person is about to
|
|
20
|
+
open it. Together they make a detail page open with no loading state.
|
|
21
|
+
|
|
22
|
+
## Quick Start
|
|
23
|
+
|
|
24
|
+
```tsx
|
|
25
|
+
import { intentPrefetchProps, useQuery } from '@molecule/app-query-react'
|
|
26
|
+
|
|
27
|
+
const packageQuery = (name: string) => ({
|
|
28
|
+
key: ['package', name],
|
|
29
|
+
fetch: (signal: AbortSignal) =>
|
|
30
|
+
fetch(`/data/packages/${name}.json`, { signal }).then((r) => r.json()),
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
function PackageLink({ name }: { name: string }) {
|
|
34
|
+
return (
|
|
35
|
+
<Link to={`/packages/${name}`} {...intentPrefetchProps(packageQuery(name))}>
|
|
36
|
+
{name}
|
|
37
|
+
</Link>
|
|
38
|
+
)
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function PackagePage({ name }: { name: string }) {
|
|
42
|
+
const { data, status } = useQuery(packageQuery(name))
|
|
43
|
+
if (!data) return status === 'error' ? <NotFound /> : <Skeleton />
|
|
44
|
+
return <Article doc={data} />
|
|
45
|
+
}
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
## Type
|
|
49
|
+
|
|
50
|
+
`feature`
|
|
51
|
+
|
|
52
|
+
## Installation
|
|
53
|
+
|
|
54
|
+
```bash
|
|
55
|
+
npm install @molecule/app-query-react @molecule/app-query react
|
|
56
|
+
npm install -D @types/react
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
## API
|
|
60
|
+
|
|
61
|
+
### Interfaces
|
|
62
|
+
|
|
63
|
+
#### `IntentPrefetchProps`
|
|
64
|
+
|
|
65
|
+
The handlers {@link intentPrefetchProps} returns.
|
|
66
|
+
|
|
67
|
+
```typescript
|
|
68
|
+
interface IntentPrefetchProps {
|
|
69
|
+
onPointerEnter: () => void
|
|
70
|
+
onFocus: () => void
|
|
71
|
+
onTouchStart: () => void
|
|
72
|
+
}
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
### Functions
|
|
76
|
+
|
|
77
|
+
#### `intentPrefetchProps(options)`
|
|
78
|
+
|
|
79
|
+
Event handlers that warm a document at the first sign of intent: the
|
|
80
|
+
pointer arriving, keyboard focus, or a touch starting. Spread them onto a
|
|
81
|
+
link: `<Link {...intentPrefetchProps(packageQuery(name))} />`.
|
|
82
|
+
|
|
83
|
+
```typescript
|
|
84
|
+
function intentPrefetchProps(options: QueryOptions<T>): IntentPrefetchProps
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
- `options` — Key, fetch, freshness.
|
|
88
|
+
|
|
89
|
+
**Returns:** The handlers.
|
|
90
|
+
|
|
91
|
+
#### `prefetchQuery(options)`
|
|
92
|
+
|
|
93
|
+
Warms the cache for a document, when the connection can afford it (see
|
|
94
|
+
`shouldPrefetch()` in the core). Safe to call repeatedly: a fresh or
|
|
95
|
+
in-flight document is skipped, and failures are swallowed.
|
|
96
|
+
|
|
97
|
+
```typescript
|
|
98
|
+
function prefetchQuery(options: QueryOptions<T>): void
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
- `options` — Key, fetch, freshness.
|
|
102
|
+
|
|
103
|
+
#### `usePrefetch()`
|
|
104
|
+
|
|
105
|
+
A stable `prefetch(options)` function for event handlers.
|
|
106
|
+
|
|
107
|
+
```typescript
|
|
108
|
+
function usePrefetch(): <T>(options: QueryOptions<T>) => void
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
**Returns:** The prefetch function.
|
|
112
|
+
|
|
113
|
+
#### `useQuery(options)`
|
|
114
|
+
|
|
115
|
+
The document for `options.key`, kept current: cached data is returned on
|
|
116
|
+
the very first render (so a warmed page never flashes a loading state),
|
|
117
|
+
a stale or missing document is fetched, and every change re-renders.
|
|
118
|
+
Pass `null` to observe nothing. A key change resets to that key's state;
|
|
119
|
+
the hook never hands a caller another key's document.
|
|
120
|
+
|
|
121
|
+
```typescript
|
|
122
|
+
function useQuery(options: QueryOptions<T> | null): QueryState<T>
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
- `options` — Key, fetch, freshness; or `null`.
|
|
126
|
+
|
|
127
|
+
**Returns:** The document's state.
|
|
128
|
+
|
|
129
|
+
## Injection Notes
|
|
130
|
+
|
|
131
|
+
### Requirements
|
|
132
|
+
|
|
133
|
+
Peer dependencies:
|
|
134
|
+
|
|
135
|
+
- `@molecule/app-query` ^1.0.0
|
|
136
|
+
- `react` ^18.0.0 || ^19.0.0
|
|
137
|
+
|
|
138
|
+
### Runtime Dependencies
|
|
139
|
+
|
|
140
|
+
- `@molecule/app-query`
|
|
141
|
+
- `react`
|
|
142
|
+
|
|
143
|
+
- **`useQuery` reads the cache synchronously on the first render.** A
|
|
144
|
+
document prefetched from a hovered link, or seeded with `client.set()`
|
|
145
|
+
from a server-rendered JSON island, renders at once — no spinner, no
|
|
146
|
+
layout shift. Render a loading state only when `data` is `undefined`.
|
|
147
|
+
- **The key is the identity.** Building `options` inline is fine; the
|
|
148
|
+
hook resubscribes only when the key's value changes, not when the
|
|
149
|
+
`fetch` closure does.
|
|
150
|
+
- `intentPrefetchProps` spreads three handlers onto a link. It is a plain
|
|
151
|
+
function (not a hook), so it works inside `.map()` too.
|
|
152
|
+
- Prefetching respects the browser's data-saver setting and skips 2G
|
|
153
|
+
connections (`shouldPrefetch()` in the core) — do not bypass that for
|
|
154
|
+
"important" links; the person can still click.
|
|
155
|
+
- Errors are not thrown from the hook; read `status === 'error'` and
|
|
156
|
+
`error`. A refetch after an error is one more subscription (a re-mount
|
|
157
|
+
or a key change) or a `client.invalidate()`.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* React hooks for `@molecule/app-query`.
|
|
3
|
+
*
|
|
4
|
+
* {@link useQuery} observes one document from the app's shared cache and
|
|
5
|
+
* returns cached data on the first render; {@link usePrefetch} and
|
|
6
|
+
* {@link intentPrefetchProps} warm a document when a person is about to
|
|
7
|
+
* open it. Together they make a detail page open with no loading state.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```tsx
|
|
11
|
+
* import { intentPrefetchProps, useQuery } from '@molecule/app-query-react'
|
|
12
|
+
*
|
|
13
|
+
* const packageQuery = (name: string) => ({
|
|
14
|
+
* key: ['package', name],
|
|
15
|
+
* fetch: (signal: AbortSignal) =>
|
|
16
|
+
* fetch(`/data/packages/${name}.json`, { signal }).then((r) => r.json()),
|
|
17
|
+
* })
|
|
18
|
+
*
|
|
19
|
+
* function PackageLink({ name }: { name: string }) {
|
|
20
|
+
* return <Link to={`/packages/${name}`} {...intentPrefetchProps(packageQuery(name))}>{name}</Link>
|
|
21
|
+
* }
|
|
22
|
+
*
|
|
23
|
+
* function PackagePage({ name }: { name: string }) {
|
|
24
|
+
* const { data, status } = useQuery(packageQuery(name))
|
|
25
|
+
* if (!data) return status === 'error' ? <NotFound /> : <Skeleton />
|
|
26
|
+
* return <Article doc={data} />
|
|
27
|
+
* }
|
|
28
|
+
* ```
|
|
29
|
+
*
|
|
30
|
+
* @remarks
|
|
31
|
+
* - **`useQuery` reads the cache synchronously on the first render.** A
|
|
32
|
+
* document prefetched from a hovered link, or seeded with `client.set()`
|
|
33
|
+
* from a server-rendered JSON island, renders at once — no spinner, no
|
|
34
|
+
* layout shift. Render a loading state only when `data` is `undefined`.
|
|
35
|
+
* - **The key is the identity.** Building `options` inline is fine; the
|
|
36
|
+
* hook resubscribes only when the key's value changes, not when the
|
|
37
|
+
* `fetch` closure does.
|
|
38
|
+
* - `intentPrefetchProps` spreads three handlers onto a link. It is a plain
|
|
39
|
+
* function (not a hook), so it works inside `.map()` too.
|
|
40
|
+
* - Prefetching respects the browser's data-saver setting and skips 2G
|
|
41
|
+
* connections (`shouldPrefetch()` in the core) — do not bypass that for
|
|
42
|
+
* "important" links; the person can still click.
|
|
43
|
+
* - Errors are not thrown from the hook; read `status === 'error'` and
|
|
44
|
+
* `error`. A refetch after an error is one more subscription (a re-mount
|
|
45
|
+
* or a key change) or a `client.invalidate()`.
|
|
46
|
+
*
|
|
47
|
+
* @module
|
|
48
|
+
*/
|
|
49
|
+
export * from './usePrefetch.js';
|
|
50
|
+
export * from './useQuery.js';
|
|
51
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+CG;AAEH,cAAc,kBAAkB,CAAA;AAChC,cAAc,eAAe,CAAA"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* React hooks for `@molecule/app-query`.
|
|
3
|
+
*
|
|
4
|
+
* {@link useQuery} observes one document from the app's shared cache and
|
|
5
|
+
* returns cached data on the first render; {@link usePrefetch} and
|
|
6
|
+
* {@link intentPrefetchProps} warm a document when a person is about to
|
|
7
|
+
* open it. Together they make a detail page open with no loading state.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```tsx
|
|
11
|
+
* import { intentPrefetchProps, useQuery } from '@molecule/app-query-react'
|
|
12
|
+
*
|
|
13
|
+
* const packageQuery = (name: string) => ({
|
|
14
|
+
* key: ['package', name],
|
|
15
|
+
* fetch: (signal: AbortSignal) =>
|
|
16
|
+
* fetch(`/data/packages/${name}.json`, { signal }).then((r) => r.json()),
|
|
17
|
+
* })
|
|
18
|
+
*
|
|
19
|
+
* function PackageLink({ name }: { name: string }) {
|
|
20
|
+
* return <Link to={`/packages/${name}`} {...intentPrefetchProps(packageQuery(name))}>{name}</Link>
|
|
21
|
+
* }
|
|
22
|
+
*
|
|
23
|
+
* function PackagePage({ name }: { name: string }) {
|
|
24
|
+
* const { data, status } = useQuery(packageQuery(name))
|
|
25
|
+
* if (!data) return status === 'error' ? <NotFound /> : <Skeleton />
|
|
26
|
+
* return <Article doc={data} />
|
|
27
|
+
* }
|
|
28
|
+
* ```
|
|
29
|
+
*
|
|
30
|
+
* @remarks
|
|
31
|
+
* - **`useQuery` reads the cache synchronously on the first render.** A
|
|
32
|
+
* document prefetched from a hovered link, or seeded with `client.set()`
|
|
33
|
+
* from a server-rendered JSON island, renders at once — no spinner, no
|
|
34
|
+
* layout shift. Render a loading state only when `data` is `undefined`.
|
|
35
|
+
* - **The key is the identity.** Building `options` inline is fine; the
|
|
36
|
+
* hook resubscribes only when the key's value changes, not when the
|
|
37
|
+
* `fetch` closure does.
|
|
38
|
+
* - `intentPrefetchProps` spreads three handlers onto a link. It is a plain
|
|
39
|
+
* function (not a hook), so it works inside `.map()` too.
|
|
40
|
+
* - Prefetching respects the browser's data-saver setting and skips 2G
|
|
41
|
+
* connections (`shouldPrefetch()` in the core) — do not bypass that for
|
|
42
|
+
* "important" links; the person can still click.
|
|
43
|
+
* - Errors are not thrown from the hook; read `status === 'error'` and
|
|
44
|
+
* `error`. A refetch after an error is one more subscription (a re-mount
|
|
45
|
+
* or a key change) or a `client.invalidate()`.
|
|
46
|
+
*
|
|
47
|
+
* @module
|
|
48
|
+
*/
|
|
49
|
+
export * from './usePrefetch.js';
|
|
50
|
+
export * from './useQuery.js';
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Warm the cache ahead of a navigation.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
import type { QueryOptions } from '@molecule/app-query';
|
|
7
|
+
/**
|
|
8
|
+
* Warms the cache for a document, when the connection can afford it (see
|
|
9
|
+
* `shouldPrefetch()` in the core). Safe to call repeatedly: a fresh or
|
|
10
|
+
* in-flight document is skipped, and failures are swallowed.
|
|
11
|
+
*
|
|
12
|
+
* @param options - Key, fetch, freshness.
|
|
13
|
+
*/
|
|
14
|
+
export declare function prefetchQuery<T>(options: QueryOptions<T>): void;
|
|
15
|
+
/**
|
|
16
|
+
* A stable `prefetch(options)` function for event handlers.
|
|
17
|
+
*
|
|
18
|
+
* @returns The prefetch function.
|
|
19
|
+
*/
|
|
20
|
+
export declare function usePrefetch(): <T>(options: QueryOptions<T>) => void;
|
|
21
|
+
/** The handlers {@link intentPrefetchProps} returns. */
|
|
22
|
+
export interface IntentPrefetchProps {
|
|
23
|
+
onPointerEnter: () => void;
|
|
24
|
+
onFocus: () => void;
|
|
25
|
+
onTouchStart: () => void;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Event handlers that warm a document at the first sign of intent: the
|
|
29
|
+
* pointer arriving, keyboard focus, or a touch starting. Spread them onto a
|
|
30
|
+
* link: `<Link {...intentPrefetchProps(packageQuery(name))} />`.
|
|
31
|
+
*
|
|
32
|
+
* @param options - Key, fetch, freshness.
|
|
33
|
+
* @returns The handlers.
|
|
34
|
+
*/
|
|
35
|
+
export declare function intentPrefetchProps<T>(options: QueryOptions<T>): IntentPrefetchProps;
|
|
36
|
+
//# sourceMappingURL=usePrefetch.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"usePrefetch.d.ts","sourceRoot":"","sources":["../src/usePrefetch.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAIH,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAA;AAGvD;;;;;;GAMG;AACH,wBAAgB,aAAa,CAAC,CAAC,EAAE,OAAO,EAAE,YAAY,CAAC,CAAC,CAAC,GAAG,IAAI,CAG/D;AAED;;;;GAIG;AACH,wBAAgB,WAAW,IAAI,CAAC,CAAC,EAAE,OAAO,EAAE,YAAY,CAAC,CAAC,CAAC,KAAK,IAAI,CAEnE;AAED,wDAAwD;AACxD,MAAM,WAAW,mBAAmB;IAClC,cAAc,EAAE,MAAM,IAAI,CAAA;IAC1B,OAAO,EAAE,MAAM,IAAI,CAAA;IACnB,YAAY,EAAE,MAAM,IAAI,CAAA;CACzB;AAED;;;;;;;GAOG;AACH,wBAAgB,mBAAmB,CAAC,CAAC,EAAE,OAAO,EAAE,YAAY,CAAC,CAAC,CAAC,GAAG,mBAAmB,CAGpF"}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Warm the cache ahead of a navigation.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
import { useCallback } from 'react';
|
|
7
|
+
import { getQueryClient, shouldPrefetch } from '@molecule/app-query';
|
|
8
|
+
/**
|
|
9
|
+
* Warms the cache for a document, when the connection can afford it (see
|
|
10
|
+
* `shouldPrefetch()` in the core). Safe to call repeatedly: a fresh or
|
|
11
|
+
* in-flight document is skipped, and failures are swallowed.
|
|
12
|
+
*
|
|
13
|
+
* @param options - Key, fetch, freshness.
|
|
14
|
+
*/
|
|
15
|
+
export function prefetchQuery(options) {
|
|
16
|
+
if (!shouldPrefetch())
|
|
17
|
+
return;
|
|
18
|
+
getQueryClient().prefetch(options);
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* A stable `prefetch(options)` function for event handlers.
|
|
22
|
+
*
|
|
23
|
+
* @returns The prefetch function.
|
|
24
|
+
*/
|
|
25
|
+
export function usePrefetch() {
|
|
26
|
+
return useCallback((options) => prefetchQuery(options), []);
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Event handlers that warm a document at the first sign of intent: the
|
|
30
|
+
* pointer arriving, keyboard focus, or a touch starting. Spread them onto a
|
|
31
|
+
* link: `<Link {...intentPrefetchProps(packageQuery(name))} />`.
|
|
32
|
+
*
|
|
33
|
+
* @param options - Key, fetch, freshness.
|
|
34
|
+
* @returns The handlers.
|
|
35
|
+
*/
|
|
36
|
+
export function intentPrefetchProps(options) {
|
|
37
|
+
const go = () => prefetchQuery(options);
|
|
38
|
+
return { onPointerEnter: go, onFocus: go, onTouchStart: go };
|
|
39
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Observe one document from the shared cache.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
import type { QueryOptions, QueryState } from '@molecule/app-query';
|
|
7
|
+
/**
|
|
8
|
+
* The document for `options.key`, kept current: cached data is returned on
|
|
9
|
+
* the very first render (so a warmed page never flashes a loading state),
|
|
10
|
+
* a stale or missing document is fetched, and every change re-renders.
|
|
11
|
+
* Pass `null` to observe nothing. A key change resets to that key's state;
|
|
12
|
+
* the hook never hands a caller another key's document.
|
|
13
|
+
*
|
|
14
|
+
* @param options - Key, fetch, freshness; or `null`.
|
|
15
|
+
* @returns The document's state.
|
|
16
|
+
*/
|
|
17
|
+
export declare function useQuery<T>(options: QueryOptions<T> | null): QueryState<T>;
|
|
18
|
+
//# sourceMappingURL=useQuery.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useQuery.d.ts","sourceRoot":"","sources":["../src/useQuery.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAIH,OAAO,KAAK,EAAE,YAAY,EAAE,UAAU,EAAE,MAAM,qBAAqB,CAAA;AAWnE;;;;;;;;;GASG;AACH,wBAAgB,QAAQ,CAAC,CAAC,EAAE,OAAO,EAAE,YAAY,CAAC,CAAC,CAAC,GAAG,IAAI,GAAG,UAAU,CAAC,CAAC,CAAC,CAqB1E"}
|
package/dist/useQuery.js
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Observe one document from the shared cache.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
import { useEffect, useMemo, useState } from 'react';
|
|
7
|
+
import { getQueryClient, hashQueryKey } from '@molecule/app-query';
|
|
8
|
+
const IDLE = {
|
|
9
|
+
data: undefined,
|
|
10
|
+
status: 'idle',
|
|
11
|
+
error: undefined,
|
|
12
|
+
isFetching: false,
|
|
13
|
+
updatedAt: undefined,
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* The document for `options.key`, kept current: cached data is returned on
|
|
17
|
+
* the very first render (so a warmed page never flashes a loading state),
|
|
18
|
+
* a stale or missing document is fetched, and every change re-renders.
|
|
19
|
+
* Pass `null` to observe nothing. A key change resets to that key's state;
|
|
20
|
+
* the hook never hands a caller another key's document.
|
|
21
|
+
*
|
|
22
|
+
* @param options - Key, fetch, freshness; or `null`.
|
|
23
|
+
* @returns The document's state.
|
|
24
|
+
*/
|
|
25
|
+
export function useQuery(options) {
|
|
26
|
+
const hash = options ? hashQueryKey(options.key) : '';
|
|
27
|
+
// Options are read once per key: a fresh `fetch` closure every render must
|
|
28
|
+
// not resubscribe.
|
|
29
|
+
const stable = useMemo(() => options, [hash]);
|
|
30
|
+
const [state, setState] = useState(() => ({
|
|
31
|
+
hash,
|
|
32
|
+
value: stable ? getQueryClient().getState(stable.key) : IDLE,
|
|
33
|
+
}));
|
|
34
|
+
useEffect(() => {
|
|
35
|
+
if (!stable) {
|
|
36
|
+
setState({ hash: '', value: IDLE });
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
const client = getQueryClient();
|
|
40
|
+
return client.subscribe(stable, (value) => setState({ hash, value }));
|
|
41
|
+
}, [stable, hash]);
|
|
42
|
+
if (state.hash === hash)
|
|
43
|
+
return state.value;
|
|
44
|
+
return stable ? getQueryClient().getState(stable.key) : IDLE;
|
|
45
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@molecule/app-query-react",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "React hooks for @molecule/app-query — read a cached document synchronously, observe it, and prefetch on intent",
|
|
5
|
+
"homepage": "https://www.molecule.dev/packages/app-query-react",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"main": "dist/index.js",
|
|
8
|
+
"types": "dist/index.d.ts",
|
|
9
|
+
"scripts": {
|
|
10
|
+
"build": "tsc",
|
|
11
|
+
"test": "vitest run",
|
|
12
|
+
"test:watch": "vitest"
|
|
13
|
+
},
|
|
14
|
+
"exports": {
|
|
15
|
+
".": {
|
|
16
|
+
"types": "./dist/index.d.ts",
|
|
17
|
+
"import": "./dist/index.js"
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
"files": [
|
|
21
|
+
"dist",
|
|
22
|
+
"README.md"
|
|
23
|
+
],
|
|
24
|
+
"keywords": [
|
|
25
|
+
"molecule",
|
|
26
|
+
"query",
|
|
27
|
+
"cache",
|
|
28
|
+
"prefetch",
|
|
29
|
+
"react",
|
|
30
|
+
"hooks"
|
|
31
|
+
],
|
|
32
|
+
"license": "Apache-2.0",
|
|
33
|
+
"repository": {
|
|
34
|
+
"type": "git",
|
|
35
|
+
"url": "https://github.com/molecule-dev/molecule.git",
|
|
36
|
+
"directory": "packages/app/features/query-react"
|
|
37
|
+
},
|
|
38
|
+
"peerDependencies": {
|
|
39
|
+
"@molecule/app-query": "^1.0.0",
|
|
40
|
+
"react": "^18.0.0 || ^19.0.0"
|
|
41
|
+
},
|
|
42
|
+
"devDependencies": {
|
|
43
|
+
"@molecule/app-query": "1.0.0",
|
|
44
|
+
"@molecule/app-query-memory": "1.0.0",
|
|
45
|
+
"@types/node": "26.1.2",
|
|
46
|
+
"@types/react": "19.2.17",
|
|
47
|
+
"@types/react-dom": "19.2.3",
|
|
48
|
+
"jsdom": "30.0.1",
|
|
49
|
+
"react": "19.2.8",
|
|
50
|
+
"react-dom": "19.2.8",
|
|
51
|
+
"typescript": "6.0.3",
|
|
52
|
+
"vitest": "4.1.11"
|
|
53
|
+
},
|
|
54
|
+
"publishConfig": {
|
|
55
|
+
"access": "public"
|
|
56
|
+
}
|
|
57
|
+
}
|