Skip to content

Commit

Permalink
refactor: combine pipes
Browse files Browse the repository at this point in the history
  • Loading branch information
tien committed Jun 24, 2024
1 parent 8d43f75 commit dfbf8c2
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 32 deletions.
17 changes: 8 additions & 9 deletions packages/react/src/stores/accounts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,16 @@ export const accountsAtom = atomFamily(
Promise.all([get(chainSpecDataAtomFamily(chainId)), get(walletsAtom)]),
).pipe(
switchMap(([chainSpec, wallets]) =>
combineLatest(wallets.map((wallet) => wallet.accounts$))
.pipe(map((wallets) => wallets.flat()))
.pipe(
map((accounts) =>
accounts.filter(
(account) =>
!account.genesisHash ||
chainSpec.genesisHash.includes(account.genesisHash),
),
combineLatest(wallets.map((wallet) => wallet.accounts$)).pipe(
map((wallets) => wallets.flat()),
map((accounts) =>
accounts.filter(
(account) =>
!account.genesisHash ||
chainSpec.genesisHash.includes(account.genesisHash),
),
),
),
),
),
),
Expand Down
7 changes: 4 additions & 3 deletions packages/react/src/stores/block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ export const finalizedBlockAtomFamily = atomFamily((chainId: ChainId) =>

export const bestBlockAtomFamily = atomFamily((chainId: ChainId) =>
atomWithObservable((get) =>
from(get(clientAtomFamily(chainId)))
.pipe(switchMap((client) => client.bestBlocks$))
.pipe(map((blocks) => blocks.at(0)!)),
from(get(clientAtomFamily(chainId))).pipe(
switchMap((client) => client.bestBlocks$),
map((blocks) => blocks.at(0)!),
),
),
);
36 changes: 16 additions & 20 deletions packages/react/src/stores/wallets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,12 @@ const aggregatorWallets = atomWithObservable((get) =>
return aggregator;
}),
),
)
.pipe(
switchMap((aggregators) =>
combineLatest(aggregators.map((aggregator) => aggregator.wallets$)),
),
)
.pipe(map((wallets) => wallets.flat())),
).pipe(
switchMap((aggregators) =>
combineLatest(aggregators.map((aggregator) => aggregator.wallets$)),
),
map((wallets) => wallets.flat()),
),
);

export const directWalletsAtom = atom<Wallet[]>([]);
Expand All @@ -31,21 +30,18 @@ export const walletsAtom = atom(async (get) => [
]);

export const connectedWalletsAtom = atomWithObservable((get) =>
from(get(walletsAtom))
.pipe(
switchMap((wallets) =>
combineLatest(
wallets.map((wallet) =>
wallet.connected$.pipe(
map((connected) => [wallet, connected] as const),
),
from(get(walletsAtom)).pipe(
switchMap((wallets) =>
combineLatest(
wallets.map((wallet) =>
wallet.connected$.pipe(
map((connected) => [wallet, connected] as const),
),
),
),
)
.pipe(
map((wallets) =>
wallets.filter(([_, connected]) => connected).map(([wallet]) => wallet),
),
),
map((wallets) =>
wallets.filter(([_, connected]) => connected).map(([wallet]) => wallet),
),
),
);

0 comments on commit dfbf8c2

Please sign in to comment.