Skip to content

Commit

Permalink
fix: wallet aggregation bug (#346)
Browse files Browse the repository at this point in the history
  • Loading branch information
tien authored Nov 15, 2024
1 parent 912ecc1 commit a3da0de
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/brave-bikes-rescue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@reactive-dot/core": patch
---

Fixed wallet aggregation bug when aggregation happens asynchronously.
7 changes: 4 additions & 3 deletions packages/core/src/actions/aggregate-wallets.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
17 changes: 11 additions & 6 deletions packages/core/src/actions/aggregate-wallets.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { Wallet, type WalletProvider } from "../wallets/index.js";

const providerWallets = new WeakMap<WalletProvider, readonly Wallet[]>();
const providerWallets = new WeakMap<
WalletProvider,
Promise<readonly Wallet[]>
>();

export function aggregateWallets(
providersOrWallets: ReadonlyArray<Wallet | WalletProvider>,
Expand All @@ -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());
}

0 comments on commit a3da0de

Please sign in to comment.