@molecule/app-query-react 1.4.0 → 1.5.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/README.md +14 -2
- package/dist/index.d.ts +9 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +9 -0
- package/dist/resource.d.ts +4 -2
- package/dist/resource.d.ts.map +1 -1
- package/dist/resource.js +53 -14
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -3,7 +3,7 @@ AUTO-GENERATED — DO NOT EDIT THIS FILE.
|
|
|
3
3
|
Generated by `mlcl sync-docs` from the package's src/index.ts JSDoc + mlcl/registry.json.
|
|
4
4
|
Edits here are overwritten on the next commit (molecule's pre-commit hook regenerates).
|
|
5
5
|
To change this document, edit the module-level JSDoc in src/index.ts.
|
|
6
|
-
Generated: 2026-11-
|
|
6
|
+
Generated: 2026-11-13T01:18:38.398Z
|
|
7
7
|
-->
|
|
8
8
|
|
|
9
9
|
# @molecule/app-query-react
|
|
@@ -302,10 +302,19 @@ const page = tickets.listPage(
|
|
|
302
302
|
// and tickets.refresh('agent-1') refresh this page too.
|
|
303
303
|
// A second list can use tickets.listPage(viewer, params, readPage, '/tickets/open').
|
|
304
304
|
// Use one canonical page reader per path; list() still returns only its own rows.
|
|
305
|
+
// For immediate deletion from an opaque page, add a fifth argument:
|
|
306
|
+
// (page, id) => {
|
|
307
|
+
// const rows = page.tickets.filter((row) => String(row.id) !== id)
|
|
308
|
+
// return { ...page, tickets: rows, total: page.total - (page.tickets.length - rows.length) }
|
|
309
|
+
// }
|
|
305
310
|
```
|
|
306
311
|
|
|
307
312
|
A resource mutation also waits out a pre-mutation list request before fetching again;
|
|
308
313
|
joining that older in-flight read alone would leave the new record absent.
|
|
314
|
+
Successful deletion removes a row immediately, including one just created through the
|
|
315
|
+
resource. Stale reads cannot restore acknowledged deletions for that viewer; an explicit
|
|
316
|
+
successful create can recreate the identity. Opaque listPage envelopes need their own
|
|
317
|
+
removeFromPage adapter; no row or total property is guessed.
|
|
309
318
|
|
|
310
319
|
## Type
|
|
311
320
|
|
|
@@ -394,12 +403,15 @@ interface QueryResource<T, Create = Partial<T>, Update = Partial<T>> {
|
|
|
394
403
|
* Options for a collection's whole page (rows plus totals), or a filtered collection path.
|
|
395
404
|
* Reads share this resource's mutation refresh and viewer isolation. `readPage` reads the
|
|
396
405
|
* canonical page response for `path`; row-only `list` reads keep a separate cache entry.
|
|
406
|
+
* Supply `removeFromPage` for opaque page envelopes to remove acknowledged deletions
|
|
407
|
+
* immediately and keep stale list responses from restoring them.
|
|
397
408
|
*/
|
|
398
409
|
listPage<Page>(
|
|
399
410
|
viewer: string,
|
|
400
411
|
params: Record<string, unknown>,
|
|
401
412
|
readPage: (response: ResourceResponse) => Page,
|
|
402
413
|
path?: string,
|
|
414
|
+
removeFromPage?: (page: Page, id: string) => Page,
|
|
403
415
|
): QueryOptions<Page>
|
|
404
416
|
/**
|
|
405
417
|
* `useQuery` options for one record, or null while `id` is not known yet (`useQuery(null)`
|
|
@@ -410,7 +422,7 @@ interface QueryResource<T, Create = Partial<T>, Update = Partial<T>> {
|
|
|
410
422
|
create(viewer: string, input: Create): Promise<T>
|
|
411
423
|
/** Updates a record, shows the saved record in the cache and refreshes the lists on screen. */
|
|
412
424
|
update(viewer: string, id: string, input: Update): Promise<T>
|
|
413
|
-
/** Removes a record
|
|
425
|
+
/** Removes a record from detail and tracked row lists, then refreshes those lists. */
|
|
414
426
|
remove(viewer: string, id: string): Promise<void>
|
|
415
427
|
/**
|
|
416
428
|
* Switches a flag the API models as a sub-path: `POST path/id/action` turns it on and
|
package/dist/index.d.ts
CHANGED
|
@@ -271,9 +271,18 @@
|
|
|
271
271
|
* // and tickets.refresh('agent-1') refresh this page too.
|
|
272
272
|
* // A second list can use tickets.listPage(viewer, params, readPage, '/tickets/open').
|
|
273
273
|
* // Use one canonical page reader per path; list() still returns only its own rows.
|
|
274
|
+
* // For immediate deletion from an opaque page, add a fifth argument:
|
|
275
|
+
* // (page, id) => {
|
|
276
|
+
* // const rows = page.tickets.filter((row) => String(row.id) !== id)
|
|
277
|
+
* // return { ...page, tickets: rows, total: page.total - (page.tickets.length - rows.length) }
|
|
278
|
+
* // }
|
|
274
279
|
* ```
|
|
275
280
|
* A resource mutation also waits out a pre-mutation list request before fetching again;
|
|
276
281
|
* joining that older in-flight read alone would leave the new record absent.
|
|
282
|
+
* Successful deletion removes a row immediately, including one just created through the
|
|
283
|
+
* resource. Stale reads cannot restore acknowledged deletions for that viewer; an explicit
|
|
284
|
+
* successful create can recreate the identity. Opaque listPage envelopes need their own
|
|
285
|
+
* removeFromPage adapter; no row or total property is guessed.
|
|
277
286
|
*
|
|
278
287
|
* @remarks
|
|
279
288
|
* - **`useQuery` reads the cache synchronously on the first render.** A
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoWG;AAEH,cAAc,eAAe,CAAA;AAC7B,cAAc,YAAY,CAAA;AAC1B,cAAc,kBAAkB,CAAA;AAChC,cAAc,eAAe,CAAA"}
|
package/dist/index.js
CHANGED
|
@@ -271,9 +271,18 @@
|
|
|
271
271
|
* // and tickets.refresh('agent-1') refresh this page too.
|
|
272
272
|
* // A second list can use tickets.listPage(viewer, params, readPage, '/tickets/open').
|
|
273
273
|
* // Use one canonical page reader per path; list() still returns only its own rows.
|
|
274
|
+
* // For immediate deletion from an opaque page, add a fifth argument:
|
|
275
|
+
* // (page, id) => {
|
|
276
|
+
* // const rows = page.tickets.filter((row) => String(row.id) !== id)
|
|
277
|
+
* // return { ...page, tickets: rows, total: page.total - (page.tickets.length - rows.length) }
|
|
278
|
+
* // }
|
|
274
279
|
* ```
|
|
275
280
|
* A resource mutation also waits out a pre-mutation list request before fetching again;
|
|
276
281
|
* joining that older in-flight read alone would leave the new record absent.
|
|
282
|
+
* Successful deletion removes a row immediately, including one just created through the
|
|
283
|
+
* resource. Stale reads cannot restore acknowledged deletions for that viewer; an explicit
|
|
284
|
+
* successful create can recreate the identity. Opaque listPage envelopes need their own
|
|
285
|
+
* removeFromPage adapter; no row or total property is guessed.
|
|
277
286
|
*
|
|
278
287
|
* @remarks
|
|
279
288
|
* - **`useQuery` reads the cache synchronously on the first render.** A
|
package/dist/resource.d.ts
CHANGED
|
@@ -57,8 +57,10 @@ export interface QueryResource<T, Create = Partial<T>, Update = Partial<T>> {
|
|
|
57
57
|
* Options for a collection's whole page (rows plus totals), or a filtered collection path.
|
|
58
58
|
* Reads share this resource's mutation refresh and viewer isolation. `readPage` reads the
|
|
59
59
|
* canonical page response for `path`; row-only `list` reads keep a separate cache entry.
|
|
60
|
+
* Supply `removeFromPage` for opaque page envelopes to remove acknowledged deletions
|
|
61
|
+
* immediately and keep stale list responses from restoring them.
|
|
60
62
|
*/
|
|
61
|
-
listPage<Page>(viewer: string, params: Record<string, unknown>, readPage: (response: ResourceResponse) => Page, path?: string): QueryOptions<Page>;
|
|
63
|
+
listPage<Page>(viewer: string, params: Record<string, unknown>, readPage: (response: ResourceResponse) => Page, path?: string, removeFromPage?: (page: Page, id: string) => Page): QueryOptions<Page>;
|
|
62
64
|
/**
|
|
63
65
|
* `useQuery` options for one record, or null while `id` is not known yet (`useQuery(null)`
|
|
64
66
|
* observes nothing). A missing record resolves to null, not to an error.
|
|
@@ -68,7 +70,7 @@ export interface QueryResource<T, Create = Partial<T>, Update = Partial<T>> {
|
|
|
68
70
|
create(viewer: string, input: Create): Promise<T>;
|
|
69
71
|
/** Updates a record, shows the saved record in the cache and refreshes the lists on screen. */
|
|
70
72
|
update(viewer: string, id: string, input: Update): Promise<T>;
|
|
71
|
-
/** Removes a record
|
|
73
|
+
/** Removes a record from detail and tracked row lists, then refreshes those lists. */
|
|
72
74
|
remove(viewer: string, id: string): Promise<void>;
|
|
73
75
|
/**
|
|
74
76
|
* Switches a flag the API models as a sub-path: `POST path/id/action` turns it on and
|
package/dist/resource.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"resource.d.ts","sourceRoot":"","sources":["../src/resource.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EAAY,YAAY,EAAE,MAAM,qBAAqB,CAAA;AAGjE,qDAAqD;AACrD,MAAM,WAAW,gBAAgB;IAC/B,gCAAgC;IAChC,IAAI,EAAE,OAAO,CAAA;CACd;AAED;;;GAGG;AACH,MAAM,WAAW,YAAY;IAC3B,8EAA8E;IAC9E,GAAG,CACD,IAAI,EAAE,MAAM,EACZ,OAAO,CAAC,EAAE;QAAE,MAAM,CAAC,EAAE,WAAW,CAAC;QAAC,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;KAAE,GACnE,OAAO,CAAC,gBAAgB,CAAC,CAAA;IAC5B,yBAAyB;IACzB,IAAI,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC,gBAAgB,CAAC,CAAA;IAC7D,0BAA0B;IAC1B,GAAG,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,CAAC,EA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;;;OAIG;IACH,MAAM,CAAC,MAAM,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,EAAE,EAAE,OAAO,GAAG,OAAO,CAAC,CAAC,CAAC,CAAA;IAC3E,2FAA2F;IAC3F,OAAO,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAA;CACvC;AAKD;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,mBAAmB,CAAC,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,CAAC,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,CAAC,CAAC,EAC7E,MAAM,EAAE,mBAAmB,CAAC,CAAC,CAAC,GAC7B,aAAa,CAAC,CAAC,EAAE,MAAM,EAAE,MAAM,CAAC,CA8JlC"}
|
package/dist/resource.js
CHANGED
|
@@ -34,10 +34,31 @@ export function createQueryResource(config) {
|
|
|
34
34
|
// The lists handed out most recently, newest last: the ones a write fetches again. A list
|
|
35
35
|
// nobody reads any more costs one request and is then dropped by the next ones.
|
|
36
36
|
const recent = new Map();
|
|
37
|
-
const
|
|
37
|
+
const removed = new Map();
|
|
38
|
+
const removals = (viewer) => removed.get(viewer) ?? new Set();
|
|
39
|
+
const recordId = (record) => {
|
|
40
|
+
const id = record != null ? record[idField] : undefined;
|
|
41
|
+
return typeof id === 'string' || typeof id === 'number' ? String(id) : undefined;
|
|
42
|
+
};
|
|
43
|
+
const withoutRemoved = (viewer, rows) => {
|
|
44
|
+
const ids = removed.get(viewer);
|
|
45
|
+
return ids?.size ? rows.filter((record) => !ids.has(recordId(record) ?? '')) : rows;
|
|
46
|
+
};
|
|
47
|
+
const projectPage = (viewer, page, remove) => {
|
|
48
|
+
if (!remove)
|
|
49
|
+
return page;
|
|
50
|
+
for (const id of removals(viewer))
|
|
51
|
+
page = remove(page, id);
|
|
52
|
+
return page;
|
|
53
|
+
};
|
|
54
|
+
const track = (viewer, options, remove) => {
|
|
38
55
|
const hash = hashQueryKey(options.key);
|
|
39
56
|
recent.delete(hash);
|
|
40
|
-
recent.set(hash, {
|
|
57
|
+
recent.set(hash, {
|
|
58
|
+
viewer,
|
|
59
|
+
options,
|
|
60
|
+
...(remove ? { remove: (page, id) => remove(page, id) } : {}),
|
|
61
|
+
});
|
|
41
62
|
for (const stale of [...recent.keys()].slice(0, -REFRESHED_LISTS))
|
|
42
63
|
recent.delete(stale);
|
|
43
64
|
return options;
|
|
@@ -47,9 +68,9 @@ export function createQueryResource(config) {
|
|
|
47
68
|
key: [config.name, viewer, 'list', params],
|
|
48
69
|
principal: viewer,
|
|
49
70
|
keepPreviousData: true,
|
|
50
|
-
fetch: async (signal) => readList(await config.http().get(base, { signal, params })),
|
|
71
|
+
fetch: async (signal) => withoutRemoved(viewer, readList(await config.http().get(base, { signal, params }))),
|
|
51
72
|
};
|
|
52
|
-
return track(viewer, options);
|
|
73
|
+
return track(viewer, options, (rows) => withoutRemoved(viewer, rows));
|
|
53
74
|
};
|
|
54
75
|
const refresh = async (viewer) => {
|
|
55
76
|
const client = getQueryClient();
|
|
@@ -70,32 +91,39 @@ export function createQueryResource(config) {
|
|
|
70
91
|
}
|
|
71
92
|
}));
|
|
72
93
|
};
|
|
73
|
-
const saved = async (viewer, response) => {
|
|
94
|
+
const saved = async (viewer, response, created = false) => {
|
|
74
95
|
const record = readOne(response);
|
|
75
96
|
if (record == null)
|
|
76
97
|
throw new Error(`${config.name}: the response held no record`);
|
|
77
|
-
const id = record
|
|
78
|
-
if (
|
|
79
|
-
|
|
98
|
+
const id = recordId(record);
|
|
99
|
+
if (id !== undefined) {
|
|
100
|
+
if (created)
|
|
101
|
+
removed.get(viewer)?.delete(id);
|
|
102
|
+
if (!removals(viewer).has(id))
|
|
103
|
+
getQueryClient().set(detailKey(viewer, id), record);
|
|
104
|
+
}
|
|
80
105
|
await refresh(viewer);
|
|
81
106
|
return record;
|
|
82
107
|
};
|
|
83
108
|
return {
|
|
84
109
|
list,
|
|
85
|
-
listPage: (viewer, params, readPage, path = base) => track(viewer, {
|
|
110
|
+
listPage: (viewer, params, readPage, path = base, removeFromPage) => track(viewer, {
|
|
86
111
|
key: [config.name, viewer, 'list', 'page', path, params],
|
|
87
112
|
principal: viewer,
|
|
88
113
|
keepPreviousData: true,
|
|
89
|
-
fetch: async (signal) => readPage(await config.http().get(path, { signal, params })),
|
|
90
|
-
}),
|
|
114
|
+
fetch: async (signal) => projectPage(viewer, readPage(await config.http().get(path, { signal, params })), removeFromPage),
|
|
115
|
+
}, removeFromPage),
|
|
91
116
|
detail: (viewer, id) => id == null || id === ''
|
|
92
117
|
? null
|
|
93
118
|
: {
|
|
94
119
|
key: detailKey(viewer, id),
|
|
95
120
|
principal: viewer,
|
|
96
121
|
fetch: async (signal) => {
|
|
122
|
+
if (removals(viewer).has(id))
|
|
123
|
+
return null;
|
|
97
124
|
try {
|
|
98
|
-
|
|
125
|
+
const record = readOne(await config.http().get(one(id), { signal }));
|
|
126
|
+
return removals(viewer).has(id) ? null : record;
|
|
99
127
|
}
|
|
100
128
|
catch (error) {
|
|
101
129
|
// A record that does not exist is an answer, not a failed load.
|
|
@@ -105,11 +133,22 @@ export function createQueryResource(config) {
|
|
|
105
133
|
}
|
|
106
134
|
},
|
|
107
135
|
},
|
|
108
|
-
create: async (viewer, input) => saved(viewer, await config.http().post(base, input)),
|
|
136
|
+
create: async (viewer, input) => saved(viewer, await config.http().post(base, input), true),
|
|
109
137
|
update: async (viewer, id, input) => saved(viewer, await config.http()[config.updateMethod ?? 'put'](one(id), input)),
|
|
110
138
|
remove: async (viewer, id) => {
|
|
111
139
|
await config.http().delete(one(id));
|
|
112
|
-
|
|
140
|
+
const ids = removed.get(viewer) ?? new Set();
|
|
141
|
+
ids.add(id);
|
|
142
|
+
removed.set(viewer, ids);
|
|
143
|
+
const client = getQueryClient();
|
|
144
|
+
client.set(detailKey(viewer, id), null);
|
|
145
|
+
for (const entry of recent.values()) {
|
|
146
|
+
if (entry.viewer !== viewer || !entry.remove)
|
|
147
|
+
continue;
|
|
148
|
+
const page = client.getState(entry.options.key).data;
|
|
149
|
+
if (page !== undefined)
|
|
150
|
+
client.set(entry.options.key, entry.remove(page, id));
|
|
151
|
+
}
|
|
113
152
|
await refresh(viewer);
|
|
114
153
|
},
|
|
115
154
|
toggle: async (viewer, id, action, on) => {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@molecule/app-query-react",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.5.0",
|
|
4
4
|
"description": "React hooks for @molecule/app-query — read a cached document synchronously, observe it, and prefetch on intent",
|
|
5
5
|
"homepage": "https://www.molecule.dev/packages/app-query-react",
|
|
6
6
|
"type": "module",
|