@jskit-ai/http-web 0.2.57 → 0.2.58
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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jskit-ai/http-web",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.58",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"test": "jskit test"
|
|
@@ -47,13 +47,13 @@
|
|
|
47
47
|
"./locales/en.json": "./locales/en.json"
|
|
48
48
|
},
|
|
49
49
|
"dependencies": {
|
|
50
|
-
"@jskit-ai/resource-crud-core": "0.1.
|
|
50
|
+
"@jskit-ai/resource-crud-core": "0.1.226"
|
|
51
51
|
},
|
|
52
52
|
"peerDependencies": {
|
|
53
|
-
"@jskit-ai/http-runtime": "0.2.
|
|
54
|
-
"@jskit-ai/kernel": "0.2.
|
|
55
|
-
"@jskit-ai/realtime": "0.2.
|
|
56
|
-
"@jskit-ai/shell-web": "0.2.
|
|
53
|
+
"@jskit-ai/http-runtime": "0.2.33",
|
|
54
|
+
"@jskit-ai/kernel": "0.2.33",
|
|
55
|
+
"@jskit-ai/realtime": "0.2.52",
|
|
56
|
+
"@jskit-ai/shell-web": "0.2.49",
|
|
57
57
|
"@tanstack/vue-query": "^5.90.5",
|
|
58
58
|
"vue": "^3.5.13",
|
|
59
59
|
"vue-router": "^5.0.4",
|
|
@@ -410,6 +410,10 @@ function useList({
|
|
|
410
410
|
);
|
|
411
411
|
}
|
|
412
412
|
|
|
413
|
+
// Changing the search or a filter starts the list again at its first page: the new key's cached pages are trimmed to
|
|
414
|
+
// the first. The trim runs at the change itself (flush "sync"), before vue-query's own watcher on the query's
|
|
415
|
+
// options sees the new key. In the same pre-flush, the two watchers run in the order they were notified, and when
|
|
416
|
+
// vue-query's runs first, its refetch of the new key takes every cached page and puts them all back over the trim.
|
|
413
417
|
watch(activeSearchQuery, (nextValue, previousValue) => {
|
|
414
418
|
if (!querySearchEnabled.value) {
|
|
415
419
|
return;
|
|
@@ -419,7 +423,7 @@ function useList({
|
|
|
419
423
|
}
|
|
420
424
|
|
|
421
425
|
list.trimToFirstPage();
|
|
422
|
-
});
|
|
426
|
+
}, { flush: "sync" });
|
|
423
427
|
watch(
|
|
424
428
|
[
|
|
425
429
|
activeQueryParamsToken,
|
|
@@ -428,7 +432,8 @@ function useList({
|
|
|
428
432
|
],
|
|
429
433
|
() => {
|
|
430
434
|
list.trimToFirstPage();
|
|
431
|
-
}
|
|
435
|
+
},
|
|
436
|
+
{ flush: "sync" }
|
|
432
437
|
);
|
|
433
438
|
const listItems = computed(() => (Array.isArray(list.items.value) ? list.items.value : []));
|
|
434
439
|
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import test from "node:test";
|
|
3
|
+
import { QueryClient, VueQueryPlugin } from "@tanstack/vue-query";
|
|
4
|
+
import { createRenderer, h, nextTick, ref } from "vue";
|
|
5
|
+
import { createMemoryHistory, createRouter } from "vue-router";
|
|
6
|
+
import { useList } from "../src/client/composables/records/useList.js";
|
|
7
|
+
import { useCrudListFilters } from "../src/client/composables/useCrudListFilters.js";
|
|
8
|
+
|
|
9
|
+
// Changing or removing a filter, or changing the search, starts the list again at its first page. Two watchers of the
|
|
10
|
+
// list's component react to such a change in the same flush: vue-query's own, on the query's options, which starts the
|
|
11
|
+
// refetch of the new key's cached pages (all of them, as they are when it starts), and the list's, which trims that
|
|
12
|
+
// cache to its first page. They run in the order they were notified, which shifts with render timing, so these tests
|
|
13
|
+
// make vue-query's run first: something else the query reads (its gcTime) changes first in the same tick.
|
|
14
|
+
|
|
15
|
+
const PAGE_SIZE = 20;
|
|
16
|
+
const SEARCH_DEBOUNCE_MS = 50;
|
|
17
|
+
const REGION_FILTER = Object.freeze({
|
|
18
|
+
region: Object.freeze({
|
|
19
|
+
type: "enum",
|
|
20
|
+
options: Object.freeze([
|
|
21
|
+
Object.freeze({ label: "Far", value: "far" }),
|
|
22
|
+
Object.freeze({ label: "Near", value: "near" })
|
|
23
|
+
])
|
|
24
|
+
})
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
const node = (type, text = "") => ({ type, text, props: {}, children: [], parent: null });
|
|
28
|
+
const renderer = createRenderer({
|
|
29
|
+
createElement: node,
|
|
30
|
+
createText: (text) => node("text", text),
|
|
31
|
+
createComment: () => node("comment"),
|
|
32
|
+
setText: (item, text) => { item.text = text; },
|
|
33
|
+
setElementText: (item, text) => { item.text = text; item.children = []; },
|
|
34
|
+
patchProp: (item, key, _previous, value) => { item.props[key] = value; },
|
|
35
|
+
parentNode: (item) => item.parent,
|
|
36
|
+
nextSibling: (item) => item.parent?.children[item.parent.children.indexOf(item) + 1] ?? null,
|
|
37
|
+
insert(item, parent, anchor) {
|
|
38
|
+
if (item.parent) item.parent.children.splice(item.parent.children.indexOf(item), 1);
|
|
39
|
+
item.parent = parent;
|
|
40
|
+
const index = anchor ? parent.children.indexOf(anchor) : -1;
|
|
41
|
+
if (index < 0) parent.children.push(item); else parent.children.splice(index, 0, item);
|
|
42
|
+
},
|
|
43
|
+
remove(item) {
|
|
44
|
+
if (item.parent) item.parent.children.splice(item.parent.children.indexOf(item), 1);
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
async function waitFor(check, message) {
|
|
49
|
+
for (let attempt = 0; attempt < 500; attempt += 1) {
|
|
50
|
+
if (check()) {
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
await new Promise((resolve) => setTimeout(resolve, 2));
|
|
54
|
+
}
|
|
55
|
+
assert.fail(message);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// Three pages of 20 projects; a region filter or a search finds one.
|
|
59
|
+
function projectsPage(query = {}) {
|
|
60
|
+
if (query.region || query.q) {
|
|
61
|
+
return { items: [{ id: "far" }], nextCursor: null };
|
|
62
|
+
}
|
|
63
|
+
const page = Number(query.cursor ?? 0);
|
|
64
|
+
return {
|
|
65
|
+
items: Array.from({ length: PAGE_SIZE }, (_, index) => ({ id: `p${page * PAGE_SIZE + index}` })),
|
|
66
|
+
nextCursor: page < 2 ? String(page + 1) : null
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// Mounts a list of projects with a region filter (and the search, when given) and loads its first two pages.
|
|
71
|
+
async function mountProjectList(t, { search = null } = {}) {
|
|
72
|
+
const requests = [];
|
|
73
|
+
const client = {
|
|
74
|
+
async request(_path, options) {
|
|
75
|
+
requests.push(options.query);
|
|
76
|
+
return projectsPage(options.query);
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
const router = createRouter({
|
|
80
|
+
history: createMemoryHistory(),
|
|
81
|
+
routes: [{ path: "/projects", component: { render: () => h("div") } }]
|
|
82
|
+
});
|
|
83
|
+
await router.push("/projects");
|
|
84
|
+
await router.isReady();
|
|
85
|
+
const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } });
|
|
86
|
+
const gcTime = ref(5 * 60 * 1000);
|
|
87
|
+
let filters = null;
|
|
88
|
+
let list = null;
|
|
89
|
+
const app = renderer.createApp({
|
|
90
|
+
setup() {
|
|
91
|
+
filters = useCrudListFilters(REGION_FILTER);
|
|
92
|
+
list = useList({
|
|
93
|
+
apiSuffix: "/projects",
|
|
94
|
+
ownershipFilter: "public",
|
|
95
|
+
client,
|
|
96
|
+
queryKeyFactory: () => ["projects"],
|
|
97
|
+
queryParams: filters.queryParams,
|
|
98
|
+
search,
|
|
99
|
+
queryOptions: { gcTime }
|
|
100
|
+
});
|
|
101
|
+
return () => h("div");
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
app.use(router);
|
|
105
|
+
app.use(VueQueryPlugin, { queryClient });
|
|
106
|
+
app.mount(node("root"));
|
|
107
|
+
t.after(() => {
|
|
108
|
+
app.unmount();
|
|
109
|
+
queryClient.clear();
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
await waitFor(() => list.items.length === PAGE_SIZE && !list.isFetching, "The list loads its first page.");
|
|
113
|
+
await list.loadMore();
|
|
114
|
+
await waitFor(() => list.items.length === PAGE_SIZE * 2 && !list.isFetching, "The list loads its second page.");
|
|
115
|
+
return { list, filters, requests, gcTime };
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// Queues vue-query's own watcher on the list's query options before any watcher the next change notifies.
|
|
119
|
+
function queueVueQueryFirst(gcTime) {
|
|
120
|
+
gcTime.value += 1;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
async function waitForReload(list, requests, requestsBefore, message) {
|
|
124
|
+
await waitFor(() => requests.length > requestsBefore && !list.isFetching, message);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
test("removing a filter starts the list again at its first page when vue-query's watcher runs first", async (t) => {
|
|
128
|
+
const { list, filters, requests, gcTime } = await mountProjectList(t);
|
|
129
|
+
filters.values.region = "far";
|
|
130
|
+
await waitFor(() => list.items.length === 1 && !list.isFetching, "The region filter finds one project.");
|
|
131
|
+
|
|
132
|
+
const requestsBefore = requests.length;
|
|
133
|
+
queueVueQueryFirst(gcTime);
|
|
134
|
+
filters.clearFilter("region");
|
|
135
|
+
await waitForReload(list, requests, requestsBefore, "The list loads again without the filter.");
|
|
136
|
+
|
|
137
|
+
assert.equal(list.items.length, PAGE_SIZE);
|
|
138
|
+
assert.deepEqual(requests.slice(requestsBefore), [{ limit: PAGE_SIZE }]);
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
test("clearing the search starts the list again at its first page when vue-query's watcher runs first", async (t) => {
|
|
142
|
+
const { list, requests, gcTime } = await mountProjectList(t, {
|
|
143
|
+
search: { enabled: true, debounceMs: SEARCH_DEBOUNCE_MS }
|
|
144
|
+
});
|
|
145
|
+
list.searchQuery = "far";
|
|
146
|
+
await waitFor(
|
|
147
|
+
() => list.items.length === 1 && !list.isFetching && !list.isSearchDebouncing,
|
|
148
|
+
"The search finds one project."
|
|
149
|
+
);
|
|
150
|
+
|
|
151
|
+
const requestsBefore = requests.length;
|
|
152
|
+
// The search's debounce ends on a mocked clock, so it ends in the same tick as the change that queues vue-query's
|
|
153
|
+
// watcher first. Nothing else starts a timer meanwhile.
|
|
154
|
+
t.mock.timers.enable({ apis: ["setTimeout"] });
|
|
155
|
+
try {
|
|
156
|
+
list.searchQuery = "";
|
|
157
|
+
await nextTick();
|
|
158
|
+
queueVueQueryFirst(gcTime);
|
|
159
|
+
t.mock.timers.tick(SEARCH_DEBOUNCE_MS);
|
|
160
|
+
} finally {
|
|
161
|
+
t.mock.timers.reset();
|
|
162
|
+
}
|
|
163
|
+
await waitForReload(list, requests, requestsBefore, "The list loads again without the search.");
|
|
164
|
+
|
|
165
|
+
assert.equal(list.items.length, PAGE_SIZE);
|
|
166
|
+
assert.deepEqual(requests.slice(requestsBefore), [{ limit: PAGE_SIZE }]);
|
|
167
|
+
});
|