@sundaeswap/wallet-lite 0.0.102 → 0.0.103

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": "@sundaeswap/wallet-lite",
3
- "version": "0.0.102",
3
+ "version": "0.0.103",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "main": "./dist/cjs/index.js",
@@ -58,6 +58,9 @@ export class WalletObserver<
58
58
  // Caching
59
59
  private _cachedMetadata: Map<string, AssetMetadata> = new Map();
60
60
 
61
+ // AbortController for cancelling in-flight metadata fetches when metadataResolver changes
62
+ private _metadataAbortController: AbortController | null = null;
63
+
61
64
  /**
62
65
  * Creates an instance of WalletObserver.
63
66
  *
@@ -296,16 +299,33 @@ export class WalletObserver<
296
299
 
297
300
  /**
298
301
  * Updates the wallet observer options. Merges the new options with the existing ones.
302
+ * If metadataResolver changes, cancels any in-flight metadata fetch, clears the cache,
303
+ * and triggers a new sync if there is an active connection.
299
304
  *
300
305
  * @param {Partial<TWalletObserverOptions<AssetMetadata>>} options - The new options to merge.
301
306
  */
302
307
  updateOptions = (
303
308
  options: Partial<TWalletObserverOptions<AssetMetadata>>,
304
309
  ): void => {
310
+ const metadataResolverChanged =
311
+ options.metadataResolver &&
312
+ options.metadataResolver !== this._options.metadataResolver;
313
+
305
314
  this._options = merge<
306
315
  IResolvedWalletObserverOptions<AssetMetadata>,
307
316
  typeof options
308
317
  >(this._options, options);
318
+
319
+ // If metadataResolver changed, abort in-flight fetch, clear cache, and re-sync
320
+ if (metadataResolverChanged) {
321
+ this._metadataAbortController?.abort();
322
+ this._cachedMetadata = new Map();
323
+
324
+ // Trigger a new sync if there's an active connection
325
+ if (this.hasActiveConnection() && !this._performingSync) {
326
+ this.sync();
327
+ }
328
+ }
309
329
  };
310
330
 
311
331
  /**
@@ -754,6 +774,7 @@ export class WalletObserver<
754
774
 
755
775
  /**
756
776
  * Resolves metadata for the given asset IDs, using a cached version if available.
777
+ * Aborts any in-flight fetch when called, ensuring only the latest request completes.
757
778
  *
758
779
  * @private
759
780
  * @param {string[]} assetIds - The IDs of the assets to resolve metadata for.
@@ -780,6 +801,11 @@ export class WalletObserver<
780
801
  }
781
802
  }
782
803
 
804
+ // Abort any in-flight metadata fetch
805
+ this._metadataAbortController?.abort();
806
+ this._metadataAbortController = new AbortController();
807
+ const currentController = this._metadataAbortController;
808
+
783
809
  let attempts = 0;
784
810
  let newMetadata: Map<string, AssetMetadata> | undefined;
785
811
  while (attempts <= 3 && !newMetadata) {
@@ -794,6 +820,11 @@ export class WalletObserver<
794
820
  }
795
821
  }
796
822
 
823
+ // If this request was aborted while in-flight, return current cache (a new fetch is in progress)
824
+ if (currentController.signal.aborted) {
825
+ return this._cachedMetadata;
826
+ }
827
+
797
828
  if (!newMetadata) {
798
829
  newMetadata = await this.fallbackMetadataResolver({
799
830
  assetIds: assetIds.map(normalizeAssetIdWithDot),
@@ -802,6 +833,11 @@ export class WalletObserver<
802
833
  });
803
834
  }
804
835
 
836
+ // Double-check abort status after fallback resolver
837
+ if (currentController.signal.aborted) {
838
+ return this._cachedMetadata;
839
+ }
840
+
805
841
  this._cachedMetadata = newMetadata;
806
842
 
807
843
  const end = performance.now();