@jcbuisson/express-x-client 3.1.1 → 3.1.6

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 +13 -10
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jcbuisson/express-x-client",
3
- "version": "3.1.1",
3
+ "version": "3.1.6",
4
4
  "type": "module",
5
5
  "description": "Client library for ExpressX framework",
6
6
  "main": "src/client.mts",
package/src/client.mts CHANGED
@@ -71,7 +71,7 @@ export function createClient(socket, options={}) {
71
71
  if (!action2service2handlers[action]) action2service2handlers[action] = {}
72
72
  const serviceHandlers = action2service2handlers[action]
73
73
  const handler = serviceHandlers[name]
74
- if (handler) handler(result)
74
+ if (handler) Promise.resolve(handler(result)).catch(err => console.error('service-event handler error', name, action, err))
75
75
  })
76
76
 
77
77
  async function serviceMethodRequest(name, action, serviceOptions, ...args) {
@@ -159,6 +159,7 @@ export async function reloadPlugin(app) {
159
159
  })
160
160
  socket.once('cnx-transfer-error', async (fromSocketId, toSocketId) => {
161
161
  console.log('ERR ERR!!!', fromSocketId, toSocketId)
162
+ cnxid.value = socketId
162
163
  })
163
164
  socket.emit('cnx-transfer', prevSocketId, socketId)
164
165
  } else {
@@ -314,7 +315,7 @@ export function offlinePlugin(app) {
314
315
 
315
316
  function addSynchroWhere(where: object) {
316
317
  const promise = addSynchroDBWhere(where, db.whereList)
317
- promise.then(isNew => isNew && count++ && console.log(`addSynchroWhere (${count})`, dbName, modelName, where))
318
+ promise.then(isNew => isNew && console.log(`addSynchroWhere (${++count})`, dbName, modelName, where))
318
319
  return promise
319
320
  }
320
321
 
@@ -414,9 +415,13 @@ export function offlinePlugin(app) {
414
415
  })
415
416
  }
416
417
  // 2- delete elements from indexedDB cache
417
- for (const [uid] of deleteClient) {
418
- await idbValues.delete(uid)
419
- await idbMetadata.delete(uid)
418
+ if (deleteClient.length > 0) {
419
+ await idbValues.db.transaction('rw', [idbValues, idbMetadata], async () => {
420
+ for (const [uid] of deleteClient) {
421
+ await idbValues.delete(uid)
422
+ await idbMetadata.delete(uid)
423
+ }
424
+ })
420
425
  }
421
426
  // 3- update elements of cache with server's newer version
422
427
  for (const [elt, serverMeta] of updateClient) {
@@ -435,14 +440,13 @@ export function offlinePlugin(app) {
435
440
  // assigned, so checking fullValue == null afterwards is too late.
436
441
  if (elt.uid == null) continue
437
442
  const fullValue = await idbValues.get(elt.uid)
438
- const meta = await idbMetadata.get(elt.uid)
439
443
  if (fullValue == null) continue // record deleted concurrently
440
444
  delete fullValue.uid
441
445
  delete fullValue.__deleted__
442
446
  try {
443
- await app.service(modelName).createWithMeta(elt.uid, fullValue, meta.created_at)
447
+ await app.service(modelName).createWithMeta(elt.uid, fullValue, elt.created_at)
444
448
  } catch(err) {
445
- console.log("*** err sync user addDatabase", err, elt.uid, fullValue, meta.created_at)
449
+ console.log("*** err sync user addDatabase", err, elt.uid, fullValue, elt.created_at)
446
450
  // rollback
447
451
  await idbValues.delete(elt.uid)
448
452
  await idbMetadata.delete(elt.uid)
@@ -453,12 +457,11 @@ export function offlinePlugin(app) {
453
457
  for (const elt of updateDatabase) {
454
458
  if (elt.uid == null) continue
455
459
  const fullValue = await idbValues.get(elt.uid)
456
- const meta = await idbMetadata.get(elt.uid)
457
460
  if (fullValue == null) continue // record deleted concurrently
458
461
  delete fullValue.uid
459
462
  delete fullValue.__deleted__
460
463
  try {
461
- await app.service(modelName).updateWithMeta(elt.uid, fullValue, meta.updated_at)
464
+ await app.service(modelName).updateWithMeta(elt.uid, fullValue, elt.updated_at)
462
465
  } catch(err) {
463
466
  console.log("*** err sync user updateDatabase", err)
464
467
  // Leave client's local version intact; it will be retried on the next sync.