From a3da0de4207499ff6e766f7affd08d086803a897 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ti=E1=BA=BFn=20Nguy=E1=BB=85n=20Kh=E1=BA=AFc?= Date: Fri, 15 Nov 2024 19:47:03 +1300 Subject: [PATCH] fix: wallet aggregation bug (#346) --- .changeset/brave-bikes-rescue.md | 5 +++++ .../core/src/actions/aggregate-wallets.spec.ts | 7 ++++--- packages/core/src/actions/aggregate-wallets.ts | 17 +++++++++++------ 3 files changed, 20 insertions(+), 9 deletions(-) create mode 100644 .changeset/brave-bikes-rescue.md diff --git a/.changeset/brave-bikes-rescue.md b/.changeset/brave-bikes-rescue.md new file mode 100644 index 00000000..73b5c1ce --- /dev/null +++ b/.changeset/brave-bikes-rescue.md @@ -0,0 +1,5 @@ +--- +"@reactive-dot/core": patch +--- + +Fixed wallet aggregation bug when aggregation happens asynchronously. diff --git a/packages/core/src/actions/aggregate-wallets.spec.ts b/packages/core/src/actions/aggregate-wallets.spec.ts index cc1e3124..770f8e1d 100644 --- a/packages/core/src/actions/aggregate-wallets.spec.ts +++ b/packages/core/src/actions/aggregate-wallets.spec.ts @@ -54,9 +54,10 @@ it("only aggregate once", async () => { new MockProvider(), ]; - const firstRunWallets = await aggregateWallets(walletsAndProviders); - - const secondRunWallets = await aggregateWallets(walletsAndProviders); + const [firstRunWallets, secondRunWallets] = await Promise.all([ + aggregateWallets(walletsAndProviders), + aggregateWallets(walletsAndProviders), + ]); expect(firstRunWallets).toHaveLength(secondRunWallets.length); diff --git a/packages/core/src/actions/aggregate-wallets.ts b/packages/core/src/actions/aggregate-wallets.ts index 69494769..108a7a91 100644 --- a/packages/core/src/actions/aggregate-wallets.ts +++ b/packages/core/src/actions/aggregate-wallets.ts @@ -1,6 +1,9 @@ import { Wallet, type WalletProvider } from "../wallets/index.js"; -const providerWallets = new WeakMap(); +const providerWallets = new WeakMap< + WalletProvider, + Promise +>(); export function aggregateWallets( providersOrWallets: ReadonlyArray, @@ -9,11 +12,13 @@ export function aggregateWallets( providersOrWallets.map((walletOrProvider) => walletOrProvider instanceof Wallet ? [walletOrProvider] - : (async () => - providerWallets.get(walletOrProvider) ?? - providerWallets - .set(walletOrProvider, await walletOrProvider.getWallets()) - .get(walletOrProvider)!)(), + : (providerWallets.get(walletOrProvider) ?? + providerWallets + .set( + walletOrProvider, + Promise.resolve(walletOrProvider.getWallets()), + ) + .get(walletOrProvider)!), ), ).then((wallets) => wallets.flat()); }