cry-synced-db-client 0.1.114 → 0.1.115

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/index.js CHANGED
@@ -3427,7 +3427,7 @@ var SyncedDb = class _SyncedDb {
3427
3427
  this.syncOnlyCollections = null;
3428
3428
  // Sync metadata cache
3429
3429
  this.syncMetaCache = /* @__PURE__ */ new Map();
3430
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l;
3430
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m;
3431
3431
  this.tenant = config.tenant;
3432
3432
  this.dexieDb = config.dexieDb;
3433
3433
  this.inMemDb = config.inMemDb;
@@ -3435,9 +3435,10 @@ var SyncedDb = class _SyncedDb {
3435
3435
  this.serverUpdateNotifier = config.serverUpdateNotifier;
3436
3436
  this.updaterId = Math.random().toString(36).substring(2, 15);
3437
3437
  this.syncedDbInstanceId = `${Date.now()}-${Math.random().toString(36).slice(2, 9)}`;
3438
- const windowId = (_a = config._testWindowId) != null ? _a : this.getOrCreateWindowId();
3439
- this.defaultReturnDeleted = (_b = config.returnDeleted) != null ? _b : false;
3440
- this.defaultReturnArchived = (_c = config.returnArchived) != null ? _c : false;
3438
+ this.dexieLoadConcurrency = (_a = config.dexieLoadConcurrency) != null ? _a : 1;
3439
+ const windowId = (_b = config._testWindowId) != null ? _b : this.getOrCreateWindowId();
3440
+ this.defaultReturnDeleted = (_c = config.returnDeleted) != null ? _c : false;
3441
+ this.defaultReturnArchived = (_d = config.returnArchived) != null ? _d : false;
3441
3442
  this.onDatabaseCreated = config.onDatabaseCreated;
3442
3443
  this.onSyncStart = config.onSyncStart;
3443
3444
  this.onSyncEnd = config.onSyncEnd;
@@ -3456,7 +3457,7 @@ var SyncedDb = class _SyncedDb {
3456
3457
  this.inMemManager = new InMemManager({
3457
3458
  inMemDb: this.inMemDb,
3458
3459
  collections: this.collections,
3459
- useObjectMetadata: (_d = config.useObjectMetadata) != null ? _d : false
3460
+ useObjectMetadata: (_e = config.useObjectMetadata) != null ? _e : false
3460
3461
  });
3461
3462
  this.leaderElection = new LeaderElectionManager({
3462
3463
  tenant: this.tenant,
@@ -3478,7 +3479,7 @@ var SyncedDb = class _SyncedDb {
3478
3479
  tenant: this.tenant,
3479
3480
  instanceId: this.syncedDbInstanceId,
3480
3481
  windowId,
3481
- debounceMs: (_e = config.crossTabSyncDebounceMs) != null ? _e : 100,
3482
+ debounceMs: (_f = config.crossTabSyncDebounceMs) != null ? _f : 100,
3482
3483
  callbacks: {
3483
3484
  onCrossTabSync: config.onCrossTabSync,
3484
3485
  onInfrastructureError: config.onInfrastructureError ? (type, message, error) => {
@@ -3504,8 +3505,8 @@ var SyncedDb = class _SyncedDb {
3504
3505
  });
3505
3506
  this.connectionManager = new ConnectionManager({
3506
3507
  restInterface: this.restInterface,
3507
- restTimeoutMs: (_f = config.restTimeoutMs) != null ? _f : 9e4,
3508
- syncTimeoutMs: (_g = config.syncTimeoutMs) != null ? _g : 12e4,
3508
+ restTimeoutMs: (_g = config.restTimeoutMs) != null ? _g : 9e4,
3509
+ syncTimeoutMs: (_h = config.syncTimeoutMs) != null ? _h : 12e4,
3509
3510
  autoSyncIntervalMs: config.autoSyncIntervalMs,
3510
3511
  callbacks: {
3511
3512
  onOnlineStatusChange: config.onOnlineStatusChange,
@@ -3531,8 +3532,8 @@ var SyncedDb = class _SyncedDb {
3531
3532
  });
3532
3533
  this.pendingChanges = new PendingChangesManager({
3533
3534
  tenant: this.tenant,
3534
- debounceDexieWritesMs: (_h = config.debounceDexieWritesMs) != null ? _h : 500,
3535
- debounceRestWritesMs: (_i = config.debounceRestWritesMs) != null ? _i : 100,
3535
+ debounceDexieWritesMs: (_i = config.debounceDexieWritesMs) != null ? _i : 500,
3536
+ debounceRestWritesMs: (_j = config.debounceRestWritesMs) != null ? _j : 100,
3536
3537
  callbacks: {
3537
3538
  onDexieWriteRequest: config.onDexieWriteRequest,
3538
3539
  onDexieWriteResult: config.onDexieWriteResult,
@@ -3609,8 +3610,8 @@ var SyncedDb = class _SyncedDb {
3609
3610
  });
3610
3611
  if (config.wakeSyncEnabled) {
3611
3612
  this.wakeSync = new WakeSyncManager({
3612
- gapThresholdMs: (_j = config.wakeSyncGapThresholdMs) != null ? _j : 1e4,
3613
- debounceMs: (_k = config.wakeSyncDebounceMs) != null ? _k : 2e3,
3613
+ gapThresholdMs: (_k = config.wakeSyncGapThresholdMs) != null ? _k : 1e4,
3614
+ debounceMs: (_l = config.wakeSyncDebounceMs) != null ? _l : 2e3,
3614
3615
  callbacks: {
3615
3616
  onWakeSync: config.onWakeSync
3616
3617
  },
@@ -3625,7 +3626,7 @@ var SyncedDb = class _SyncedDb {
3625
3626
  }
3626
3627
  if (config.networkStatusEnabled) {
3627
3628
  this.networkStatus = new NetworkStatusManager({
3628
- debounceMs: (_l = config.networkStatusDebounceMs) != null ? _l : 100,
3629
+ debounceMs: (_m = config.networkStatusDebounceMs) != null ? _m : 100,
3629
3630
  callbacks: {
3630
3631
  onBrowserNetworkChange: config.onBrowserNetworkChange,
3631
3632
  onBrowserOnline: config.onBrowserOnline,
@@ -3668,16 +3669,7 @@ var SyncedDb = class _SyncedDb {
3668
3669
  }
3669
3670
  }
3670
3671
  if (newlyAllowed.length > 0) {
3671
- const dexieStart = Date.now();
3672
- let totalItems = 0;
3673
- this.safeCallback(this.onDexieSyncStart, { calledFrom: "setSyncOnlyTheseCollections", collectionCount: newlyAllowed.length });
3674
- for (let i = 0; i < newlyAllowed.length; i++) {
3675
- const name = newlyAllowed[i];
3676
- const items = await this.loadCollectionToInMem(name);
3677
- totalItems += items;
3678
- this.safeCallback(this.onSyncProgress, { collection: name, loaded: i + 1, total: newlyAllowed.length, items });
3679
- }
3680
- this.safeCallback(this.onDexieSyncEnd, { calledFrom: "setSyncOnlyTheseCollections", collectionCount: newlyAllowed.length, totalItems, durationMs: Date.now() - dexieStart });
3672
+ await this.loadCollectionsToInMem(newlyAllowed, "setSyncOnlyTheseCollections");
3681
3673
  }
3682
3674
  if (newlyAllowed.length > 0 && this.connectionManager.canSync()) {
3683
3675
  this.sync("setSyncOnlyTheseCollections").catch(() => {
@@ -3724,16 +3716,7 @@ var SyncedDb = class _SyncedDb {
3724
3716
  }
3725
3717
  await this.pendingChanges.recoverPendingWrites();
3726
3718
  const allowedColls = [...this.collections.keys()].filter((n) => this.isSyncAllowed(n));
3727
- const dexieStart = Date.now();
3728
- let totalItems = 0;
3729
- this.safeCallback(this.onDexieSyncStart, { calledFrom: "init", collectionCount: allowedColls.length });
3730
- for (let i = 0; i < allowedColls.length; i++) {
3731
- const name = allowedColls[i];
3732
- const items = await this.loadCollectionToInMem(name);
3733
- totalItems += items;
3734
- this.safeCallback(this.onSyncProgress, { collection: name, loaded: i + 1, total: allowedColls.length, items });
3735
- }
3736
- this.safeCallback(this.onDexieSyncEnd, { calledFrom: "init", collectionCount: allowedColls.length, totalItems, durationMs: Date.now() - dexieStart });
3719
+ await this.loadCollectionsToInMem(allowedColls, "init");
3737
3720
  this.leaderElection.init();
3738
3721
  this.crossTabSync.init();
3739
3722
  (_a = this.wakeSync) == null ? void 0 : _a.init();
@@ -4566,6 +4549,47 @@ var SyncedDb = class _SyncedDb {
4566
4549
  }
4567
4550
  }
4568
4551
  }
4552
+ /**
4553
+ * Load multiple collections from Dexie into in-mem with concurrency control.
4554
+ * @returns total items loaded
4555
+ */
4556
+ async loadCollectionsToInMem(names, calledFrom) {
4557
+ if (names.length === 0) return 0;
4558
+ const dexieStart = Date.now();
4559
+ let totalItems = 0;
4560
+ let loaded = 0;
4561
+ this.safeCallback(this.onDexieSyncStart, {
4562
+ calledFrom,
4563
+ collectionCount: names.length
4564
+ });
4565
+ const concurrency = this.dexieLoadConcurrency;
4566
+ const queue = [...names];
4567
+ const workers = Array.from(
4568
+ { length: Math.min(concurrency, queue.length) },
4569
+ async () => {
4570
+ while (queue.length > 0) {
4571
+ const name = queue.shift();
4572
+ const items = await this.loadCollectionToInMem(name);
4573
+ totalItems += items;
4574
+ loaded++;
4575
+ this.safeCallback(this.onSyncProgress, {
4576
+ collection: name,
4577
+ loaded,
4578
+ total: names.length,
4579
+ items
4580
+ });
4581
+ }
4582
+ }
4583
+ );
4584
+ await Promise.all(workers);
4585
+ this.safeCallback(this.onDexieSyncEnd, {
4586
+ calledFrom,
4587
+ collectionCount: names.length,
4588
+ totalItems,
4589
+ durationMs: Date.now() - dexieStart
4590
+ });
4591
+ return totalItems;
4592
+ }
4569
4593
  async loadCollectionToInMem(name) {
4570
4594
  const allItems = [];
4571
4595
  await this.dexieDb.forEachBatch(name, 2e3, async (chunk) => {
@@ -31,6 +31,7 @@ export declare class SyncedDb implements I_SyncedDb {
31
31
  private syncOnlyCollections;
32
32
  private readonly updaterId;
33
33
  private readonly syncedDbInstanceId;
34
+ private readonly dexieLoadConcurrency;
34
35
  private syncMetaCache;
35
36
  private unsubscribeServerUpdates?;
36
37
  private cleanupNotifierCallbacks?;
@@ -182,6 +183,11 @@ export declare class SyncedDb implements I_SyncedDb {
182
183
  */
183
184
  /** Safe callback invocation — swallows errors */
184
185
  private safeCallback;
186
+ /**
187
+ * Load multiple collections from Dexie into in-mem with concurrency control.
188
+ * @returns total items loaded
189
+ */
190
+ private loadCollectionsToInMem;
185
191
  private loadCollectionToInMem;
186
192
  private assertCollection;
187
193
  /** Stringify an Id parameter (ObjectId → hex string). */
@@ -408,6 +408,12 @@ export interface SyncedDbConfig {
408
408
  * Default: false
409
409
  */
410
410
  returnArchived?: boolean;
411
+ /**
412
+ * Number of collections to load from Dexie concurrently during init
413
+ * and setSyncOnlyTheseCollections. Higher values speed up initial load
414
+ * by issuing parallel IndexedDB reads. Default: 1 (sequential).
415
+ */
416
+ dexieLoadConcurrency?: number;
411
417
  /**
412
418
  * Enable sync on wake from sleep for leader tab.
413
419
  * When enabled, detects wake via pageshow/focus/visibilitychange events
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cry-synced-db-client",
3
- "version": "0.1.114",
3
+ "version": "0.1.115",
4
4
  "type": "module",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.js",