Skip to content

Commit

Permalink
refactor: simplify global states (#124)
Browse files Browse the repository at this point in the history
Hydrate only a single config atom, from which other global states will be derived.
  • Loading branch information
tien authored Aug 18, 2024
1 parent 6471f9a commit d122505
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 35 deletions.
5 changes: 5 additions & 0 deletions .changeset/famous-vans-bow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@reactive-dot/react": patch
---

Fixed some global states that were unscoped.
2 changes: 1 addition & 1 deletion apps/example/.papi/descriptors/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "0.1.0-autogenerated.15663640930135754644",
"version": "0.1.0-autogenerated.13427088018156132241",
"name": "@polkadot-api/descriptors",
"files": [
"dist"
Expand Down
30 changes: 3 additions & 27 deletions packages/react/src/contexts/index.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { useWalletsReconnector } from "../hooks/use-wallets-reconnector.js";
import { chainConfigsAtom } from "../stores/config.js";
import { aggregatorsAtom, directWalletsAtom } from "../stores/wallets.js";
import { configAtom } from "../stores/config.js";
import { MutationEventSubjectContext } from "./mutation.js";
import type { Config } from "@reactive-dot/core";
import { Wallet, WalletAggregator } from "@reactive-dot/core/wallets.js";
import { ScopeProvider } from "jotai-scope";
import { useHydrateAtoms } from "jotai/utils";
import { Suspense, useEffect, useMemo, type PropsWithChildren } from "react";
Expand Down Expand Up @@ -34,29 +32,7 @@ export type ReDotProviderProps = PropsWithChildren<{
}>;

function ReDotHydrator(props: ReDotProviderProps) {
useHydrateAtoms(
useMemo(
() =>
// eslint-disable-next-line @typescript-eslint/no-explicit-any
new Map<any, any>([
[chainConfigsAtom, props.config.chains],
[
directWalletsAtom,
props.config.wallets?.filter(
(wallet): wallet is Wallet => wallet instanceof Wallet,
) ?? [],
],
[
aggregatorsAtom,
props.config.wallets?.filter(
(aggregator): aggregator is WalletAggregator =>
aggregator instanceof WalletAggregator,
) ?? [],
],
]),
[props.config],
),
);
useHydrateAtoms(useMemo(() => [[configAtom, props.config]], [props.config]));

return null;
}
Expand All @@ -82,7 +58,7 @@ export function ReDotProvider({
...props
}: ReDotProviderProps) {
return (
<ScopeProvider atoms={[chainConfigsAtom]}>
<ScopeProvider atoms={[configAtom]}>
<ReDotHydrator {...props} />
{autoReconnectWallets && (
<Suspense>
Expand Down
7 changes: 4 additions & 3 deletions packages/react/src/stores/config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { ChainConfig, ChainId } from "@reactive-dot/core";
import type { Config } from "@reactive-dot/core";
import { atom } from "jotai";

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const chainConfigsAtom = atom<Record<ChainId, ChainConfig>>({} as any);
export const configAtom = atom<Config>({ chains: {} });

export const chainConfigsAtom = atom((get) => get(configAtom).chains);
17 changes: 13 additions & 4 deletions packages/react/src/stores/wallets.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
import { configAtom } from "./config.js";
import { aggregateWallets, getConnectedWallets } from "@reactive-dot/core";
import type { Wallet, WalletAggregator } from "@reactive-dot/core/wallets.js";
import { Wallet, WalletAggregator } from "@reactive-dot/core/wallets.js";
import { atom } from "jotai";
import { atomWithObservable } from "jotai/utils";

export const aggregatorsAtom = atom<WalletAggregator[]>([]);
export const walletAggregatorsAtom = atom(
(get) =>
get(configAtom).wallets?.filter(
(aggregator) => aggregator instanceof WalletAggregator,
) ?? [],
);

const aggregatorWallets = atomWithObservable((get) =>
aggregateWallets(get(aggregatorsAtom)),
aggregateWallets(get(walletAggregatorsAtom)),
);

export const directWalletsAtom = atom<Wallet[]>([]);
export const directWalletsAtom = atom<Wallet[]>(
(get) =>
get(configAtom).wallets?.filter((wallet) => wallet instanceof Wallet) ?? [],
);

export const walletsAtom = atom(async (get) => [
...get(directWalletsAtom),
Expand Down

0 comments on commit d122505

Please sign in to comment.