From 87f21cb0c5a547341ef53b44682816a1f5253a0b 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, 8 Dec 2024 02:02:09 +1300 Subject: [PATCH] refactor: promise types (#388) --- packages/core/src/types.ts | 10 ++++++---- packages/core/src/wallets/deep-link-wallet.ts | 5 ++--- packages/core/src/wallets/local-wallet.ts | 13 +++++++------ packages/core/src/wallets/provider.ts | 3 ++- packages/core/src/wallets/wallet.ts | 9 +++++---- 5 files changed, 22 insertions(+), 18 deletions(-) diff --git a/packages/core/src/types.ts b/packages/core/src/types.ts index 34dfa758..7a476d1b 100644 --- a/packages/core/src/types.ts +++ b/packages/core/src/types.ts @@ -9,10 +9,12 @@ export type FalsyGuard< TFalsyValues = Falsy, > = TType extends TFalsyValues ? TReturnType | TFallback : TReturnType; +export type MaybePromise = T | Promise; + +export type MaybeAsync = T | Promise | Observable; + +export type Gettable = MaybePromise | (() => MaybePromise); + export type FlatHead = TArray extends [infer Head] ? Head : TArray; - -export type Gettable = T | PromiseLike | (() => T | PromiseLike); - -export type MaybeAsync = T | Promise | Observable; diff --git a/packages/core/src/wallets/deep-link-wallet.ts b/packages/core/src/wallets/deep-link-wallet.ts index 23184bfe..d6fccccb 100644 --- a/packages/core/src/wallets/deep-link-wallet.ts +++ b/packages/core/src/wallets/deep-link-wallet.ts @@ -1,3 +1,4 @@ +import type { MaybePromise } from "../types.js"; import { Wallet, type WalletOptions } from "./wallet.js"; type ConnectionHandshake = { @@ -9,7 +10,5 @@ export abstract class DeepLinkWallet< TOptions extends WalletOptions = WalletOptions, TStorageKey extends string = string, > extends Wallet { - abstract initiateConnectionHandshake(): - | ConnectionHandshake - | Promise; + abstract initiateConnectionHandshake(): MaybePromise; } diff --git a/packages/core/src/wallets/local-wallet.ts b/packages/core/src/wallets/local-wallet.ts index aaa76452..c0cf052f 100644 --- a/packages/core/src/wallets/local-wallet.ts +++ b/packages/core/src/wallets/local-wallet.ts @@ -1,3 +1,4 @@ +import type { MaybePromise } from "../types.js"; import type { PolkadotSignerAccount } from "./account.js"; import { Wallet, type WalletOptions } from "./wallet.js"; @@ -13,12 +14,12 @@ export abstract class LocalWallet< * @experimental */ abstract accountStore: { - add(account: TAccount): void | Promise; - clear(): void | Promise; - delete(account: { id: TAccount["id"] }): void | Promise; - delete(accountId: TAccount["id"]): void | Promise; - has(account: { id: TAccount["id"] }): boolean | Promise; - has(accountId: TAccount["id"]): boolean | Promise; + add(account: TAccount): MaybePromise; + clear(): MaybePromise; + delete(account: { id: TAccount["id"] }): MaybePromise; + delete(accountId: TAccount["id"]): MaybePromise; + has(account: { id: TAccount["id"] }): MaybePromise; + has(accountId: TAccount["id"]): MaybePromise; values(): Iterable; }; } diff --git a/packages/core/src/wallets/provider.ts b/packages/core/src/wallets/provider.ts index d39d7d30..6bc312e1 100644 --- a/packages/core/src/wallets/provider.ts +++ b/packages/core/src/wallets/provider.ts @@ -1,5 +1,6 @@ +import type { MaybePromise } from "../types.js"; import type { Wallet } from "./wallet.js"; export abstract class WalletProvider { - abstract getWallets(): Wallet[] | Promise; + abstract getWallets(): MaybePromise; } diff --git a/packages/core/src/wallets/wallet.ts b/packages/core/src/wallets/wallet.ts index f5a93f94..1eedc450 100644 --- a/packages/core/src/wallets/wallet.ts +++ b/packages/core/src/wallets/wallet.ts @@ -1,4 +1,5 @@ import { type Storage, defaultStorage } from "../storage.js"; +import type { MaybePromise } from "../types.js"; import type { PolkadotSignerAccount } from "./account.js"; import { firstValueFrom, type Observable } from "rxjs"; @@ -24,17 +25,17 @@ export abstract class Wallet< this.#storage = (options?.storage ?? defaultStorage).join("wallet"); } - abstract initialize(): void | Promise; + abstract initialize(): MaybePromise; abstract readonly connected$: Observable; - abstract connect(): void | Promise; + abstract connect(): MaybePromise; - abstract disconnect(): void | Promise; + abstract disconnect(): MaybePromise; abstract readonly accounts$: Observable; - getAccounts(): PolkadotSignerAccount[] | Promise { + getAccounts(): MaybePromise { return firstValueFrom(this.accounts$, { defaultValue: [] }); } }