-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
34 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
"@reactive-dot/react": patch | ||
"@reactive-dot/core": patch | ||
"@reactive-dot/vue": patch | ||
--- | ||
|
||
Simplified wallet provider interface. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,13 @@ | ||
import type { MaybeAsync } from "../types.js"; | ||
import { toObservable } from "../utils/to-observable.js"; | ||
import type { WalletProvider } from "../wallets/index.js"; | ||
import { combineLatest, from, of } from "rxjs"; | ||
import { map, switchMap } from "rxjs/operators"; | ||
import { Wallet, type WalletProvider } from "../wallets/index.js"; | ||
|
||
export function aggregateWallets(providers: MaybeAsync<WalletProvider[]>) { | ||
return toObservable(providers).pipe( | ||
switchMap((providers) => { | ||
if (providers.length === 0) { | ||
return of([]); | ||
} | ||
|
||
return from( | ||
Promise.all( | ||
providers.map(async (provider) => { | ||
await provider.scan(); | ||
return provider; | ||
}), | ||
), | ||
); | ||
}), | ||
switchMap((providers) => | ||
combineLatest(providers.map((provider) => provider.wallets$)), | ||
export function aggregateWallets( | ||
providersOrWallets: ReadonlyArray<Wallet | WalletProvider>, | ||
) { | ||
return Promise.all( | ||
providersOrWallets.map((walletOrProvider) => | ||
walletOrProvider instanceof Wallet | ||
? [walletOrProvider] | ||
: walletOrProvider.getWallets(), | ||
), | ||
map((wallets) => wallets.flat()), | ||
); | ||
).then((wallets) => wallets.flat()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,5 @@ | ||
import { WalletProvider } from "./provider.js"; | ||
import type { Wallet } from "./wallet.js"; | ||
|
||
export async function initializeWallets( | ||
walletsOrProviders: Array<Wallet | WalletProvider>, | ||
) { | ||
const wallets = ( | ||
await Promise.all( | ||
walletsOrProviders.map((walletOrProvider) => | ||
walletOrProvider instanceof WalletProvider | ||
? walletOrProvider.scan() | ||
: [walletOrProvider], | ||
), | ||
) | ||
).flat(); | ||
|
||
export function initializeWallets(wallets: readonly Wallet[]) { | ||
return Promise.all(wallets.map((wallet) => wallet.initialize())); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,15 @@ | ||
import { WalletProvider } from "../provider.js"; | ||
import { InjectedWallet, type InjectedWalletOptions } from "./wallet.js"; | ||
import { getInjectedExtensions } from "polkadot-api/pjs-signer"; | ||
import { BehaviorSubject } from "rxjs"; | ||
import { map } from "rxjs/operators"; | ||
|
||
export class InjectedWalletProvider extends WalletProvider { | ||
constructor(private readonly options?: InjectedWalletOptions) { | ||
super(); | ||
} | ||
|
||
readonly #walletMap$ = new BehaviorSubject(new Map<string, InjectedWallet>()); | ||
|
||
readonly wallets$ = this.#walletMap$.pipe( | ||
map((walletMap) => Array.from(walletMap.values())), | ||
); | ||
|
||
scan() { | ||
const injectedNames = getInjectedExtensions() ?? []; | ||
|
||
const current = new Map(this.#walletMap$.value); | ||
|
||
for (const name of injectedNames) { | ||
if (!current.has(name)) { | ||
current.set(name, new InjectedWallet(name, this.options)); | ||
} | ||
} | ||
|
||
this.#walletMap$.next(current); | ||
|
||
return Array.from(current.values()); | ||
getWallets() { | ||
return getInjectedExtensions().map( | ||
(name) => new InjectedWallet(name, this.options), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,5 @@ | ||
import type { Wallet } from "./wallet.js"; | ||
import type { Observable } from "rxjs"; | ||
|
||
export abstract class WalletProvider { | ||
abstract scan(): Wallet[] | Promise<Wallet[]>; | ||
|
||
abstract readonly wallets$: Observable<Wallet[]>; | ||
abstract getWallets(): Wallet[] | Promise<Wallet[]>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,16 @@ | ||
import { useAsyncAction } from "./use-async-action.js"; | ||
import { useWalletsObservable } from "./use-wallets.js"; | ||
import { useWalletsPromise } from "./use-wallets.js"; | ||
import { initializeWallets } from "@reactive-dot/core/wallets.js"; | ||
import { firstValueFrom } from "rxjs"; | ||
|
||
/** | ||
* Composable for initializing wallets. | ||
* | ||
* @returns The initialization state and initialize function | ||
*/ | ||
export function useWalletsInitializer() { | ||
const walletsObservable = useWalletsObservable(); | ||
const walletsPromise = useWalletsPromise(); | ||
|
||
return useAsyncAction(async () => | ||
initializeWallets(await firstValueFrom(walletsObservable.value)), | ||
initializeWallets(await walletsPromise.value), | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters