From 0958ce1f6c06f6e163b4ce6e8f012caf4fb34040 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ti=E1=BA=BFn=20Nguy=E1=BB=85n=20Kh=E1=BA=AFc?= Date: Sun, 3 Nov 2024 20:20:05 +0800 Subject: [PATCH] refactor: add default implementation for `Wallet.getAccounts` (#304) --- .changeset/ninety-plants-give.md | 7 +++++++ packages/core/src/wallets/injected/wallet.ts | 2 +- packages/core/src/wallets/wallet.ts | 8 ++++---- packages/wallet-ledger/src/ledger-wallet.ts | 6 +----- packages/wallet-walletconnect/src/index.ts | 6 +----- 5 files changed, 14 insertions(+), 15 deletions(-) create mode 100644 .changeset/ninety-plants-give.md diff --git a/.changeset/ninety-plants-give.md b/.changeset/ninety-plants-give.md new file mode 100644 index 00000000..5f2fa6bf --- /dev/null +++ b/.changeset/ninety-plants-give.md @@ -0,0 +1,7 @@ +--- +"@reactive-dot/wallet-walletconnect": patch +"@reactive-dot/wallet-ledger": patch +"@reactive-dot/core": patch +--- + +Added default implementation for `Wallet.getAccounts`. diff --git a/packages/core/src/wallets/injected/wallet.ts b/packages/core/src/wallets/injected/wallet.ts index ba3263d3..19106805 100644 --- a/packages/core/src/wallets/injected/wallet.ts +++ b/packages/core/src/wallets/injected/wallet.ts @@ -70,7 +70,7 @@ export class InjectedWallet extends Wallet { ), ); - getAccounts() { + override getAccounts() { const extension = this.#extension$.getValue(); if (extension === undefined) { diff --git a/packages/core/src/wallets/wallet.ts b/packages/core/src/wallets/wallet.ts index b73c61df..0663c726 100644 --- a/packages/core/src/wallets/wallet.ts +++ b/packages/core/src/wallets/wallet.ts @@ -1,6 +1,6 @@ import { type PrefixedStorage, defaultStorage } from "../storage.js"; import type { PolkadotSignerAccount } from "./account.js"; -import type { Observable } from "rxjs"; +import { firstValueFrom, type Observable } from "rxjs"; export type WalletOptions = { storage?: PrefixedStorage | undefined; @@ -34,7 +34,7 @@ export abstract class Wallet< abstract readonly accounts$: Observable; - abstract getAccounts(): - | PolkadotSignerAccount[] - | Promise; + getAccounts(): PolkadotSignerAccount[] | Promise { + return firstValueFrom(this.accounts$, { defaultValue: [] }); + } } diff --git a/packages/wallet-ledger/src/ledger-wallet.ts b/packages/wallet-ledger/src/ledger-wallet.ts index ae013db2..e2fcc52f 100644 --- a/packages/wallet-ledger/src/ledger-wallet.ts +++ b/packages/wallet-ledger/src/ledger-wallet.ts @@ -6,7 +6,7 @@ import { LocalWallet, type PolkadotSignerAccount, } from "@reactive-dot/core/wallets.js"; -import { BehaviorSubject, firstValueFrom } from "rxjs"; +import { BehaviorSubject } from "rxjs"; import { map, skip } from "rxjs/operators"; type LedgerAccount = { @@ -114,10 +114,6 @@ export class LedgerWallet extends LocalWallet< this.accountStore.clear(); } - getAccounts() { - return firstValueFrom(this.accounts$); - } - accountStore = { add: (account: LedgerAccount) => { this.#ledgerAccounts$.next( diff --git a/packages/wallet-walletconnect/src/index.ts b/packages/wallet-walletconnect/src/index.ts index c47c1b11..03106b08 100644 --- a/packages/wallet-walletconnect/src/index.ts +++ b/packages/wallet-walletconnect/src/index.ts @@ -13,7 +13,7 @@ import type { IUniversalProvider, UniversalProviderOpts, } from "@walletconnect/universal-provider"; -import { BehaviorSubject, firstValueFrom } from "rxjs"; +import { BehaviorSubject } from "rxjs"; import { map } from "rxjs/operators"; export class WalletConnect extends DeepLinkWallet { @@ -216,10 +216,6 @@ export class WalletConnect extends DeepLinkWallet { }), ); - getAccounts() { - return firstValueFrom(this.accounts$); - } - async #getModal() { if (this.#modal !== undefined) { return this.#modal;