@jcbuisson/express-x-client 3.1.2 → 3.1.7

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 +10 -5
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jcbuisson/express-x-client",
3
- "version": "3.1.2",
3
+ "version": "3.1.7",
4
4
  "type": "module",
5
5
  "description": "Client library for ExpressX framework",
6
6
  "main": "src/client.mts",
package/src/client.mts CHANGED
@@ -1,6 +1,6 @@
1
1
  import Dexie from "dexie";
2
2
  import { from } from 'rxjs';
3
- import { distinctUntilChanged } from 'rxjs/operators';
3
+ import { distinctUntilChanged, startWith } from 'rxjs/operators';
4
4
  import { liveQuery } from "dexie";
5
5
  // uuidv7 are monotonically increasing and much improve database performance amid B-tree indexes
6
6
  import { v7 as uuidv7 } from 'uuid';
@@ -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) {
@@ -304,6 +304,7 @@ export function offlinePlugin(app) {
304
304
  })
305
305
  const predicate = wherePredicate(where)
306
306
  return from(liveQuery(() => db.values.filter(value => !value.__deleted__ && predicate(value)).toArray())).pipe(
307
+ startWith(undefined),
307
308
  distinctUntilChanged((prev, curr) => {
308
309
  // Deep equality check to prevent unnecessary emissions (in particular on database write)
309
310
  return JSON.stringify(prev) === JSON.stringify(curr)
@@ -415,9 +416,13 @@ export function offlinePlugin(app) {
415
416
  })
416
417
  }
417
418
  // 2- delete elements from indexedDB cache
418
- for (const [uid] of deleteClient) {
419
- await idbValues.delete(uid)
420
- await idbMetadata.delete(uid)
419
+ if (deleteClient.length > 0) {
420
+ await idbValues.db.transaction('rw', [idbValues, idbMetadata], async () => {
421
+ for (const [uid] of deleteClient) {
422
+ await idbValues.delete(uid)
423
+ await idbMetadata.delete(uid)
424
+ }
425
+ })
421
426
  }
422
427
  // 3- update elements of cache with server's newer version
423
428
  for (const [elt, serverMeta] of updateClient) {