@bopen-io/wallet-toolbox 1.7.20-idb-fix.4 → 1.7.20-idb-fix.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bopen-io/wallet-toolbox",
3
- "version": "1.7.20-idb-fix.4",
3
+ "version": "1.7.20-idb-fix.7",
4
4
  "description": "BRC100 conforming wallet, wallet storage and wallet signer components",
5
5
  "main": "./out/src/index.js",
6
6
  "types": "./out/src/index.d.ts",
@@ -844,38 +844,60 @@ export class WalletStorageManager implements sdk.WalletStorage {
844
844
 
845
845
  log += progLog('\n')
846
846
 
847
- log += await this.runAsSync(async sync => {
848
- let log = ''
849
-
850
- if (this._conflictingActives!.length > 0) {
851
- // Merge state from conflicting actives into `newActive`.
852
-
853
- // Handle case where new active is current active to resolve conflicts.
854
- // And where new active is one of the current conflict actives.
855
- this._conflictingActives!.push(this._active!)
856
- // Remove the new active from conflicting actives and
857
- // set new active as the conflicting active that matches the target `storageIdentityKey`
858
- this._conflictingActives = this._conflictingActives!.filter(ca => {
859
- const isNewActive = ca.settings!.storageIdentityKey === storageIdentityKey
860
- return !isNewActive
861
- })
862
-
863
- // Merge state from conflicting actives into `newActive`.
864
- for (const conflict of this._conflictingActives) {
865
- log += progLog('MERGING STATE FROM CONFLICTING ACTIVES:\n')
866
- const sfr = await this.syncToWriter(
867
- { identityKey, userId: newActive.user!.userId, isActive: false },
868
- newActive.storage,
869
- conflict.storage,
870
- undefined,
871
- progLog
847
+ // Handle conflicting actives OUTSIDE runAsSync to avoid IDB timeout.
848
+ // Network calls to remote storage can take longer than IDB transaction timeout.
849
+ if (this._conflictingActives!.length > 0) {
850
+ // Handle case where new active is current active to resolve conflicts.
851
+ // And where new active is one of the current conflict actives.
852
+ this._conflictingActives!.push(this._active!)
853
+ // Remove the new active from conflicting actives
854
+ this._conflictingActives = this._conflictingActives!.filter(ca => {
855
+ const isNewActive = ca.settings!.storageIdentityKey === storageIdentityKey
856
+ return !isNewActive
857
+ })
858
+
859
+ // Merge state from each conflicting active into newActive.
860
+ // Network calls happen outside transactions to avoid IDB timeout.
861
+ // Database operations are wrapped in brief runAsSync calls for transaction safety.
862
+ for (const conflict of this._conflictingActives) {
863
+ log += progLog('MERGING STATE FROM CONFLICTING ACTIVES:\n')
864
+ const readerSettings = await conflict.storage.makeAvailable()
865
+ const writerSettings = await newActive.storage.makeAvailable()
866
+
867
+ let i = -1
868
+ for (;;) {
869
+ i++
870
+ // Get sync state in a brief transaction to prevent race conditions
871
+ const args = await this.runAsSync(async sync => {
872
+ const ss = await EntitySyncState.fromStorage(sync, identityKey, readerSettings)
873
+ return ss.makeRequestSyncChunkArgs(identityKey, writerSettings.storageIdentityKey)
874
+ })
875
+
876
+ // Network call to conflict (reader) - outside transaction
877
+ const chunk = await conflict.storage.getSyncChunk(args)
878
+ log += EntitySyncState.syncChunkSummary(chunk)
879
+
880
+ // Preserve activeStorage - merging from reader cannot change active
881
+ if (chunk.user) {
882
+ chunk.user.activeStorage = storageIdentityKey
883
+ }
884
+
885
+ // Process chunk in a brief transaction
886
+ const r = await this.runAsSync(async sync =>
887
+ sync.processSyncChunk(args, chunk)
872
888
  )
873
- log += sfr.log
889
+ log += progLog(`chunk ${i} inserted ${r.inserts} updated ${r.updates} ${r.maxUpdated_at}\n`)
890
+ if (r.done) break
874
891
  }
875
- log += progLog('PROPAGATE MERGED ACTIVE STATE TO NON-ACTIVES\n')
876
- } else {
877
- log += progLog('BACKUP CURRENT ACTIVE STATE THEN SET NEW ACTIVE\n')
878
892
  }
893
+ log += progLog('PROPAGATE MERGED ACTIVE STATE TO NON-ACTIVES\n')
894
+ } else {
895
+ log += progLog('BACKUP CURRENT ACTIVE STATE THEN SET NEW ACTIVE\n')
896
+ }
897
+
898
+ // Continue with local-only operations in runAsSync
899
+ log += await this.runAsSync(async sync => {
900
+ let innerLog = ''
879
901
 
880
902
  // If there were conflicting actives,
881
903
  // Push state merged from all merged actives into newActive to all stores other than the now single active.
@@ -896,18 +918,18 @@ export class WalletStorageManager implements sdk.WalletStorage {
896
918
  const stwr = await this.syncToWriter(
897
919
  { identityKey, userId: store.user!.userId, isActive: false },
898
920
  store.storage,
899
- backupSource.storage,
921
+ sync,
900
922
  undefined,
901
923
  progLog
902
924
  )
903
- log += stwr.log
925
+ innerLog += stwr.log
904
926
  }
905
927
  }
906
928
 
907
929
  this._isAvailable = false
908
930
  await this.makeAvailable()
909
931
 
910
- return log
932
+ return innerLog
911
933
  })
912
934
 
913
935
  return log