@tanstack/db 0.0.14 → 0.0.15

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 (198) hide show
  1. package/dist/cjs/collection.cjs +117 -104
  2. package/dist/cjs/collection.cjs.map +1 -1
  3. package/dist/cjs/collection.d.cts +18 -21
  4. package/dist/cjs/index.cjs +35 -13
  5. package/dist/cjs/index.cjs.map +1 -1
  6. package/dist/cjs/index.d.cts +0 -1
  7. package/dist/cjs/query/builder/functions.cjs +107 -0
  8. package/dist/cjs/query/builder/functions.cjs.map +1 -0
  9. package/dist/cjs/query/builder/functions.d.cts +38 -0
  10. package/dist/cjs/query/builder/index.cjs +499 -0
  11. package/dist/cjs/query/builder/index.cjs.map +1 -0
  12. package/dist/cjs/query/builder/index.d.cts +324 -0
  13. package/dist/cjs/query/builder/ref-proxy.cjs +96 -0
  14. package/dist/cjs/query/builder/ref-proxy.cjs.map +1 -0
  15. package/dist/cjs/query/builder/ref-proxy.d.cts +28 -0
  16. package/dist/cjs/query/builder/types.d.cts +80 -0
  17. package/dist/cjs/query/compiler/evaluators.cjs +261 -0
  18. package/dist/cjs/query/compiler/evaluators.cjs.map +1 -0
  19. package/dist/cjs/query/compiler/evaluators.d.cts +11 -0
  20. package/dist/cjs/query/compiler/group-by.cjs +271 -0
  21. package/dist/cjs/query/compiler/group-by.cjs.map +1 -0
  22. package/dist/cjs/query/compiler/group-by.d.cts +7 -0
  23. package/dist/cjs/query/compiler/index.cjs +181 -0
  24. package/dist/cjs/query/compiler/index.cjs.map +1 -0
  25. package/dist/cjs/query/compiler/index.d.cts +15 -0
  26. package/dist/cjs/query/compiler/joins.cjs +116 -0
  27. package/dist/cjs/query/compiler/joins.cjs.map +1 -0
  28. package/dist/cjs/query/compiler/joins.d.cts +11 -0
  29. package/dist/cjs/query/compiler/order-by.cjs +89 -0
  30. package/dist/cjs/query/compiler/order-by.cjs.map +1 -0
  31. package/dist/cjs/query/compiler/order-by.d.cts +9 -0
  32. package/dist/cjs/query/compiler/select.cjs +57 -0
  33. package/dist/cjs/query/compiler/select.cjs.map +1 -0
  34. package/dist/cjs/query/compiler/select.d.cts +15 -0
  35. package/dist/cjs/query/index.d.cts +6 -5
  36. package/dist/cjs/query/ir.cjs +57 -0
  37. package/dist/cjs/query/ir.cjs.map +1 -0
  38. package/dist/cjs/query/ir.d.cts +81 -0
  39. package/dist/cjs/query/live-query-collection.cjs +224 -0
  40. package/dist/cjs/query/live-query-collection.cjs.map +1 -0
  41. package/dist/cjs/query/live-query-collection.d.cts +124 -0
  42. package/dist/cjs/transactions.cjs +20 -13
  43. package/dist/cjs/transactions.cjs.map +1 -1
  44. package/dist/cjs/transactions.d.cts +10 -1
  45. package/dist/cjs/types.d.cts +13 -0
  46. package/dist/esm/collection.d.ts +18 -21
  47. package/dist/esm/collection.js +118 -105
  48. package/dist/esm/collection.js.map +1 -1
  49. package/dist/esm/index.d.ts +0 -1
  50. package/dist/esm/index.js +34 -12
  51. package/dist/esm/index.js.map +1 -1
  52. package/dist/esm/query/builder/functions.d.ts +38 -0
  53. package/dist/esm/query/builder/functions.js +107 -0
  54. package/dist/esm/query/builder/functions.js.map +1 -0
  55. package/dist/esm/query/builder/index.d.ts +324 -0
  56. package/dist/esm/query/builder/index.js +499 -0
  57. package/dist/esm/query/builder/index.js.map +1 -0
  58. package/dist/esm/query/builder/ref-proxy.d.ts +28 -0
  59. package/dist/esm/query/builder/ref-proxy.js +96 -0
  60. package/dist/esm/query/builder/ref-proxy.js.map +1 -0
  61. package/dist/esm/query/builder/types.d.ts +80 -0
  62. package/dist/esm/query/compiler/evaluators.d.ts +11 -0
  63. package/dist/esm/query/compiler/evaluators.js +261 -0
  64. package/dist/esm/query/compiler/evaluators.js.map +1 -0
  65. package/dist/esm/query/compiler/group-by.d.ts +7 -0
  66. package/dist/esm/query/compiler/group-by.js +271 -0
  67. package/dist/esm/query/compiler/group-by.js.map +1 -0
  68. package/dist/esm/query/compiler/index.d.ts +15 -0
  69. package/dist/esm/query/compiler/index.js +181 -0
  70. package/dist/esm/query/compiler/index.js.map +1 -0
  71. package/dist/esm/query/compiler/joins.d.ts +11 -0
  72. package/dist/esm/query/compiler/joins.js +116 -0
  73. package/dist/esm/query/compiler/joins.js.map +1 -0
  74. package/dist/esm/query/compiler/order-by.d.ts +9 -0
  75. package/dist/esm/query/compiler/order-by.js +89 -0
  76. package/dist/esm/query/compiler/order-by.js.map +1 -0
  77. package/dist/esm/query/compiler/select.d.ts +15 -0
  78. package/dist/esm/query/compiler/select.js +57 -0
  79. package/dist/esm/query/compiler/select.js.map +1 -0
  80. package/dist/esm/query/index.d.ts +6 -5
  81. package/dist/esm/query/ir.d.ts +81 -0
  82. package/dist/esm/query/ir.js +57 -0
  83. package/dist/esm/query/ir.js.map +1 -0
  84. package/dist/esm/query/live-query-collection.d.ts +124 -0
  85. package/dist/esm/query/live-query-collection.js +224 -0
  86. package/dist/esm/query/live-query-collection.js.map +1 -0
  87. package/dist/esm/transactions.d.ts +10 -1
  88. package/dist/esm/transactions.js +20 -13
  89. package/dist/esm/transactions.js.map +1 -1
  90. package/dist/esm/types.d.ts +13 -0
  91. package/package.json +3 -4
  92. package/src/collection.ts +152 -129
  93. package/src/index.ts +0 -1
  94. package/src/query/builder/functions.ts +267 -0
  95. package/src/query/builder/index.ts +648 -0
  96. package/src/query/builder/ref-proxy.ts +156 -0
  97. package/src/query/builder/types.ts +278 -0
  98. package/src/query/compiler/evaluators.ts +315 -0
  99. package/src/query/compiler/group-by.ts +428 -0
  100. package/src/query/compiler/index.ts +276 -0
  101. package/src/query/compiler/joins.ts +228 -0
  102. package/src/query/compiler/order-by.ts +139 -0
  103. package/src/query/compiler/select.ts +173 -0
  104. package/src/query/index.ts +64 -5
  105. package/src/query/ir.ts +128 -0
  106. package/src/query/live-query-collection.ts +509 -0
  107. package/src/transactions.ts +27 -16
  108. package/src/types.ts +15 -0
  109. package/dist/cjs/query/compiled-query.cjs +0 -160
  110. package/dist/cjs/query/compiled-query.cjs.map +0 -1
  111. package/dist/cjs/query/compiled-query.d.cts +0 -20
  112. package/dist/cjs/query/evaluators.cjs +0 -161
  113. package/dist/cjs/query/evaluators.cjs.map +0 -1
  114. package/dist/cjs/query/evaluators.d.cts +0 -14
  115. package/dist/cjs/query/extractors.cjs +0 -122
  116. package/dist/cjs/query/extractors.cjs.map +0 -1
  117. package/dist/cjs/query/extractors.d.cts +0 -22
  118. package/dist/cjs/query/functions.cjs +0 -152
  119. package/dist/cjs/query/functions.cjs.map +0 -1
  120. package/dist/cjs/query/functions.d.cts +0 -21
  121. package/dist/cjs/query/group-by.cjs +0 -88
  122. package/dist/cjs/query/group-by.cjs.map +0 -1
  123. package/dist/cjs/query/group-by.d.cts +0 -40
  124. package/dist/cjs/query/joins.cjs +0 -141
  125. package/dist/cjs/query/joins.cjs.map +0 -1
  126. package/dist/cjs/query/joins.d.cts +0 -14
  127. package/dist/cjs/query/order-by.cjs +0 -185
  128. package/dist/cjs/query/order-by.cjs.map +0 -1
  129. package/dist/cjs/query/order-by.d.cts +0 -3
  130. package/dist/cjs/query/pipeline-compiler.cjs +0 -89
  131. package/dist/cjs/query/pipeline-compiler.cjs.map +0 -1
  132. package/dist/cjs/query/pipeline-compiler.d.cts +0 -10
  133. package/dist/cjs/query/query-builder.cjs +0 -307
  134. package/dist/cjs/query/query-builder.cjs.map +0 -1
  135. package/dist/cjs/query/query-builder.d.cts +0 -225
  136. package/dist/cjs/query/schema.d.cts +0 -100
  137. package/dist/cjs/query/select.cjs +0 -130
  138. package/dist/cjs/query/select.cjs.map +0 -1
  139. package/dist/cjs/query/select.d.cts +0 -3
  140. package/dist/cjs/query/types.d.cts +0 -189
  141. package/dist/cjs/query/utils.cjs +0 -154
  142. package/dist/cjs/query/utils.cjs.map +0 -1
  143. package/dist/cjs/query/utils.d.cts +0 -37
  144. package/dist/cjs/utils.cjs +0 -17
  145. package/dist/cjs/utils.cjs.map +0 -1
  146. package/dist/cjs/utils.d.cts +0 -3
  147. package/dist/esm/query/compiled-query.d.ts +0 -20
  148. package/dist/esm/query/compiled-query.js +0 -160
  149. package/dist/esm/query/compiled-query.js.map +0 -1
  150. package/dist/esm/query/evaluators.d.ts +0 -14
  151. package/dist/esm/query/evaluators.js +0 -161
  152. package/dist/esm/query/evaluators.js.map +0 -1
  153. package/dist/esm/query/extractors.d.ts +0 -22
  154. package/dist/esm/query/extractors.js +0 -122
  155. package/dist/esm/query/extractors.js.map +0 -1
  156. package/dist/esm/query/functions.d.ts +0 -21
  157. package/dist/esm/query/functions.js +0 -152
  158. package/dist/esm/query/functions.js.map +0 -1
  159. package/dist/esm/query/group-by.d.ts +0 -40
  160. package/dist/esm/query/group-by.js +0 -88
  161. package/dist/esm/query/group-by.js.map +0 -1
  162. package/dist/esm/query/joins.d.ts +0 -14
  163. package/dist/esm/query/joins.js +0 -141
  164. package/dist/esm/query/joins.js.map +0 -1
  165. package/dist/esm/query/order-by.d.ts +0 -3
  166. package/dist/esm/query/order-by.js +0 -185
  167. package/dist/esm/query/order-by.js.map +0 -1
  168. package/dist/esm/query/pipeline-compiler.d.ts +0 -10
  169. package/dist/esm/query/pipeline-compiler.js +0 -89
  170. package/dist/esm/query/pipeline-compiler.js.map +0 -1
  171. package/dist/esm/query/query-builder.d.ts +0 -225
  172. package/dist/esm/query/query-builder.js +0 -307
  173. package/dist/esm/query/query-builder.js.map +0 -1
  174. package/dist/esm/query/schema.d.ts +0 -100
  175. package/dist/esm/query/select.d.ts +0 -3
  176. package/dist/esm/query/select.js +0 -130
  177. package/dist/esm/query/select.js.map +0 -1
  178. package/dist/esm/query/types.d.ts +0 -189
  179. package/dist/esm/query/utils.d.ts +0 -37
  180. package/dist/esm/query/utils.js +0 -154
  181. package/dist/esm/query/utils.js.map +0 -1
  182. package/dist/esm/utils.d.ts +0 -3
  183. package/dist/esm/utils.js +0 -17
  184. package/dist/esm/utils.js.map +0 -1
  185. package/src/query/compiled-query.ts +0 -234
  186. package/src/query/evaluators.ts +0 -250
  187. package/src/query/extractors.ts +0 -214
  188. package/src/query/functions.ts +0 -297
  189. package/src/query/group-by.ts +0 -139
  190. package/src/query/joins.ts +0 -260
  191. package/src/query/order-by.ts +0 -264
  192. package/src/query/pipeline-compiler.ts +0 -149
  193. package/src/query/query-builder.ts +0 -902
  194. package/src/query/schema.ts +0 -268
  195. package/src/query/select.ts +0 -208
  196. package/src/query/types.ts +0 -418
  197. package/src/query/utils.ts +0 -245
  198. package/src/utils.ts +0 -15
@@ -1,6 +1,5 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
- const store = require("@tanstack/store");
4
3
  const proxy = require("./proxy.cjs");
5
4
  const transactions = require("./transactions.cjs");
6
5
  const SortedMap = require("./SortedMap.cjs");
@@ -34,8 +33,8 @@ class CollectionImpl {
34
33
  constructor(config) {
35
34
  this.pendingSyncedTransactions = [];
36
35
  this.syncedMetadata = /* @__PURE__ */ new Map();
37
- this.derivedUpserts = /* @__PURE__ */ new Map();
38
- this.derivedDeletes = /* @__PURE__ */ new Set();
36
+ this.optimisticUpserts = /* @__PURE__ */ new Map();
37
+ this.optimisticDeletes = /* @__PURE__ */ new Set();
39
38
  this._size = 0;
40
39
  this.changeListeners = /* @__PURE__ */ new Set();
41
40
  this.changeKeyListeners = /* @__PURE__ */ new Map();
@@ -46,6 +45,8 @@ class CollectionImpl {
46
45
  this.hasReceivedFirstCommit = false;
47
46
  this.isCommittingSyncTransactions = false;
48
47
  this.onFirstCommitCallbacks = [];
48
+ this.batchedEvents = [];
49
+ this.shouldBatchEvents = false;
49
50
  this._status = `idle`;
50
51
  this.activeSubscribersCount = 0;
51
52
  this.gcTimeoutId = null;
@@ -79,6 +80,7 @@ class CollectionImpl {
79
80
  }
80
81
  }
81
82
  const events = [];
83
+ const rowUpdateMode = this.config.sync.rowUpdateMode || `partial`;
82
84
  for (const transaction of this.pendingSyncedTransactions) {
83
85
  for (const operation of transaction.operations) {
84
86
  const key = operation.key;
@@ -106,12 +108,16 @@ class CollectionImpl {
106
108
  this.syncedData.set(key, operation.value);
107
109
  break;
108
110
  case `update`: {
109
- const updatedValue = Object.assign(
110
- {},
111
- this.syncedData.get(key),
112
- operation.value
113
- );
114
- this.syncedData.set(key, updatedValue);
111
+ if (rowUpdateMode === `partial`) {
112
+ const updatedValue = Object.assign(
113
+ {},
114
+ this.syncedData.get(key),
115
+ operation.value
116
+ );
117
+ this.syncedData.set(key, updatedValue);
118
+ } else {
119
+ this.syncedData.set(key, operation.value);
120
+ }
115
121
  break;
116
122
  }
117
123
  case `delete`:
@@ -120,8 +126,8 @@ class CollectionImpl {
120
126
  }
121
127
  }
122
128
  }
123
- this.derivedUpserts.clear();
124
- this.derivedDeletes.clear();
129
+ this.optimisticUpserts.clear();
130
+ this.optimisticDeletes.clear();
125
131
  this.isCommittingSyncTransactions = false;
126
132
  for (const transaction of this.transactions.values()) {
127
133
  if (![`completed`, `failed`].includes(transaction.state)) {
@@ -130,12 +136,15 @@ class CollectionImpl {
130
136
  switch (mutation.type) {
131
137
  case `insert`:
132
138
  case `update`:
133
- this.derivedUpserts.set(mutation.key, mutation.modified);
134
- this.derivedDeletes.delete(mutation.key);
139
+ this.optimisticUpserts.set(
140
+ mutation.key,
141
+ mutation.modified
142
+ );
143
+ this.optimisticDeletes.delete(mutation.key);
135
144
  break;
136
145
  case `delete`:
137
- this.derivedUpserts.delete(mutation.key);
138
- this.derivedDeletes.add(mutation.key);
146
+ this.optimisticUpserts.delete(mutation.key);
147
+ this.optimisticDeletes.add(mutation.key);
139
148
  break;
140
149
  }
141
150
  }
@@ -184,7 +193,7 @@ class CollectionImpl {
184
193
  }
185
194
  }
186
195
  this._size = this.calculateSize();
187
- this.emitEvents(events);
196
+ this.emitEvents(events, true);
188
197
  this.pendingSyncedTransactions = [];
189
198
  this.preSyncVisibleState.clear();
190
199
  Promise.resolve().then(() => {
@@ -238,7 +247,7 @@ class CollectionImpl {
238
247
  this.recomputeOptimisticState();
239
248
  return ambientTransaction;
240
249
  } else {
241
- const directOpTransaction = new transactions.Transaction({
250
+ const directOpTransaction = transactions.createTransaction({
242
251
  mutationFn: async (params) => {
243
252
  return this.config.onInsert(params);
244
253
  }
@@ -292,7 +301,7 @@ class CollectionImpl {
292
301
  this.recomputeOptimisticState();
293
302
  return ambientTransaction;
294
303
  }
295
- const directOpTransaction = new transactions.Transaction({
304
+ const directOpTransaction = transactions.createTransaction({
296
305
  autoCommit: true,
297
306
  mutationFn: async (params) => {
298
307
  return this.config.onDelete(params);
@@ -316,7 +325,7 @@ class CollectionImpl {
316
325
  throw new Error(`Collection requires a sync config`);
317
326
  }
318
327
  this.transactions = new SortedMap.SortedMap(
319
- (a, b) => a.createdAt.getTime() - b.createdAt.getTime()
328
+ (a, b) => a.compareCreatedAt(b)
320
329
  );
321
330
  this.config = config;
322
331
  collectionsStore.set(this.id, this);
@@ -450,10 +459,10 @@ class CollectionImpl {
450
459
  );
451
460
  }
452
461
  pendingTransaction.committed = true;
453
- this.commitPendingTransactions();
454
462
  if (this._status === `loading`) {
455
463
  this.setStatus(`ready`);
456
464
  }
465
+ this.commitPendingTransactions();
457
466
  }
458
467
  });
459
468
  this.syncCleanupFn = typeof cleanupFn === `function` ? cleanupFn : null;
@@ -525,14 +534,16 @@ class CollectionImpl {
525
534
  }
526
535
  this.syncedData.clear();
527
536
  this.syncedMetadata.clear();
528
- this.derivedUpserts.clear();
529
- this.derivedDeletes.clear();
537
+ this.optimisticUpserts.clear();
538
+ this.optimisticDeletes.clear();
530
539
  this._size = 0;
531
540
  this.pendingSyncedTransactions = [];
532
541
  this.syncedKeys.clear();
533
542
  this.hasReceivedFirstCommit = false;
534
543
  this.onFirstCommitCallbacks = [];
535
544
  this.preloadPromise = null;
545
+ this.batchedEvents = [];
546
+ this.shouldBatchEvents = false;
536
547
  this.setStatus(`cleaned-up`);
537
548
  return Promise.resolve();
538
549
  }
@@ -592,10 +603,10 @@ class CollectionImpl {
592
603
  if (this.isCommittingSyncTransactions) {
593
604
  return;
594
605
  }
595
- const previousState = new Map(this.derivedUpserts);
596
- const previousDeletes = new Set(this.derivedDeletes);
597
- this.derivedUpserts.clear();
598
- this.derivedDeletes.clear();
606
+ const previousState = new Map(this.optimisticUpserts);
607
+ const previousDeletes = new Set(this.optimisticDeletes);
608
+ this.optimisticUpserts.clear();
609
+ this.optimisticDeletes.clear();
599
610
  const activeTransactions = [];
600
611
  const completedTransactions = [];
601
612
  for (const transaction of this.transactions.values()) {
@@ -611,12 +622,12 @@ class CollectionImpl {
611
622
  switch (mutation.type) {
612
623
  case `insert`:
613
624
  case `update`:
614
- this.derivedUpserts.set(mutation.key, mutation.modified);
615
- this.derivedDeletes.delete(mutation.key);
625
+ this.optimisticUpserts.set(mutation.key, mutation.modified);
626
+ this.optimisticDeletes.delete(mutation.key);
616
627
  break;
617
628
  case `delete`:
618
- this.derivedUpserts.delete(mutation.key);
619
- this.derivedDeletes.add(mutation.key);
629
+ this.optimisticUpserts.delete(mutation.key);
630
+ this.optimisticDeletes.add(mutation.key);
620
631
  break;
621
632
  }
622
633
  }
@@ -666,10 +677,10 @@ class CollectionImpl {
666
677
  */
667
678
  calculateSize() {
668
679
  const syncedSize = this.syncedData.size;
669
- const deletesFromSynced = Array.from(this.derivedDeletes).filter(
670
- (key) => this.syncedData.has(key) && !this.derivedUpserts.has(key)
680
+ const deletesFromSynced = Array.from(this.optimisticDeletes).filter(
681
+ (key) => this.syncedData.has(key) && !this.optimisticUpserts.has(key)
671
682
  ).length;
672
- const upsertsNotInSynced = Array.from(this.derivedUpserts.keys()).filter(
683
+ const upsertsNotInSynced = Array.from(this.optimisticUpserts.keys()).filter(
673
684
  (key) => !this.syncedData.has(key)
674
685
  ).length;
675
686
  return syncedSize - deletesFromSynced + upsertsNotInSynced;
@@ -680,9 +691,9 @@ class CollectionImpl {
680
691
  collectOptimisticChanges(previousUpserts, previousDeletes, events) {
681
692
  const allKeys = /* @__PURE__ */ new Set([
682
693
  ...previousUpserts.keys(),
683
- ...this.derivedUpserts.keys(),
694
+ ...this.optimisticUpserts.keys(),
684
695
  ...previousDeletes,
685
- ...this.derivedDeletes
696
+ ...this.optimisticDeletes
686
697
  ]);
687
698
  for (const key of allKeys) {
688
699
  const currentValue = this.get(key);
@@ -718,28 +729,39 @@ class CollectionImpl {
718
729
  return this.syncedData.get(key);
719
730
  }
720
731
  /**
721
- * Emit multiple events at once to all listeners
732
+ * Emit events either immediately or batch them for later emission
722
733
  */
723
- emitEvents(changes) {
724
- if (changes.length > 0) {
725
- for (const listener of this.changeListeners) {
726
- listener(changes);
734
+ emitEvents(changes, endBatching = false) {
735
+ if (this.shouldBatchEvents && !endBatching) {
736
+ this.batchedEvents.push(...changes);
737
+ return;
738
+ }
739
+ let eventsToEmit = changes;
740
+ if (endBatching) {
741
+ if (this.batchedEvents.length > 0) {
742
+ eventsToEmit = [...this.batchedEvents, ...changes];
727
743
  }
728
- if (this.changeKeyListeners.size > 0) {
729
- const changesByKey = /* @__PURE__ */ new Map();
730
- for (const change of changes) {
731
- if (this.changeKeyListeners.has(change.key)) {
732
- if (!changesByKey.has(change.key)) {
733
- changesByKey.set(change.key, []);
734
- }
735
- changesByKey.get(change.key).push(change);
744
+ this.batchedEvents = [];
745
+ this.shouldBatchEvents = false;
746
+ }
747
+ if (eventsToEmit.length === 0) return;
748
+ for (const listener of this.changeListeners) {
749
+ listener(eventsToEmit);
750
+ }
751
+ if (this.changeKeyListeners.size > 0) {
752
+ const changesByKey = /* @__PURE__ */ new Map();
753
+ for (const change of eventsToEmit) {
754
+ if (this.changeKeyListeners.has(change.key)) {
755
+ if (!changesByKey.has(change.key)) {
756
+ changesByKey.set(change.key, []);
736
757
  }
758
+ changesByKey.get(change.key).push(change);
737
759
  }
738
- for (const [key, keyChanges] of changesByKey) {
739
- const keyListeners = this.changeKeyListeners.get(key);
740
- for (const listener of keyListeners) {
741
- listener(keyChanges);
742
- }
760
+ }
761
+ for (const [key, keyChanges] of changesByKey) {
762
+ const keyListeners = this.changeKeyListeners.get(key);
763
+ for (const listener of keyListeners) {
764
+ listener(keyChanges);
743
765
  }
744
766
  }
745
767
  }
@@ -748,11 +770,11 @@ class CollectionImpl {
748
770
  * Get the current value for a key (virtual derived state)
749
771
  */
750
772
  get(key) {
751
- if (this.derivedDeletes.has(key)) {
773
+ if (this.optimisticDeletes.has(key)) {
752
774
  return void 0;
753
775
  }
754
- if (this.derivedUpserts.has(key)) {
755
- return this.derivedUpserts.get(key);
776
+ if (this.optimisticUpserts.has(key)) {
777
+ return this.optimisticUpserts.get(key);
756
778
  }
757
779
  return this.syncedData.get(key);
758
780
  }
@@ -760,10 +782,10 @@ class CollectionImpl {
760
782
  * Check if a key exists in the collection (virtual derived state)
761
783
  */
762
784
  has(key) {
763
- if (this.derivedDeletes.has(key)) {
785
+ if (this.optimisticDeletes.has(key)) {
764
786
  return false;
765
787
  }
766
- if (this.derivedUpserts.has(key)) {
788
+ if (this.optimisticUpserts.has(key)) {
767
789
  return true;
768
790
  }
769
791
  return this.syncedData.has(key);
@@ -779,12 +801,12 @@ class CollectionImpl {
779
801
  */
780
802
  *keys() {
781
803
  for (const key of this.syncedData.keys()) {
782
- if (!this.derivedDeletes.has(key)) {
804
+ if (!this.optimisticDeletes.has(key)) {
783
805
  yield key;
784
806
  }
785
807
  }
786
- for (const key of this.derivedUpserts.keys()) {
787
- if (!this.syncedData.has(key) && !this.derivedDeletes.has(key)) {
808
+ for (const key of this.optimisticUpserts.keys()) {
809
+ if (!this.syncedData.has(key) && !this.optimisticDeletes.has(key)) {
788
810
  yield key;
789
811
  }
790
812
  }
@@ -796,8 +818,7 @@ class CollectionImpl {
796
818
  for (const key of this.keys()) {
797
819
  const value = this.get(key);
798
820
  if (value !== void 0) {
799
- const { _orderByIndex, ...copy } = value;
800
- yield copy;
821
+ yield value;
801
822
  }
802
823
  }
803
824
  }
@@ -808,11 +829,38 @@ class CollectionImpl {
808
829
  for (const key of this.keys()) {
809
830
  const value = this.get(key);
810
831
  if (value !== void 0) {
811
- const { _orderByIndex, ...copy } = value;
812
- yield [key, copy];
832
+ yield [key, value];
813
833
  }
814
834
  }
815
835
  }
836
+ /**
837
+ * Get all entries (virtual derived state)
838
+ */
839
+ *[Symbol.iterator]() {
840
+ for (const [key, value] of this.entries()) {
841
+ yield [key, value];
842
+ }
843
+ }
844
+ /**
845
+ * Execute a callback for each entry in the collection
846
+ */
847
+ forEach(callbackfn) {
848
+ let index = 0;
849
+ for (const [key, value] of this.entries()) {
850
+ callbackfn(value, key, index++);
851
+ }
852
+ }
853
+ /**
854
+ * Create a new array with the results of calling a function for each entry in the collection
855
+ */
856
+ map(callbackfn) {
857
+ const result = [];
858
+ let index = 0;
859
+ for (const [key, value] of this.entries()) {
860
+ result.push(callbackfn(value, key, index++));
861
+ }
862
+ return result;
863
+ }
816
864
  ensureStandardSchema(schema) {
817
865
  if (schema && typeof schema === `object` && `~standard` in schema) {
818
866
  return schema;
@@ -970,7 +1018,7 @@ class CollectionImpl {
970
1018
  };
971
1019
  }).filter(Boolean);
972
1020
  if (mutations.length === 0) {
973
- const emptyTransaction = new transactions.Transaction({
1021
+ const emptyTransaction = transactions.createTransaction({
974
1022
  mutationFn: async () => {
975
1023
  }
976
1024
  });
@@ -983,7 +1031,7 @@ class CollectionImpl {
983
1031
  this.recomputeOptimisticState();
984
1032
  return ambientTransaction;
985
1033
  }
986
- const directOpTransaction = new transactions.Transaction({
1034
+ const directOpTransaction = transactions.createTransaction({
987
1035
  mutationFn: async (params) => {
988
1036
  return this.config.onUpdate(params);
989
1037
  }
@@ -1028,13 +1076,7 @@ class CollectionImpl {
1028
1076
  * @returns An Array containing all items in the collection
1029
1077
  */
1030
1078
  get toArray() {
1031
- const array = Array.from(this.values());
1032
- if (array[0] && array[0]._orderByIndex) {
1033
- return array.sort(
1034
- (a, b) => a._orderByIndex - b._orderByIndex
1035
- );
1036
- }
1037
- return array;
1079
+ return Array.from(this.values());
1038
1080
  }
1039
1081
  /**
1040
1082
  * Gets the current state of the collection as an Array, but only resolves when data is available
@@ -1136,39 +1178,10 @@ class CollectionImpl {
1136
1178
  * This method should be called by the Transaction class when state changes
1137
1179
  */
1138
1180
  onTransactionStateChange() {
1181
+ this.shouldBatchEvents = this.pendingSyncedTransactions.length > 0;
1139
1182
  this.capturePreSyncVisibleState();
1140
1183
  this.recomputeOptimisticState();
1141
1184
  }
1142
- /**
1143
- * Returns a Tanstack Store Map that is updated when the collection changes
1144
- * This is a temporary solution to enable the existing framework hooks to work
1145
- * with the new internals of Collection until they are rewritten.
1146
- * TODO: Remove this once the framework hooks are rewritten.
1147
- */
1148
- asStoreMap() {
1149
- if (!this._storeMap) {
1150
- this._storeMap = new store.Store(new Map(this.entries()));
1151
- this.changeListeners.add(() => {
1152
- this._storeMap.setState(() => new Map(this.entries()));
1153
- });
1154
- }
1155
- return this._storeMap;
1156
- }
1157
- /**
1158
- * Returns a Tanstack Store Array that is updated when the collection changes
1159
- * This is a temporary solution to enable the existing framework hooks to work
1160
- * with the new internals of Collection until they are rewritten.
1161
- * TODO: Remove this once the framework hooks are rewritten.
1162
- */
1163
- asStoreArray() {
1164
- if (!this._storeArray) {
1165
- this._storeArray = new store.Store(this.toArray);
1166
- this.changeListeners.add(() => {
1167
- this._storeArray.setState(() => this.toArray);
1168
- });
1169
- }
1170
- return this._storeArray;
1171
- }
1172
1185
  }
1173
1186
  exports.CollectionImpl = CollectionImpl;
1174
1187
  exports.SchemaValidationError = SchemaValidationError;