cry-synced-db-client 0.1.27 → 0.1.29

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.
@@ -187,6 +187,11 @@ export declare class SyncedDb implements I_SyncedDb {
187
187
  * Normalize timestamp to number for comparison
188
188
  */
189
189
  private normalizeTimestamp;
190
+ /**
191
+ * Broadcast metadata update to other tabs.
192
+ * Updates both Dexie sync meta and the in-memory cache.
193
+ */
194
+ private broadcastMetaUpdate;
190
195
  /**
191
196
  * Handle cross-tab sync metadata updates from BroadcastChannel
192
197
  */
package/dist/index.js CHANGED
@@ -1506,12 +1506,27 @@ class SyncedDb {
1506
1506
  return ts;
1507
1507
  if (ts instanceof Date)
1508
1508
  return ts.getTime();
1509
+ if (typeof ts === "object" && "t" in ts)
1510
+ return ts.t;
1509
1511
  if (typeof ts.toNumber === "function")
1510
1512
  return ts.toNumber();
1511
1513
  if (typeof ts.valueOf === "function")
1512
1514
  return ts.valueOf();
1513
1515
  return 0;
1514
1516
  }
1517
+ async broadcastMetaUpdate(collection, timestamp) {
1518
+ const cachedMeta = this.syncMetaCache.get(collection);
1519
+ const cachedTs = this.normalizeTimestamp(cachedMeta?.lastSyncTs);
1520
+ const newTs = this.normalizeTimestamp(timestamp);
1521
+ if (newTs > cachedTs) {
1522
+ await this.dexieDb.setSyncMeta(collection, timestamp);
1523
+ this.syncMetaCache.set(collection, {
1524
+ tenant: this.tenant,
1525
+ collection,
1526
+ lastSyncTs: timestamp
1527
+ });
1528
+ }
1529
+ }
1515
1530
  async handleCrossTabMetaUpdate(payload) {
1516
1531
  if (payload.instanceId === this.syncedDbInstanceId) {
1517
1532
  return;
@@ -1588,11 +1603,15 @@ class SyncedDb {
1588
1603
  const serverItem = items[0];
1589
1604
  if (serverItem) {
1590
1605
  await this.handleServerItemUpdate(collectionName, serverItem);
1606
+ if (serverItem._ts) {
1607
+ await this.broadcastMetaUpdate(collectionName, serverItem._ts);
1608
+ }
1591
1609
  }
1592
1610
  break;
1593
1611
  }
1594
1612
  case "delete": {
1595
1613
  await this.handleServerItemDelete(collectionName, payload._id);
1614
+ await this.broadcastMetaUpdate(collectionName, Date.now());
1596
1615
  break;
1597
1616
  }
1598
1617
  case "updateMany":
@@ -1712,7 +1731,7 @@ class DexieDb extends Dexie {
1712
1731
  schema[SYNC_META_TABLE] = "[tenant+collection]";
1713
1732
  for (const config of collectionConfigs) {
1714
1733
  const additionalIndexes = config.indexes || [];
1715
- const indexes = ["_id", "_dirty", "_updatedAt", ...additionalIndexes.map(String)];
1734
+ const indexes = ["_id", "_dirty", "_ts", ...additionalIndexes.map(String)];
1716
1735
  schema[config.name] = indexes.join(", ");
1717
1736
  }
1718
1737
  this.version(1).stores(schema);
@@ -1877,7 +1896,7 @@ class DexieDb extends Dexie {
1877
1896
  async getNewerThan(collection, timestamp) {
1878
1897
  const table = this.getTable(collection);
1879
1898
  const normalizedTs = this.normalizeTimestamp(timestamp);
1880
- return await table.where("_updatedAt").above(normalizedTs).toArray();
1899
+ return await table.where("_ts").above(normalizedTs).toArray();
1881
1900
  }
1882
1901
  onMetaUpdated(callback) {
1883
1902
  this.metaUpdateCallbacks.add(callback);
@@ -61,7 +61,7 @@ export interface I_DexieDb {
61
61
  deleteSyncMeta(collection: string): Promise<void>;
62
62
  /** Vrne tenant */
63
63
  getTenant(): string;
64
- /** Get records newer than timestamp (uses _updatedAt index) */
64
+ /** Get records newer than timestamp (uses _ts index - server timestamp) */
65
65
  getNewerThan<T extends LocalDbEntity>(collection: string, timestamp: any): Promise<T[]>;
66
66
  /** Subscribe to metadata updates from other tabs. Returns unsubscribe function. */
67
67
  onMetaUpdated(callback: (payload: MetaUpdateBroadcast) => void): () => void;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cry-synced-db-client",
3
- "version": "0.1.27",
3
+ "version": "0.1.29",
4
4
  "type": "module",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.js",