@tanstack/db 0.0.12 → 0.0.13

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.
Files changed (47) hide show
  1. package/dist/cjs/SortedMap.cjs +38 -11
  2. package/dist/cjs/SortedMap.cjs.map +1 -1
  3. package/dist/cjs/SortedMap.d.cts +10 -0
  4. package/dist/cjs/collection.cjs +467 -95
  5. package/dist/cjs/collection.cjs.map +1 -1
  6. package/dist/cjs/collection.d.cts +80 -4
  7. package/dist/cjs/index.cjs +2 -0
  8. package/dist/cjs/index.cjs.map +1 -1
  9. package/dist/cjs/index.d.cts +1 -0
  10. package/dist/cjs/optimistic-action.cjs +21 -0
  11. package/dist/cjs/optimistic-action.cjs.map +1 -0
  12. package/dist/cjs/optimistic-action.d.cts +39 -0
  13. package/dist/cjs/query/compiled-query.cjs +21 -11
  14. package/dist/cjs/query/compiled-query.cjs.map +1 -1
  15. package/dist/cjs/query/query-builder.cjs +2 -2
  16. package/dist/cjs/query/query-builder.cjs.map +1 -1
  17. package/dist/cjs/transactions.cjs +3 -1
  18. package/dist/cjs/transactions.cjs.map +1 -1
  19. package/dist/cjs/types.d.cts +44 -0
  20. package/dist/esm/SortedMap.d.ts +10 -0
  21. package/dist/esm/SortedMap.js +38 -11
  22. package/dist/esm/SortedMap.js.map +1 -1
  23. package/dist/esm/collection.d.ts +80 -4
  24. package/dist/esm/collection.js +467 -95
  25. package/dist/esm/collection.js.map +1 -1
  26. package/dist/esm/index.d.ts +1 -0
  27. package/dist/esm/index.js +2 -0
  28. package/dist/esm/index.js.map +1 -1
  29. package/dist/esm/optimistic-action.d.ts +39 -0
  30. package/dist/esm/optimistic-action.js +21 -0
  31. package/dist/esm/optimistic-action.js.map +1 -0
  32. package/dist/esm/query/compiled-query.js +21 -11
  33. package/dist/esm/query/compiled-query.js.map +1 -1
  34. package/dist/esm/query/query-builder.js +2 -2
  35. package/dist/esm/query/query-builder.js.map +1 -1
  36. package/dist/esm/transactions.js +3 -1
  37. package/dist/esm/transactions.js.map +1 -1
  38. package/dist/esm/types.d.ts +44 -0
  39. package/package.json +1 -1
  40. package/src/SortedMap.ts +46 -13
  41. package/src/collection.ts +624 -119
  42. package/src/index.ts +1 -0
  43. package/src/optimistic-action.ts +65 -0
  44. package/src/query/compiled-query.ts +36 -14
  45. package/src/query/query-builder.ts +2 -2
  46. package/src/transactions.ts +6 -1
  47. package/src/types.ts +47 -0
@@ -1,9 +1,13 @@
1
1
  import { Store } from '@tanstack/store';
2
2
  import { Transaction } from './transactions.js';
3
3
  import { SortedMap } from './SortedMap.js';
4
- import { ChangeListener, ChangeMessage, CollectionConfig, Fn, InsertConfig, OperationConfig, ResolveType, Transaction as TransactionType, UtilsRecord } from './types.js';
4
+ import { ChangeListener, ChangeMessage, CollectionConfig, CollectionStatus, Fn, InsertConfig, OperationConfig, OptimisticChangeMessage, ResolveType, Transaction as TransactionType, UtilsRecord } from './types.js';
5
5
  import { StandardSchemaV1 } from '@standard-schema/spec';
6
6
  export declare const collectionsStore: Map<string, CollectionImpl<any, any>>;
7
+ interface PendingSyncedTransaction<T extends object = Record<string, unknown>> {
8
+ committed: boolean;
9
+ operations: Array<OptimisticChangeMessage<T>>;
10
+ }
7
11
  /**
8
12
  * Enhanced Collection interface that includes both data type T and utilities TUtils
9
13
  * @template T - The type of items in the collection
@@ -64,8 +68,10 @@ export declare class SchemaValidationError extends Error {
64
68
  }>, message?: string);
65
69
  }
66
70
  export declare class CollectionImpl<T extends object = Record<string, unknown>, TKey extends string | number = string | number> {
71
+ config: CollectionConfig<T, TKey, any>;
67
72
  transactions: SortedMap<string, Transaction<any>>;
68
- syncedData: Map<TKey, T>;
73
+ pendingSyncedTransactions: Array<PendingSyncedTransaction<T>>;
74
+ syncedData: Map<TKey, T> | SortedMap<TKey, T>;
69
75
  syncedMetadata: Map<TKey, unknown>;
70
76
  derivedUpserts: Map<TKey, T>;
71
77
  derivedDeletes: Set<TKey>;
@@ -73,11 +79,17 @@ export declare class CollectionImpl<T extends object = Record<string, unknown>,
73
79
  private changeListeners;
74
80
  private changeKeyListeners;
75
81
  utils: Record<string, Fn>;
76
- private pendingSyncedTransactions;
77
82
  private syncedKeys;
78
- config: CollectionConfig<T, TKey, any>;
83
+ private preSyncVisibleState;
84
+ private recentlySyncedKeys;
79
85
  private hasReceivedFirstCommit;
86
+ private isCommittingSyncTransactions;
80
87
  private onFirstCommitCallbacks;
88
+ private _status;
89
+ private activeSubscribersCount;
90
+ private gcTimeoutId;
91
+ private preloadPromise;
92
+ private syncCleanupFn;
81
93
  /**
82
94
  * Register a callback to be executed on the next commit
83
95
  * Useful for preloading collections
@@ -85,6 +97,25 @@ export declare class CollectionImpl<T extends object = Record<string, unknown>,
85
97
  */
86
98
  onFirstCommit(callback: () => void): void;
87
99
  id: string;
100
+ /**
101
+ * Gets the current status of the collection
102
+ */
103
+ get status(): CollectionStatus;
104
+ /**
105
+ * Validates that the collection is in a usable state for data operations
106
+ * @private
107
+ */
108
+ private validateCollectionUsable;
109
+ /**
110
+ * Validates state transitions to prevent invalid status changes
111
+ * @private
112
+ */
113
+ private validateStatusTransition;
114
+ /**
115
+ * Safely update the collection status with validation
116
+ * @private
117
+ */
118
+ private setStatus;
88
119
  /**
89
120
  * Creates a new Collection instance
90
121
  *
@@ -92,6 +123,44 @@ export declare class CollectionImpl<T extends object = Record<string, unknown>,
92
123
  * @throws Error if sync config is missing
93
124
  */
94
125
  constructor(config: CollectionConfig<T, TKey, any>);
126
+ /**
127
+ * Start sync immediately - internal method for compiled queries
128
+ * This bypasses lazy loading for special cases like live query results
129
+ */
130
+ startSyncImmediate(): void;
131
+ /**
132
+ * Start the sync process for this collection
133
+ * This is called when the collection is first accessed or preloaded
134
+ */
135
+ private startSync;
136
+ /**
137
+ * Preload the collection data by starting sync if not already started
138
+ * Multiple concurrent calls will share the same promise
139
+ */
140
+ preload(): Promise<void>;
141
+ /**
142
+ * Clean up the collection by stopping sync and clearing data
143
+ * This can be called manually or automatically by garbage collection
144
+ */
145
+ cleanup(): Promise<void>;
146
+ /**
147
+ * Start the garbage collection timer
148
+ * Called when the collection becomes inactive (no subscribers)
149
+ */
150
+ private startGCTimer;
151
+ /**
152
+ * Cancel the garbage collection timer
153
+ * Called when the collection becomes active again
154
+ */
155
+ private cancelGCTimer;
156
+ /**
157
+ * Increment the active subscribers count and start sync if needed
158
+ */
159
+ private addSubscriber;
160
+ /**
161
+ * Decrement the active subscribers count and start GC timer if needed
162
+ */
163
+ private removeSubscriber;
95
164
  /**
96
165
  * Recompute optimistic state from active transactions
97
166
  */
@@ -144,6 +213,7 @@ export declare class CollectionImpl<T extends object = Record<string, unknown>,
144
213
  private ensureStandardSchema;
145
214
  getKeyFromItem(item: T): TKey;
146
215
  generateGlobalKey(key: any, item: any): string;
216
+ private deepEqual;
147
217
  private validateData;
148
218
  /**
149
219
  * Inserts one or more items into the collection
@@ -268,6 +338,11 @@ export declare class CollectionImpl<T extends object = Record<string, unknown>,
268
338
  subscribeChangesKey(key: TKey, listener: ChangeListener<T, TKey>, { includeInitialState }?: {
269
339
  includeInitialState?: boolean;
270
340
  }): () => void;
341
+ /**
342
+ * Capture visible state for keys that will be affected by pending sync operations
343
+ * This must be called BEFORE onTransactionStateChange clears optimistic state
344
+ */
345
+ private capturePreSyncVisibleState;
271
346
  /**
272
347
  * Trigger a recomputation when transactions change
273
348
  * This method should be called by the Transaction class when state changes
@@ -290,3 +365,4 @@ export declare class CollectionImpl<T extends object = Record<string, unknown>,
290
365
  */
291
366
  asStoreArray(): Store<Array<T>>;
292
367
  }
368
+ export {};