@jskit-ai/http-web 0.2.2 → 0.2.4
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.4",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"test": "node --test --test-concurrency=1"
|
|
@@ -52,7 +52,7 @@
|
|
|
52
52
|
"peerDependencies": {
|
|
53
53
|
"@jskit-ai/http-runtime": "0.2.1",
|
|
54
54
|
"@jskit-ai/kernel": "0.2.1",
|
|
55
|
-
"@jskit-ai/realtime": "0.2.
|
|
55
|
+
"@jskit-ai/realtime": "0.2.3",
|
|
56
56
|
"@jskit-ai/shell-web": "0.2.1",
|
|
57
57
|
"@tanstack/vue-query": "^5.90.5",
|
|
58
58
|
"vue": "^3.5.13",
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { computed, unref } from "vue";
|
|
1
|
+
import { computed, nextTick, unref } from "vue";
|
|
2
2
|
import { useQueryClient } from "@tanstack/vue-query";
|
|
3
3
|
import { normalizeText } from "@jskit-ai/kernel/shared/support/normalize";
|
|
4
4
|
import { useRealtimeEvent } from "@jskit-ai/realtime/client/composables/useRealtimeEvent";
|
|
@@ -55,6 +55,41 @@ function toQueryKeyList(value) {
|
|
|
55
55
|
return [Object.freeze([...resolved])];
|
|
56
56
|
}
|
|
57
57
|
|
|
58
|
+
// Realtime never cancels a read in flight: the browser would only drop its
|
|
59
|
+
// response while the server still runs the request in full. A query that is
|
|
60
|
+
// reading is read once more after that read settles, so a change made meanwhile
|
|
61
|
+
// is still read, and events arriving before that read starts join it. A burst
|
|
62
|
+
// of events therefore costs at most two reads per query.
|
|
63
|
+
const followUpReads = new WeakMap();
|
|
64
|
+
|
|
65
|
+
function refetchAfterCurrentRead(queryClient, query) {
|
|
66
|
+
if (followUpReads.has(query)) {
|
|
67
|
+
return followUpReads.get(query);
|
|
68
|
+
}
|
|
69
|
+
// Joins a read in flight; failures surface in the query's own state.
|
|
70
|
+
const read = () => queryClient.refetchQueries({ queryKey: query.queryKey, exact: true }, { cancelRefetch: false });
|
|
71
|
+
if (query.state.fetchStatus !== "fetching") {
|
|
72
|
+
return read();
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const followUp = read().then(() => {
|
|
76
|
+
followUpReads.delete(query);
|
|
77
|
+
return read();
|
|
78
|
+
});
|
|
79
|
+
followUpReads.set(query, followUp);
|
|
80
|
+
return followUp;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// Marks every matching query stale, then reads again the ones on screen; the
|
|
84
|
+
// others read when they are next used.
|
|
85
|
+
async function refreshQueriesWithoutCancelling(queryClient, queryKey) {
|
|
86
|
+
await queryClient.invalidateQueries({ queryKey, refetchType: "none" });
|
|
87
|
+
// As TanStack's own invalidation does, read after reactive values settle.
|
|
88
|
+
await nextTick();
|
|
89
|
+
await Promise.all(queryClient.getQueryCache().findAll({ queryKey, type: "active" })
|
|
90
|
+
.map((query) => refetchAfterCurrentRead(queryClient, query)));
|
|
91
|
+
}
|
|
92
|
+
|
|
58
93
|
function useRealtimeQueryInvalidation({
|
|
59
94
|
event = "",
|
|
60
95
|
enabled = true,
|
|
@@ -77,9 +112,7 @@ function useRealtimeQueryInvalidation({
|
|
|
77
112
|
|
|
78
113
|
const keys = toQueryKeyList(queryKey);
|
|
79
114
|
for (const key of keys) {
|
|
80
|
-
await queryClient
|
|
81
|
-
queryKey: key
|
|
82
|
-
});
|
|
115
|
+
await refreshQueriesWithoutCancelling(queryClient, key);
|
|
83
116
|
}
|
|
84
117
|
}
|
|
85
118
|
});
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import assert from "node:assert/strict";
|
|
2
2
|
import test from "node:test";
|
|
3
|
-
import { QueryClient, VueQueryPlugin } from "@tanstack/vue-query";
|
|
3
|
+
import { QueryClient, QueryObserver, VueQueryPlugin } from "@tanstack/vue-query";
|
|
4
4
|
import { createSSRApp, h } from "vue";
|
|
5
5
|
import { renderToString } from "vue/server-renderer";
|
|
6
6
|
|
|
@@ -16,7 +16,8 @@ import {
|
|
|
16
16
|
} from "../src/client/lib/httpClient.js";
|
|
17
17
|
import { buildListRequestOptions, useListCore } from "../src/client/composables/runtime/useListCore.js";
|
|
18
18
|
import {
|
|
19
|
-
resolveOperationRealtimeOptions
|
|
19
|
+
resolveOperationRealtimeOptions,
|
|
20
|
+
useRealtimeQueryInvalidation
|
|
20
21
|
} from "../src/client/composables/useRealtimeQueryInvalidation.js";
|
|
21
22
|
|
|
22
23
|
test("endpoint read request options include transport only when provided", () => {
|
|
@@ -250,11 +251,63 @@ test("endpoint resources invalidate their query key from configured realtime eve
|
|
|
250
251
|
|
|
251
252
|
assert.deepEqual(invalidations, [
|
|
252
253
|
{
|
|
253
|
-
queryKey: ["today-workout-detail", "/api/today/workouts/2026-05-06"]
|
|
254
|
+
queryKey: ["today-workout-detail", "/api/today/workouts/2026-05-06"],
|
|
255
|
+
refetchType: "none"
|
|
254
256
|
}
|
|
255
257
|
]);
|
|
256
258
|
});
|
|
257
259
|
|
|
260
|
+
test("realtime bursts never cancel a read in flight and read once more after it", async (t) => {
|
|
261
|
+
const queryClient = new QueryClient();
|
|
262
|
+
const queryKey = ["records"];
|
|
263
|
+
// Each read stays open until the test answers it.
|
|
264
|
+
const reads = [];
|
|
265
|
+
const read = ({ signal }) => new Promise((resolve) => reads.push({ signal, resolve }));
|
|
266
|
+
const settle = () => new Promise((resolve) => setImmediate(resolve));
|
|
267
|
+
const socketHandlers = new Map();
|
|
268
|
+
const socket = {
|
|
269
|
+
on(eventName, handler) {
|
|
270
|
+
socketHandlers.set(eventName, handler);
|
|
271
|
+
},
|
|
272
|
+
off() {}
|
|
273
|
+
};
|
|
274
|
+
const app = createSSRApp({
|
|
275
|
+
setup() {
|
|
276
|
+
useRealtimeQueryInvalidation({ event: "records.changed", queryKey });
|
|
277
|
+
return () => h("div");
|
|
278
|
+
}
|
|
279
|
+
});
|
|
280
|
+
app.use(VueQueryPlugin, { queryClient });
|
|
281
|
+
app.provide("jskit.realtime.runtime.client.socket", socket);
|
|
282
|
+
await renderToString(app);
|
|
283
|
+
// A screen showing the records keeps the query active.
|
|
284
|
+
const screen = new QueryObserver(queryClient, { queryKey, queryFn: read });
|
|
285
|
+
t.after(screen.subscribe(() => {}));
|
|
286
|
+
reads[0].resolve("first");
|
|
287
|
+
await settle();
|
|
288
|
+
|
|
289
|
+
const changed = socketHandlers.get("records.changed");
|
|
290
|
+
changed({});
|
|
291
|
+
await settle();
|
|
292
|
+
assert.equal(reads.length, 2);
|
|
293
|
+
|
|
294
|
+
changed({});
|
|
295
|
+
changed({});
|
|
296
|
+
changed({});
|
|
297
|
+
await settle();
|
|
298
|
+
assert.equal(reads.length, 2, "Events during a read join it instead of cancelling it");
|
|
299
|
+
|
|
300
|
+
reads[1].resolve("second");
|
|
301
|
+
await settle();
|
|
302
|
+
assert.equal(reads.length, 3, "One more read picks up changes made during the first");
|
|
303
|
+
reads[2].resolve("third");
|
|
304
|
+
await settle();
|
|
305
|
+
|
|
306
|
+
assert.equal(reads.length, 3);
|
|
307
|
+
assert.equal(reads.some((entry) => entry.signal.aborted), false);
|
|
308
|
+
assert.equal(queryClient.getQueryData(queryKey), "third");
|
|
309
|
+
});
|
|
310
|
+
|
|
258
311
|
test("endpoint resource reads attach request recovery metadata to query options", async () => {
|
|
259
312
|
const queryClient = new QueryClient();
|
|
260
313
|
const queryKey = ["project-access", "beepollen"];
|