@tanstack/db 0.0.30 → 0.0.31
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/dist/cjs/collection.cjs +24 -20
- package/dist/cjs/collection.cjs.map +1 -1
- package/dist/esm/collection.js +24 -20
- package/dist/esm/collection.js.map +1 -1
- package/package.json +1 -1
- package/src/collection.ts +36 -22
package/dist/cjs/collection.cjs
CHANGED
|
@@ -255,7 +255,7 @@ class CollectionImpl {
|
|
|
255
255
|
if (ambientTransaction) {
|
|
256
256
|
ambientTransaction.applyMutations(mutations);
|
|
257
257
|
this.transactions.set(ambientTransaction.id, ambientTransaction);
|
|
258
|
-
this.recomputeOptimisticState();
|
|
258
|
+
this.recomputeOptimisticState(true);
|
|
259
259
|
return ambientTransaction;
|
|
260
260
|
} else {
|
|
261
261
|
const directOpTransaction = transactions.createTransaction({
|
|
@@ -269,7 +269,7 @@ class CollectionImpl {
|
|
|
269
269
|
directOpTransaction.applyMutations(mutations);
|
|
270
270
|
directOpTransaction.commit();
|
|
271
271
|
this.transactions.set(directOpTransaction.id, directOpTransaction);
|
|
272
|
-
this.recomputeOptimisticState();
|
|
272
|
+
this.recomputeOptimisticState(true);
|
|
273
273
|
return directOpTransaction;
|
|
274
274
|
}
|
|
275
275
|
};
|
|
@@ -309,7 +309,7 @@ class CollectionImpl {
|
|
|
309
309
|
if (ambientTransaction) {
|
|
310
310
|
ambientTransaction.applyMutations(mutations);
|
|
311
311
|
this.transactions.set(ambientTransaction.id, ambientTransaction);
|
|
312
|
-
this.recomputeOptimisticState();
|
|
312
|
+
this.recomputeOptimisticState(true);
|
|
313
313
|
return ambientTransaction;
|
|
314
314
|
}
|
|
315
315
|
const directOpTransaction = transactions.createTransaction({
|
|
@@ -324,7 +324,7 @@ class CollectionImpl {
|
|
|
324
324
|
directOpTransaction.applyMutations(mutations);
|
|
325
325
|
directOpTransaction.commit();
|
|
326
326
|
this.transactions.set(directOpTransaction.id, directOpTransaction);
|
|
327
|
-
this.recomputeOptimisticState();
|
|
327
|
+
this.recomputeOptimisticState(true);
|
|
328
328
|
return directOpTransaction;
|
|
329
329
|
};
|
|
330
330
|
if (!config) {
|
|
@@ -653,7 +653,7 @@ class CollectionImpl {
|
|
|
653
653
|
/**
|
|
654
654
|
* Recompute optimistic state from active transactions
|
|
655
655
|
*/
|
|
656
|
-
recomputeOptimisticState() {
|
|
656
|
+
recomputeOptimisticState(triggeredByUserAction = false) {
|
|
657
657
|
if (this.isCommittingSyncTransactions) {
|
|
658
658
|
return;
|
|
659
659
|
}
|
|
@@ -690,10 +690,16 @@ class CollectionImpl {
|
|
|
690
690
|
this._size = this.calculateSize();
|
|
691
691
|
const events = [];
|
|
692
692
|
this.collectOptimisticChanges(previousState, previousDeletes, events);
|
|
693
|
-
const filteredEventsBySyncStatus = events.filter(
|
|
694
|
-
(
|
|
695
|
-
|
|
696
|
-
|
|
693
|
+
const filteredEventsBySyncStatus = events.filter((event) => {
|
|
694
|
+
if (!this.recentlySyncedKeys.has(event.key)) {
|
|
695
|
+
return true;
|
|
696
|
+
}
|
|
697
|
+
if (triggeredByUserAction) {
|
|
698
|
+
return true;
|
|
699
|
+
}
|
|
700
|
+
return false;
|
|
701
|
+
});
|
|
702
|
+
if (this.pendingSyncedTransactions.length > 0 && !triggeredByUserAction) {
|
|
697
703
|
const pendingSyncKeys = /* @__PURE__ */ new Set();
|
|
698
704
|
const completedTransactionMutations = /* @__PURE__ */ new Set();
|
|
699
705
|
for (const transaction of this.pendingSyncedTransactions) {
|
|
@@ -724,12 +730,12 @@ class CollectionImpl {
|
|
|
724
730
|
if (filteredEvents.length > 0) {
|
|
725
731
|
this.updateIndexes(filteredEvents);
|
|
726
732
|
}
|
|
727
|
-
this.emitEvents(filteredEvents);
|
|
733
|
+
this.emitEvents(filteredEvents, triggeredByUserAction);
|
|
728
734
|
} else {
|
|
729
735
|
if (filteredEventsBySyncStatus.length > 0) {
|
|
730
736
|
this.updateIndexes(filteredEventsBySyncStatus);
|
|
731
737
|
}
|
|
732
|
-
this.emitEvents(filteredEventsBySyncStatus);
|
|
738
|
+
this.emitEvents(filteredEventsBySyncStatus, triggeredByUserAction);
|
|
733
739
|
}
|
|
734
740
|
}
|
|
735
741
|
/**
|
|
@@ -791,16 +797,14 @@ class CollectionImpl {
|
|
|
791
797
|
/**
|
|
792
798
|
* Emit events either immediately or batch them for later emission
|
|
793
799
|
*/
|
|
794
|
-
emitEvents(changes,
|
|
795
|
-
if (this.shouldBatchEvents && !
|
|
800
|
+
emitEvents(changes, forceEmit = false) {
|
|
801
|
+
if (this.shouldBatchEvents && !forceEmit) {
|
|
796
802
|
this.batchedEvents.push(...changes);
|
|
797
803
|
return;
|
|
798
804
|
}
|
|
799
805
|
let eventsToEmit = changes;
|
|
800
|
-
if (
|
|
801
|
-
|
|
802
|
-
eventsToEmit = [...this.batchedEvents, ...changes];
|
|
803
|
-
}
|
|
806
|
+
if (this.batchedEvents.length > 0 && forceEmit) {
|
|
807
|
+
eventsToEmit = [...this.batchedEvents, ...changes];
|
|
804
808
|
this.batchedEvents = [];
|
|
805
809
|
this.shouldBatchEvents = false;
|
|
806
810
|
}
|
|
@@ -1206,7 +1210,7 @@ class CollectionImpl {
|
|
|
1206
1210
|
if (ambientTransaction) {
|
|
1207
1211
|
ambientTransaction.applyMutations(mutations);
|
|
1208
1212
|
this.transactions.set(ambientTransaction.id, ambientTransaction);
|
|
1209
|
-
this.recomputeOptimisticState();
|
|
1213
|
+
this.recomputeOptimisticState(true);
|
|
1210
1214
|
return ambientTransaction;
|
|
1211
1215
|
}
|
|
1212
1216
|
const directOpTransaction = transactions.createTransaction({
|
|
@@ -1220,7 +1224,7 @@ class CollectionImpl {
|
|
|
1220
1224
|
directOpTransaction.applyMutations(mutations);
|
|
1221
1225
|
directOpTransaction.commit();
|
|
1222
1226
|
this.transactions.set(directOpTransaction.id, directOpTransaction);
|
|
1223
|
-
this.recomputeOptimisticState();
|
|
1227
|
+
this.recomputeOptimisticState(true);
|
|
1224
1228
|
return directOpTransaction;
|
|
1225
1229
|
}
|
|
1226
1230
|
/**
|
|
@@ -1424,7 +1428,7 @@ class CollectionImpl {
|
|
|
1424
1428
|
onTransactionStateChange() {
|
|
1425
1429
|
this.shouldBatchEvents = this.pendingSyncedTransactions.length > 0;
|
|
1426
1430
|
this.capturePreSyncVisibleState();
|
|
1427
|
-
this.recomputeOptimisticState();
|
|
1431
|
+
this.recomputeOptimisticState(false);
|
|
1428
1432
|
}
|
|
1429
1433
|
}
|
|
1430
1434
|
exports.CollectionImpl = CollectionImpl;
|