@jcbuisson/express-x-client 3.1.18 → 3.1.20

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 (2) hide show
  1. package/package.json +1 -1
  2. package/src/client.mts +22 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jcbuisson/express-x-client",
3
- "version": "3.1.18",
3
+ "version": "3.1.20",
4
4
  "type": "module",
5
5
  "description": "Client library for ExpressX framework",
6
6
  "main": "src/client.mts",
package/src/client.mts CHANGED
@@ -534,6 +534,8 @@ export function offlinePlugin(app) {
534
534
  if (deleteClient.length > 0) {
535
535
  await idbValues.db.transaction('rw', [idbValues, idbMetadata], async () => {
536
536
  for (const [uid] of deleteClient) {
537
+ const currentMetadata = await idbMetadata.get(uid)
538
+ if (!metadataUnchangedSinceRequest(currentMetadata, clientMetadataDict[uid])) continue
537
539
  await idbValues.delete(uid)
538
540
  await idbMetadata.delete(uid)
539
541
  }
@@ -541,6 +543,8 @@ export function offlinePlugin(app) {
541
543
  }
542
544
  // 3- update elements of cache with server's newer version
543
545
  for (const [elt, serverMeta] of updateClient) {
546
+ const currentMetadata = await idbMetadata.get(elt.uid)
547
+ if (!metadataUnchangedSinceRequest(currentMetadata, clientMetadataDict[elt.uid])) continue
544
548
  const value = { ...elt }
545
549
  delete value.__deleted__
546
550
  await idbValues.put(value)
@@ -554,6 +558,8 @@ export function offlinePlugin(app) {
554
558
  // get() call: idbValues.get(undefined) itself throws before fullValue is
555
559
  // assigned, so checking fullValue == null afterwards is too late.
556
560
  if (elt.uid == null) continue
561
+ let currentMetadata = await idbMetadata.get(elt.uid)
562
+ if (!metadataUnchangedSinceRequest(currentMetadata, elt)) continue
557
563
  const fullValue = await idbValues.get(elt.uid)
558
564
  if (fullValue == null) continue // record deleted concurrently
559
565
  delete fullValue.uid
@@ -561,10 +567,14 @@ export function offlinePlugin(app) {
561
567
  try {
562
568
  const result = await app.service(modelName).createWithMeta(elt.uid, fullValue, elt.created_at)
563
569
  const serverMeta = Array.isArray(result) ? result[1] : null
570
+ currentMetadata = await idbMetadata.get(elt.uid)
571
+ if (!metadataUnchangedSinceRequest(currentMetadata, elt)) continue
564
572
  if (serverMeta?.uid) await idbMetadata.put({ ...serverMeta, __dirty__: false })
565
573
  else await idbMetadata.update(elt.uid, { __dirty__: false })
566
574
  } catch(err) {
567
575
  console.log("*** err sync user addDatabase", err, elt.uid, fullValue, elt.created_at)
576
+ currentMetadata = await idbMetadata.get(elt.uid)
577
+ if (!metadataUnchangedSinceRequest(currentMetadata, elt)) continue
568
578
  // rollback
569
579
  await idbValues.delete(elt.uid)
570
580
  await idbMetadata.delete(elt.uid)
@@ -574,6 +584,8 @@ export function offlinePlugin(app) {
574
584
  // 5- update elements of `updateDatabase` with full data from cache
575
585
  for (const elt of updateDatabase) {
576
586
  if (elt.uid == null) continue
587
+ let currentMetadata = await idbMetadata.get(elt.uid)
588
+ if (!metadataUnchangedSinceRequest(currentMetadata, elt)) continue
577
589
  const fullValue = await idbValues.get(elt.uid)
578
590
  if (fullValue == null) continue // record deleted concurrently
579
591
  delete fullValue.uid
@@ -581,6 +593,8 @@ export function offlinePlugin(app) {
581
593
  try {
582
594
  const result = await app.service(modelName).updateWithMeta(elt.uid, fullValue, elt.updated_at)
583
595
  const serverMeta = Array.isArray(result) ? result[1] : null
596
+ currentMetadata = await idbMetadata.get(elt.uid)
597
+ if (!metadataUnchangedSinceRequest(currentMetadata, elt)) continue
584
598
  if (serverMeta?.uid) await idbMetadata.put({ ...serverMeta, __dirty__: false })
585
599
  else await idbMetadata.update(elt.uid, { __dirty__: false })
586
600
  } catch(err) {
@@ -596,6 +610,14 @@ export function offlinePlugin(app) {
596
610
  }
597
611
  }
598
612
 
613
+ function metadataUnchangedSinceRequest(currentMetadata, requestMetadata) {
614
+ return currentMetadata
615
+ && requestMetadata
616
+ && sameTimestamp(currentMetadata.created_at, requestMetadata.created_at)
617
+ && sameTimestamp(currentMetadata.updated_at, requestMetadata.updated_at)
618
+ && sameTimestamp(currentMetadata.deleted_at, requestMetadata.deleted_at)
619
+ }
620
+
599
621
  // Singleton map to reuse Dexie instances per database name
600
622
  const dbInstances = new Map();
601
623