@hyper-fetch/db 10.5.0 → 10.5.1
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/__tests__/integration/infinite.spec.ts +33 -0
- package/__tests__/integration/syncs.spec.ts +11 -0
- package/dist/index.mjs +20 -9
- package/dist/index.mjs.map +1 -1
- package/dist/query/query.d.ts +2 -2
- package/dist/subset/base-subset.d.ts.map +1 -1
- package/dist/subset/list-subset.d.ts +8 -0
- package/dist/subset/list-subset.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/query/live-query.ts +2 -2
- package/src/query/query.ts +2 -2
- package/src/subset/base-subset.ts +4 -0
- package/src/subset/list-subset.ts +20 -6
|
@@ -179,6 +179,39 @@ describe("db [ Infinite queries ]", () => {
|
|
|
179
179
|
expect(api.callsTo("/top")).toHaveLength(2);
|
|
180
180
|
});
|
|
181
181
|
|
|
182
|
+
it("shows a pending insert inside the loaded range at once and removes it when the write fails", async () => {
|
|
183
|
+
const { api, db } = context;
|
|
184
|
+
serveFeed(api, () => [1, 2, 3, 4, 5, 6].map((id) => createPost(id)));
|
|
185
|
+
const response = deferred();
|
|
186
|
+
api.post("/posts", async () => {
|
|
187
|
+
await response.promise;
|
|
188
|
+
return { status: 500, data: { message: "Rejected" } };
|
|
189
|
+
});
|
|
190
|
+
const feed = db.feed.orderBy((post) => post.createdAt).infinite({ pageSize: 2 });
|
|
191
|
+
const list = db.feed.orderBy((post) => post.createdAt);
|
|
192
|
+
const unsubscribeFeed = feed.subscribe(() => {});
|
|
193
|
+
const unsubscribeList = list.subscribe(() => {});
|
|
194
|
+
await vi.waitFor(() => expect(feed.getSnapshot()).toMatchObject({ status: "ready", isFetchingNextPage: false }));
|
|
195
|
+
await vi.waitFor(() => expect(list.getSnapshot().status).toBe("ready"));
|
|
196
|
+
// Both read one subset, which holds the backend pages the window needed
|
|
197
|
+
const loaded = ids(list.getSnapshot().data)!;
|
|
198
|
+
expect(loaded).toStrictEqual([1, 2, 3, 4]);
|
|
199
|
+
|
|
200
|
+
const inRange = db.feed.insert(createPost(0, { createdAt: "2026-01-00" }));
|
|
201
|
+
// Sorts after the last loaded row, where the next backend page may hold rows before it
|
|
202
|
+
const pastRange = db.feed.insert(createPost(9, { createdAt: "2026-09-09" }));
|
|
203
|
+
expect(ids(feed.getSnapshot().data)).toStrictEqual([0, 1]);
|
|
204
|
+
expect(ids(list.getSnapshot().data)).toStrictEqual([0, ...loaded]);
|
|
205
|
+
|
|
206
|
+
response.resolve();
|
|
207
|
+
expect((await inRange).success).toBeFalse();
|
|
208
|
+
expect((await pastRange).success).toBeFalse();
|
|
209
|
+
expect(ids(feed.getSnapshot().data)).toStrictEqual([1, 2]);
|
|
210
|
+
expect(ids(list.getSnapshot().data)).toStrictEqual(loaded);
|
|
211
|
+
unsubscribeFeed();
|
|
212
|
+
unsubscribeList();
|
|
213
|
+
});
|
|
214
|
+
|
|
182
215
|
it("keeps the window live: an insert shifts rows across pages and a delete pulls the next row in", async () => {
|
|
183
216
|
const { api, db } = context;
|
|
184
217
|
const rows = [1, 2, 3, 4, 5, 6].map((id) => createPost(id));
|
|
@@ -275,6 +275,17 @@ describe("db [ Syncs ]", () => {
|
|
|
275
275
|
unsubscribe();
|
|
276
276
|
});
|
|
277
277
|
|
|
278
|
+
it("goes stale when the socket disconnects, even while its last load is within staleTime", async () => {
|
|
279
|
+
const { requests, socket } = context;
|
|
280
|
+
requests.getTeamFullPosts.staleTime = 60_000;
|
|
281
|
+
const { query, unsubscribe } = await loadTeam(context);
|
|
282
|
+
expect(query.getSnapshot().stale).toBeFalse();
|
|
283
|
+
|
|
284
|
+
await socket.disconnect();
|
|
285
|
+
await vi.waitFor(() => expect(query.getSnapshot().stale).toBeTrue());
|
|
286
|
+
unsubscribe();
|
|
287
|
+
});
|
|
288
|
+
|
|
278
289
|
it("does not refetch after a reconnect when the subscription resumes", async () => {
|
|
279
290
|
context.db.destroy();
|
|
280
291
|
context = await setup({ resumes: true });
|
package/dist/index.mjs
CHANGED
|
@@ -1284,8 +1284,8 @@ var Query = class Query {
|
|
|
1284
1284
|
* are left out. With `{ on: "postId" }`, the join matches that field of the joined collection instead of its key and
|
|
1285
1285
|
* holds an array of every matching row, loaded through a list loader that can send `inArray` on it. Options go in an
|
|
1286
1286
|
* object with the field as `by`, as in `join("comments", db.comment, { by: (post) => post.id, on: "postId" })`. The db loads joined rows in batches through the joined
|
|
1287
|
-
* collection's key loader, and a result updates whenever its joined row changes.
|
|
1288
|
-
*
|
|
1287
|
+
* collection's key loader, and a result updates whenever its joined row changes. Syncs keep a joined row live too: a
|
|
1288
|
+
* sync with `where` subscribes with the values of the joined row's own fields, such as its `teamId`.
|
|
1289
1289
|
*/
|
|
1290
1290
|
join(name, collection, by) {
|
|
1291
1291
|
const options = typeof by === "function" ? {} : by;
|
|
@@ -2040,7 +2040,7 @@ var LiveQuery = class {
|
|
|
2040
2040
|
field,
|
|
2041
2041
|
value: fixed[field]
|
|
2042
2042
|
}, row))) return false;
|
|
2043
|
-
if (source.ownsOnly) return source.holds(key);
|
|
2043
|
+
if (source.ownsOnly) return source.holds(key) || source.holdsPendingInsert(key);
|
|
2044
2044
|
return !source.excluded.has(key);
|
|
2045
2045
|
};
|
|
2046
2046
|
/** The joined value of a row: the row with that key, or every row whose `on` field holds the value */
|
|
@@ -2331,6 +2331,7 @@ var BaseSubset = class {
|
|
|
2331
2331
|
isFresh = () => {
|
|
2332
2332
|
if (!this.hasLoaded || this.invalidated) return false;
|
|
2333
2333
|
if (this.isLive()) return true;
|
|
2334
|
+
if (this.subscriptions.some((subscription) => !subscription.isLive())) return false;
|
|
2334
2335
|
return this.loadedAt > 0 && Date.now() - this.loadedAt < getStaleTime(this.loader);
|
|
2335
2336
|
};
|
|
2336
2337
|
/** Marks the subset stale, for example after a reconnect that may have missed events */
|
|
@@ -2433,15 +2434,25 @@ var ListSubset = class extends BaseSubset {
|
|
|
2433
2434
|
if (this.ownsOnly && this.hasLoaded) this.invalidate();
|
|
2434
2435
|
return;
|
|
2435
2436
|
}
|
|
2437
|
+
if (this.fitsLoadedRange(key)) this.inserted.add(key);
|
|
2438
|
+
};
|
|
2439
|
+
/**
|
|
2440
|
+
* Whether a paginated subset shows a row only a pending write created. It does when the row matches what was sent
|
|
2441
|
+
* and sorts inside the loaded range, so an optimistic insert shows at once, like in lists that are not paginated.
|
|
2442
|
+
* Once the server confirms it, `noteInsert` keeps it.
|
|
2443
|
+
*/
|
|
2444
|
+
holdsPendingInsert = (key) => {
|
|
2445
|
+
const { store } = this.collection;
|
|
2446
|
+
return this.plan.paginated && store.hasLayer(key) && !store.getServerRow(key) && this.fitsLoadedRange(key);
|
|
2447
|
+
};
|
|
2448
|
+
/** Whether a row belongs between the loaded pages, rather than in a page not loaded yet */
|
|
2449
|
+
fitsLoadedRange = (key) => {
|
|
2436
2450
|
const row = this.collection.store.get(key);
|
|
2437
|
-
if (!row || !this.matchesSent(row)) return;
|
|
2438
|
-
if (!this.hasMore)
|
|
2439
|
-
this.inserted.add(key);
|
|
2440
|
-
return;
|
|
2441
|
-
}
|
|
2451
|
+
if (!row || !this.matchesSent(row)) return false;
|
|
2452
|
+
if (!this.hasMore) return true;
|
|
2442
2453
|
const compare = this.collection.createComparator(this.definition.orderBy, this);
|
|
2443
2454
|
const last = [...this.owned].toSorted(compare).pop();
|
|
2444
|
-
|
|
2455
|
+
return last !== void 0 && compare(key, last) < 0;
|
|
2445
2456
|
};
|
|
2446
2457
|
/** Loads the page after the last loaded one */
|
|
2447
2458
|
loadNext = () => {
|