@molecule/app-query-memory 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 +134 -0
- package/dist/index.d.ts +33 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +32 -0
- package/dist/provider.d.ts +18 -0
- package/dist/provider.d.ts.map +1 -0
- package/dist/provider.js +268 -0
- package/dist/types.d.ts +15 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +6 -0
- package/package.json +48 -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,134 @@
|
|
|
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:01.977Z
|
|
7
|
+
-->
|
|
8
|
+
|
|
9
|
+
# @molecule/app-query-memory
|
|
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
|
+
In-memory bond for `@molecule/app-query`.
|
|
16
|
+
|
|
17
|
+
A Map of documents with freshness, shared in-flight fetches, observers and
|
|
18
|
+
garbage collection, and nothing else: no dependency, a few kilobytes. The
|
|
19
|
+
right bond when the app has no other reason to ship a query library; the
|
|
20
|
+
`@molecule/app-query-tanstack` bond is the same contract over TanStack Query
|
|
21
|
+
for apps that already use it or want its devtools.
|
|
22
|
+
|
|
23
|
+
## Quick Start
|
|
24
|
+
|
|
25
|
+
```typescript
|
|
26
|
+
import { getQueryClient, setProvider } from '@molecule/app-query'
|
|
27
|
+
import { provider } from '@molecule/app-query-memory'
|
|
28
|
+
|
|
29
|
+
setProvider(provider)
|
|
30
|
+
const client = getQueryClient()
|
|
31
|
+
await client.fetch({ key: ['profile'], fetch: (signal) => api.get('/me', { signal }) })
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## Type
|
|
35
|
+
|
|
36
|
+
`provider`
|
|
37
|
+
|
|
38
|
+
## Installation
|
|
39
|
+
|
|
40
|
+
```bash
|
|
41
|
+
npm install @molecule/app-query-memory @molecule/app-query
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
## API
|
|
45
|
+
|
|
46
|
+
### Interfaces
|
|
47
|
+
|
|
48
|
+
#### `MemoryQueryConfig`
|
|
49
|
+
|
|
50
|
+
Provider-specific configuration options. The in-memory bond needs none
|
|
51
|
+
beyond the core's `QueryClientConfig`; the type exists so `createProvider`
|
|
52
|
+
has the same shape as every other bond's.
|
|
53
|
+
|
|
54
|
+
```typescript
|
|
55
|
+
interface MemoryQueryConfig {
|
|
56
|
+
/** Reserved. */
|
|
57
|
+
readonly _?: never
|
|
58
|
+
}
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
### Functions
|
|
62
|
+
|
|
63
|
+
#### `createProvider(_config)`
|
|
64
|
+
|
|
65
|
+
Creates an in-memory query provider.
|
|
66
|
+
|
|
67
|
+
```typescript
|
|
68
|
+
function createProvider(_config?: MemoryQueryConfig): QueryProvider
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
- `_config` — Provider configuration (none today).
|
|
72
|
+
|
|
73
|
+
**Returns:** A query provider.
|
|
74
|
+
|
|
75
|
+
### Constants
|
|
76
|
+
|
|
77
|
+
#### `provider`
|
|
78
|
+
|
|
79
|
+
Default in-memory provider instance.
|
|
80
|
+
|
|
81
|
+
```typescript
|
|
82
|
+
const provider: QueryProvider
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
## Core Interface
|
|
86
|
+
|
|
87
|
+
Implements `@molecule/app-query` interface.
|
|
88
|
+
|
|
89
|
+
## Bond Wiring
|
|
90
|
+
|
|
91
|
+
Setup function to register this provider with the core interface:
|
|
92
|
+
|
|
93
|
+
```typescript
|
|
94
|
+
import { setProvider } from '@molecule/app-query'
|
|
95
|
+
import { provider } from '@molecule/app-query-memory'
|
|
96
|
+
|
|
97
|
+
export function setupQueryMemory(): void {
|
|
98
|
+
setProvider(provider)
|
|
99
|
+
}
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
## Injection Notes
|
|
103
|
+
|
|
104
|
+
### Requirements
|
|
105
|
+
|
|
106
|
+
Peer dependencies:
|
|
107
|
+
|
|
108
|
+
- `@molecule/app-query` ^1.0.0
|
|
109
|
+
|
|
110
|
+
### Runtime Dependencies
|
|
111
|
+
|
|
112
|
+
- `@molecule/app-query`
|
|
113
|
+
|
|
114
|
+
- Documents are dropped `gcMs` after their last observer leaves (default 30
|
|
115
|
+
minutes) — a cache, not a store: nothing survives a reload.
|
|
116
|
+
- `subscribe()` refetches a stale document while still emitting the stale
|
|
117
|
+
data first, so a component paints at once and updates when the fresh copy
|
|
118
|
+
lands.
|
|
119
|
+
- Swapping to `@molecule/app-query-tanstack` changes no application code:
|
|
120
|
+
both pass the same behavioural suite.
|
|
121
|
+
|
|
122
|
+
## E2E Tests
|
|
123
|
+
|
|
124
|
+
Integration checklist — drive the real UI (live preview, no mocks), adapt
|
|
125
|
+
each item to this app's actual screens/flows, and check every box off one
|
|
126
|
+
by one. A box you can't check is an integration bug to fix — not a skip:
|
|
127
|
+
|
|
128
|
+
- [ ] Opening a detail page after hovering its link shows no loading
|
|
129
|
+
state (the document was warmed) and the page is the right one.
|
|
130
|
+
- [ ] Opening the same page twice fetches it once (check the network panel).
|
|
131
|
+
- [ ] Going back to a page painted earlier paints at once, from memory.
|
|
132
|
+
- [ ] After the app invalidates a key (an edit, a refresh action), the
|
|
133
|
+
next view shows the new data.
|
|
134
|
+
- [ ] With the browser's data-saver on, hovering links fetches nothing.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* In-memory bond for `@molecule/app-query`.
|
|
3
|
+
*
|
|
4
|
+
* A Map of documents with freshness, shared in-flight fetches, observers and
|
|
5
|
+
* garbage collection, and nothing else: no dependency, a few kilobytes. The
|
|
6
|
+
* right bond when the app has no other reason to ship a query library; the
|
|
7
|
+
* `@molecule/app-query-tanstack` bond is the same contract over TanStack Query
|
|
8
|
+
* for apps that already use it or want its devtools.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```typescript
|
|
12
|
+
* import { getQueryClient, setProvider } from '@molecule/app-query'
|
|
13
|
+
* import { provider } from '@molecule/app-query-memory'
|
|
14
|
+
*
|
|
15
|
+
* setProvider(provider)
|
|
16
|
+
* const client = getQueryClient()
|
|
17
|
+
* await client.fetch({ key: ['profile'], fetch: (signal) => api.get('/me', { signal }) })
|
|
18
|
+
* ```
|
|
19
|
+
*
|
|
20
|
+
* @remarks
|
|
21
|
+
* - Documents are dropped `gcMs` after their last observer leaves (default 30
|
|
22
|
+
* minutes) — a cache, not a store: nothing survives a reload.
|
|
23
|
+
* - `subscribe()` refetches a stale document while still emitting the stale
|
|
24
|
+
* data first, so a component paints at once and updates when the fresh copy
|
|
25
|
+
* lands.
|
|
26
|
+
* - Swapping to `@molecule/app-query-tanstack` changes no application code:
|
|
27
|
+
* both pass the same behavioural suite.
|
|
28
|
+
*
|
|
29
|
+
* @module
|
|
30
|
+
*/
|
|
31
|
+
export * from './provider.js';
|
|
32
|
+
export * from './types.js';
|
|
33
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AAEH,cAAc,eAAe,CAAA;AAC7B,cAAc,YAAY,CAAA"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* In-memory bond for `@molecule/app-query`.
|
|
3
|
+
*
|
|
4
|
+
* A Map of documents with freshness, shared in-flight fetches, observers and
|
|
5
|
+
* garbage collection, and nothing else: no dependency, a few kilobytes. The
|
|
6
|
+
* right bond when the app has no other reason to ship a query library; the
|
|
7
|
+
* `@molecule/app-query-tanstack` bond is the same contract over TanStack Query
|
|
8
|
+
* for apps that already use it or want its devtools.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```typescript
|
|
12
|
+
* import { getQueryClient, setProvider } from '@molecule/app-query'
|
|
13
|
+
* import { provider } from '@molecule/app-query-memory'
|
|
14
|
+
*
|
|
15
|
+
* setProvider(provider)
|
|
16
|
+
* const client = getQueryClient()
|
|
17
|
+
* await client.fetch({ key: ['profile'], fetch: (signal) => api.get('/me', { signal }) })
|
|
18
|
+
* ```
|
|
19
|
+
*
|
|
20
|
+
* @remarks
|
|
21
|
+
* - Documents are dropped `gcMs` after their last observer leaves (default 30
|
|
22
|
+
* minutes) — a cache, not a store: nothing survives a reload.
|
|
23
|
+
* - `subscribe()` refetches a stale document while still emitting the stale
|
|
24
|
+
* data first, so a component paints at once and updates when the fresh copy
|
|
25
|
+
* lands.
|
|
26
|
+
* - Swapping to `@molecule/app-query-tanstack` changes no application code:
|
|
27
|
+
* both pass the same behavioural suite.
|
|
28
|
+
*
|
|
29
|
+
* @module
|
|
30
|
+
*/
|
|
31
|
+
export * from './provider.js';
|
|
32
|
+
export * from './types.js';
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* In-memory query client: a Map of documents with freshness, in-flight
|
|
3
|
+
* sharing, observers and garbage collection.
|
|
4
|
+
*
|
|
5
|
+
* @module
|
|
6
|
+
*/
|
|
7
|
+
import type { QueryProvider } from '@molecule/app-query';
|
|
8
|
+
import type { MemoryQueryConfig } from './types.js';
|
|
9
|
+
/**
|
|
10
|
+
* Creates an in-memory query provider.
|
|
11
|
+
*
|
|
12
|
+
* @param _config - Provider configuration (none today).
|
|
13
|
+
* @returns A query provider.
|
|
14
|
+
*/
|
|
15
|
+
export declare function createProvider(_config?: MemoryQueryConfig): QueryProvider;
|
|
16
|
+
/** Default in-memory provider instance. */
|
|
17
|
+
export declare const provider: QueryProvider;
|
|
18
|
+
//# sourceMappingURL=provider.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"provider.d.ts","sourceRoot":"","sources":["../src/provider.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EAKV,aAAa,EAEd,MAAM,qBAAqB,CAAA;AAG5B,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAA;AA0QnD;;;;;GAKG;AACH,wBAAgB,cAAc,CAAC,OAAO,GAAE,iBAAsB,GAAG,aAAa,CAO7E;AAED,2CAA2C;AAC3C,eAAO,MAAM,QAAQ,EAAE,aAAgC,CAAA"}
|
package/dist/provider.js
ADDED
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* In-memory query client: a Map of documents with freshness, in-flight
|
|
3
|
+
* sharing, observers and garbage collection.
|
|
4
|
+
*
|
|
5
|
+
* @module
|
|
6
|
+
*/
|
|
7
|
+
import { hashQueryKey, keyStartsWith } from '@molecule/app-query';
|
|
8
|
+
const DEFAULT_STALE_MS = 5 * 60 * 1000;
|
|
9
|
+
const DEFAULT_GC_MS = 30 * 60 * 1000;
|
|
10
|
+
/**
|
|
11
|
+
* One cache.
|
|
12
|
+
*/
|
|
13
|
+
class MemoryQueryClient {
|
|
14
|
+
entries = new Map();
|
|
15
|
+
staleMs;
|
|
16
|
+
gcMs;
|
|
17
|
+
/**
|
|
18
|
+
* Creates the cache.
|
|
19
|
+
*
|
|
20
|
+
* @param config - Defaults for every query.
|
|
21
|
+
*/
|
|
22
|
+
constructor(config = {}) {
|
|
23
|
+
this.staleMs = config.staleMs ?? DEFAULT_STALE_MS;
|
|
24
|
+
this.gcMs = config.gcMs ?? DEFAULT_GC_MS;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* The entry for a key, created idle when unknown.
|
|
28
|
+
*
|
|
29
|
+
* @param key - The document's key.
|
|
30
|
+
* @param gcMs - Its collection delay, when first seen.
|
|
31
|
+
* @returns The entry.
|
|
32
|
+
*/
|
|
33
|
+
entry(key, gcMs) {
|
|
34
|
+
const hash = hashQueryKey(key);
|
|
35
|
+
let entry = this.entries.get(hash);
|
|
36
|
+
if (!entry) {
|
|
37
|
+
entry = {
|
|
38
|
+
key,
|
|
39
|
+
state: {
|
|
40
|
+
data: undefined,
|
|
41
|
+
status: 'idle',
|
|
42
|
+
error: undefined,
|
|
43
|
+
isFetching: false,
|
|
44
|
+
updatedAt: undefined,
|
|
45
|
+
},
|
|
46
|
+
stale: true,
|
|
47
|
+
gcMs: gcMs ?? this.gcMs,
|
|
48
|
+
gcTimer: null,
|
|
49
|
+
inflight: null,
|
|
50
|
+
listeners: new Set(),
|
|
51
|
+
};
|
|
52
|
+
this.entries.set(hash, entry);
|
|
53
|
+
}
|
|
54
|
+
return entry;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Tells every observer of an entry its current state.
|
|
58
|
+
*
|
|
59
|
+
* @param entry - The entry.
|
|
60
|
+
*/
|
|
61
|
+
emit(entry) {
|
|
62
|
+
for (const listener of entry.listeners)
|
|
63
|
+
listener(entry.state);
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Whether an entry holds data younger than `staleMs` and not invalidated.
|
|
67
|
+
*
|
|
68
|
+
* @param entry - The entry.
|
|
69
|
+
* @param staleMs - The freshness window.
|
|
70
|
+
* @returns `true` when fresh.
|
|
71
|
+
*/
|
|
72
|
+
isFresh(entry, staleMs) {
|
|
73
|
+
if (entry.stale || entry.state.updatedAt === undefined || entry.state.data === undefined)
|
|
74
|
+
return false;
|
|
75
|
+
return Date.now() - entry.state.updatedAt < staleMs;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Drops the entry after its collection delay unless something uses it by then.
|
|
79
|
+
*
|
|
80
|
+
* @param entry - The entry.
|
|
81
|
+
*/
|
|
82
|
+
scheduleGc(entry) {
|
|
83
|
+
if (entry.gcTimer)
|
|
84
|
+
clearTimeout(entry.gcTimer);
|
|
85
|
+
entry.gcTimer = setTimeout(() => {
|
|
86
|
+
if (entry.listeners.size || entry.inflight)
|
|
87
|
+
return;
|
|
88
|
+
this.entries.delete(hashQueryKey(entry.key));
|
|
89
|
+
}, entry.gcMs);
|
|
90
|
+
entry.gcTimer.unref?.();
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Starts (or joins) the entry's fetch.
|
|
94
|
+
*
|
|
95
|
+
* @param entry - The entry.
|
|
96
|
+
* @param options - Key, fetch, freshness.
|
|
97
|
+
* @returns The document.
|
|
98
|
+
*/
|
|
99
|
+
start(entry, options) {
|
|
100
|
+
if (entry.inflight)
|
|
101
|
+
return entry.inflight.promise;
|
|
102
|
+
const controller = new AbortController();
|
|
103
|
+
entry.state = {
|
|
104
|
+
...entry.state,
|
|
105
|
+
isFetching: true,
|
|
106
|
+
status: entry.state.data === undefined ? 'loading' : entry.state.status,
|
|
107
|
+
};
|
|
108
|
+
this.emit(entry);
|
|
109
|
+
const promise = options
|
|
110
|
+
.fetch(controller.signal)
|
|
111
|
+
.then((data) => {
|
|
112
|
+
entry.inflight = null;
|
|
113
|
+
entry.stale = false;
|
|
114
|
+
entry.state = {
|
|
115
|
+
data,
|
|
116
|
+
status: 'success',
|
|
117
|
+
error: undefined,
|
|
118
|
+
isFetching: false,
|
|
119
|
+
updatedAt: Date.now(),
|
|
120
|
+
};
|
|
121
|
+
this.emit(entry);
|
|
122
|
+
this.scheduleGc(entry);
|
|
123
|
+
return data;
|
|
124
|
+
})
|
|
125
|
+
.catch((error) => {
|
|
126
|
+
entry.inflight = null;
|
|
127
|
+
entry.state = { ...entry.state, status: 'error', error, isFetching: false };
|
|
128
|
+
this.emit(entry);
|
|
129
|
+
this.scheduleGc(entry);
|
|
130
|
+
throw error;
|
|
131
|
+
});
|
|
132
|
+
entry.inflight = { promise, controller };
|
|
133
|
+
return promise;
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* The cached document, synchronously, fresh or stale.
|
|
137
|
+
*
|
|
138
|
+
* @param key - The document's key.
|
|
139
|
+
* @returns The document, or `undefined`.
|
|
140
|
+
*/
|
|
141
|
+
get(key) {
|
|
142
|
+
return this.entries.get(hashQueryKey(key))?.state.data;
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* The document's full state, synchronously.
|
|
146
|
+
*
|
|
147
|
+
* @param key - The document's key.
|
|
148
|
+
* @returns The state; `idle` for an unknown key.
|
|
149
|
+
*/
|
|
150
|
+
getState(key) {
|
|
151
|
+
const entry = this.entries.get(hashQueryKey(key));
|
|
152
|
+
return entry
|
|
153
|
+
? entry.state
|
|
154
|
+
: {
|
|
155
|
+
data: undefined,
|
|
156
|
+
status: 'idle',
|
|
157
|
+
error: undefined,
|
|
158
|
+
isFetching: false,
|
|
159
|
+
updatedAt: undefined,
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* The document: cached-and-fresh at once, a fetch in flight shared, otherwise fetched.
|
|
164
|
+
*
|
|
165
|
+
* @param options - Key, fetch, freshness.
|
|
166
|
+
* @returns The document.
|
|
167
|
+
*/
|
|
168
|
+
fetch(options) {
|
|
169
|
+
const entry = this.entry(options.key, options.gcMs);
|
|
170
|
+
if (this.isFresh(entry, options.staleMs ?? this.staleMs))
|
|
171
|
+
return Promise.resolve(entry.state.data);
|
|
172
|
+
return this.start(entry, options);
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Warms the cache; never throws; skips a fresh or in-flight document.
|
|
176
|
+
*
|
|
177
|
+
* @param options - Key, fetch, freshness.
|
|
178
|
+
*/
|
|
179
|
+
prefetch(options) {
|
|
180
|
+
const entry = this.entry(options.key, options.gcMs);
|
|
181
|
+
if (entry.inflight || this.isFresh(entry, options.staleMs ?? this.staleMs))
|
|
182
|
+
return;
|
|
183
|
+
this.start(entry, options).catch(() => {
|
|
184
|
+
// Speculative: the real navigation reports a document that will not load.
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Seeds the cache.
|
|
189
|
+
*
|
|
190
|
+
* @param key - The document's key.
|
|
191
|
+
* @param data - The document.
|
|
192
|
+
*/
|
|
193
|
+
set(key, data) {
|
|
194
|
+
const entry = this.entry(key);
|
|
195
|
+
entry.stale = false;
|
|
196
|
+
entry.state = {
|
|
197
|
+
data,
|
|
198
|
+
status: 'success',
|
|
199
|
+
error: undefined,
|
|
200
|
+
isFetching: entry.state.isFetching,
|
|
201
|
+
updatedAt: Date.now(),
|
|
202
|
+
};
|
|
203
|
+
this.emit(entry);
|
|
204
|
+
this.scheduleGc(entry);
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* Marks the key and everything it prefixes as stale.
|
|
208
|
+
*
|
|
209
|
+
* @param key - A key or a key prefix.
|
|
210
|
+
*/
|
|
211
|
+
invalidate(key) {
|
|
212
|
+
for (const entry of this.entries.values()) {
|
|
213
|
+
if (keyStartsWith(entry.key, key))
|
|
214
|
+
entry.stale = true;
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
/**
|
|
218
|
+
* Observes a document, fetching it when absent or stale.
|
|
219
|
+
*
|
|
220
|
+
* @param options - Key, fetch, freshness.
|
|
221
|
+
* @param listener - Called with each state.
|
|
222
|
+
* @returns Stops observing.
|
|
223
|
+
*/
|
|
224
|
+
subscribe(options, listener) {
|
|
225
|
+
const entry = this.entry(options.key, options.gcMs);
|
|
226
|
+
entry.listeners.add(listener);
|
|
227
|
+
if (entry.gcTimer) {
|
|
228
|
+
clearTimeout(entry.gcTimer);
|
|
229
|
+
entry.gcTimer = null;
|
|
230
|
+
}
|
|
231
|
+
listener(entry.state);
|
|
232
|
+
if (!entry.inflight && !this.isFresh(entry, options.staleMs ?? this.staleMs)) {
|
|
233
|
+
this.start(entry, options).catch(() => {
|
|
234
|
+
// The state carries the error; observers read it from there.
|
|
235
|
+
});
|
|
236
|
+
}
|
|
237
|
+
return () => {
|
|
238
|
+
entry.listeners.delete(listener);
|
|
239
|
+
if (!entry.listeners.size)
|
|
240
|
+
this.scheduleGc(entry);
|
|
241
|
+
};
|
|
242
|
+
}
|
|
243
|
+
/** Drops every document and every in-flight fetch. */
|
|
244
|
+
clear() {
|
|
245
|
+
for (const entry of this.entries.values()) {
|
|
246
|
+
entry.inflight?.controller.abort();
|
|
247
|
+
if (entry.gcTimer)
|
|
248
|
+
clearTimeout(entry.gcTimer);
|
|
249
|
+
}
|
|
250
|
+
this.entries.clear();
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
/**
|
|
254
|
+
* Creates an in-memory query provider.
|
|
255
|
+
*
|
|
256
|
+
* @param _config - Provider configuration (none today).
|
|
257
|
+
* @returns A query provider.
|
|
258
|
+
*/
|
|
259
|
+
export function createProvider(_config = {}) {
|
|
260
|
+
return {
|
|
261
|
+
name: 'memory',
|
|
262
|
+
createClient(config) {
|
|
263
|
+
return new MemoryQueryClient(config);
|
|
264
|
+
},
|
|
265
|
+
};
|
|
266
|
+
}
|
|
267
|
+
/** Default in-memory provider instance. */
|
|
268
|
+
export const provider = createProvider();
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Configuration for the in-memory provider.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* Provider-specific configuration options. The in-memory bond needs none
|
|
8
|
+
* beyond the core's `QueryClientConfig`; the type exists so `createProvider`
|
|
9
|
+
* has the same shape as every other bond's.
|
|
10
|
+
*/
|
|
11
|
+
export interface MemoryQueryConfig {
|
|
12
|
+
/** Reserved. */
|
|
13
|
+
readonly _?: never;
|
|
14
|
+
}
|
|
15
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH;;;;GAIG;AACH,MAAM,WAAW,iBAAiB;IAChC,gBAAgB;IAChB,QAAQ,CAAC,CAAC,CAAC,EAAE,KAAK,CAAA;CACnB"}
|
package/dist/types.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@molecule/app-query-memory",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Dependency-free in-memory bond for @molecule/app-query — a Map of documents with freshness, in-flight sharing and observers",
|
|
5
|
+
"homepage": "https://www.molecule.dev/packages/app-query-memory",
|
|
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
|
+
"memory"
|
|
29
|
+
],
|
|
30
|
+
"license": "Apache-2.0",
|
|
31
|
+
"repository": {
|
|
32
|
+
"type": "git",
|
|
33
|
+
"url": "https://github.com/molecule-dev/molecule.git",
|
|
34
|
+
"directory": "packages/app/bonds/query/memory"
|
|
35
|
+
},
|
|
36
|
+
"peerDependencies": {
|
|
37
|
+
"@molecule/app-query": "^1.0.0"
|
|
38
|
+
},
|
|
39
|
+
"devDependencies": {
|
|
40
|
+
"@molecule/app-query": "1.0.0",
|
|
41
|
+
"@types/node": "26.1.2",
|
|
42
|
+
"typescript": "6.0.3",
|
|
43
|
+
"vitest": "4.1.11"
|
|
44
|
+
},
|
|
45
|
+
"publishConfig": {
|
|
46
|
+
"access": "public"
|
|
47
|
+
}
|
|
48
|
+
}
|