@tanstack/svelte-db 0.5.4 → 0.5.7

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.
@@ -1,4 +1,4 @@
1
- import { assertLiveQueryWindowManyResult, compareLiveQueryWindowDependencies, createLiveQueryCollection, createLiveQueryWindowController, fetchNextLiveQueryWindowPage, getLiveQueryWindowCollectionWarning, normalizeLiveQueryWindowPageSize, resolveLiveQueryWindowInput, shouldPreserveLiveQueryWindowPageCount, } from '@tanstack/db';
1
+ import { assertLiveQueryWindowManyResult, compareLiveQueryWindowDependencies, createLiveQueryCollection, createLiveQueryWindowController, fetchNextLiveQueryWindowPage, getLiveQueryWindowCollectionWarning, liveQueryWindowMatches, normalizeLiveQueryWindowPageSize, resolveLiveQueryWindowInput, shouldPreserveLiveQueryWindowPageCount, } from '@tanstack/db';
2
2
  import { tick, untrack } from 'svelte';
3
3
  const DEFAULT_GC_TIME_MS = 1;
4
4
  export function useLiveInfiniteQuery(queryFnOrCollection, config, deps = []) {
@@ -37,6 +37,8 @@ export function useLiveInfiniteQuery(queryFnOrCollection, config, deps = []) {
37
37
  ? Math.max(1, previousController.getSnapshot().pages.length)
38
38
  : 1;
39
39
  const initialPageCount = canPreservePageCount ? previousPageCount : 1;
40
+ // The peek-ahead window for every retained page.
41
+ const requiredLimit = initialPageCount * pageSize + 1;
40
42
  previousInput = input;
41
43
  previousDependencies = [...dependencies];
42
44
  previousPageSize = pageSize;
@@ -49,6 +51,12 @@ export function useLiveInfiniteQuery(queryFnOrCollection, config, deps = []) {
49
51
  if (warning)
50
52
  console.warn(warning);
51
53
  }
54
+ // Like useLiveQuery, start a supplied collection during construction when
55
+ // its rows are already correct. A window the controller must still adjust
56
+ // waits for the subscription.
57
+ if (liveQueryWindowMatches(collection, requiredLimit)) {
58
+ collection.startSyncImmediate();
59
+ }
52
60
  const currentController = createLiveQueryWindowController(collection, {
53
61
  pageSize,
54
62
  initialPageParam,
@@ -58,11 +66,16 @@ export function useLiveInfiniteQuery(queryFnOrCollection, config, deps = []) {
58
66
  return currentController;
59
67
  }
60
68
  const collection = createLiveQueryCollection({
61
- query: input.query.limit(pageSize + 1).offset(0),
62
- startSync: false,
69
+ // Size the window for every retained page, so a collection that starts
70
+ // syncing now never publishes fewer rows than the controller's pages.
71
+ query: input.query.limit(requiredLimit).offset(0),
63
72
  gcTime: DEFAULT_GC_TIME_MS,
64
73
  });
65
74
  assertLiveQueryWindowManyResult(collection);
75
+ // Like useLiveQuery, start sync during construction once the query is
76
+ // valid. Construction reads only rows the sources already hold and sends
77
+ // no request until the subscribing effect attaches.
78
+ collection.startSyncImmediate();
66
79
  const currentController = createLiveQueryWindowController(collection, {
67
80
  pageSize,
68
81
  initialPageParam,
@@ -4,6 +4,12 @@ import { untrack } from 'svelte';
4
4
  import { SvelteMap } from 'svelte/reactivity';
5
5
  import { BaseQueryBuilder, UnhashableQueryIRError, createLiveQueryCollection, createLiveQueryObserver, getLiveQueryHash, getPublicCollection, getStableValueHash, isCollection, isSingleResultCollection, prepareLiveQueryValue, resolveLiveQueryValue, } from '@tanstack/db';
6
6
  import { useOptionalDbClient } from './db-context.js';
7
+ /** Copy a snapshot's ordered rows; a single-result query's `data` is one row. */
8
+ function snapshotRows(data) {
9
+ if (Array.isArray(data))
10
+ return [...data];
11
+ return data === undefined ? [] : [data];
12
+ }
7
13
  function toValue(value) {
8
14
  if (typeof value === `function`) {
9
15
  return value();
@@ -118,7 +124,7 @@ export function useLiveQuery(configOrQueryOrCollection, deps = []) {
118
124
  // Reactive state that gets updated granularly through change events
119
125
  const state = new SvelteMap(initialSnapshot.state ?? []);
120
126
  // Reactive data array that maintains sorted order
121
- let internalData = $state(Array.from(initialSnapshot.state?.values() ?? []));
127
+ let internalData = $state(snapshotRows(initialSnapshot.data));
122
128
  // Track collection status reactively
123
129
  let status = $state(initialSnapshot.status);
124
130
  let persistedStatus = $state(initialSnapshot.persistedStatus);
@@ -148,7 +154,7 @@ export function useLiveQuery(configOrQueryOrCollection, deps = []) {
148
154
  state.set(key, value);
149
155
  }
150
156
  }
151
- internalData = Array.from(snapshot.state?.values() ?? []);
157
+ internalData = snapshotRows(snapshot.data);
152
158
  });
153
159
  };
154
160
  // Watch for collection changes and subscribe to updates
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tanstack/svelte-db",
3
- "version": "0.5.4",
3
+ "version": "0.5.7",
4
4
  "description": "Svelte integration for @tanstack/db",
5
5
  "author": "Kyle Mathews",
6
6
  "license": "MIT",
@@ -35,7 +35,7 @@
35
35
  "skills"
36
36
  ],
37
37
  "dependencies": {
38
- "@tanstack/db": "0.12.0"
38
+ "@tanstack/db": "0.12.3"
39
39
  },
40
40
  "peerDependencies": {
41
41
  "svelte": "^5.0.0"