Skip to content

Commit

Permalink
refactor: promise types (#388)
Browse files Browse the repository at this point in the history
  • Loading branch information
tien authored Dec 7, 2024
1 parent 8ca10b3 commit 87f21cb
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 18 deletions.
10 changes: 6 additions & 4 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ export type FalsyGuard<
TFalsyValues = Falsy,
> = TType extends TFalsyValues ? TReturnType | TFallback : TReturnType;

export type MaybePromise<T> = T | Promise<T>;

export type MaybeAsync<T> = T | Promise<T> | Observable<T>;

export type Gettable<T> = MaybePromise<T> | (() => MaybePromise<T>);

export type FlatHead<TArray extends unknown[]> = TArray extends [infer Head]
? Head
: TArray;

export type Gettable<T> = T | PromiseLike<T> | (() => T | PromiseLike<T>);

export type MaybeAsync<T> = T | Promise<T> | Observable<T>;
5 changes: 2 additions & 3 deletions packages/core/src/wallets/deep-link-wallet.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { MaybePromise } from "../types.js";
import { Wallet, type WalletOptions } from "./wallet.js";

type ConnectionHandshake = {
Expand All @@ -9,7 +10,5 @@ export abstract class DeepLinkWallet<
TOptions extends WalletOptions = WalletOptions,
TStorageKey extends string = string,
> extends Wallet<TOptions, TStorageKey> {
abstract initiateConnectionHandshake():
| ConnectionHandshake
| Promise<ConnectionHandshake>;
abstract initiateConnectionHandshake(): MaybePromise<ConnectionHandshake>;
}
13 changes: 7 additions & 6 deletions packages/core/src/wallets/local-wallet.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { MaybePromise } from "../types.js";
import type { PolkadotSignerAccount } from "./account.js";
import { Wallet, type WalletOptions } from "./wallet.js";

Expand All @@ -13,12 +14,12 @@ export abstract class LocalWallet<
* @experimental
*/
abstract accountStore: {
add(account: TAccount): void | Promise<void>;
clear(): void | Promise<void>;
delete(account: { id: TAccount["id"] }): void | Promise<void>;
delete(accountId: TAccount["id"]): void | Promise<void>;
has(account: { id: TAccount["id"] }): boolean | Promise<boolean>;
has(accountId: TAccount["id"]): boolean | Promise<boolean>;
add(account: TAccount): MaybePromise<void>;
clear(): MaybePromise<void>;
delete(account: { id: TAccount["id"] }): MaybePromise<void>;
delete(accountId: TAccount["id"]): MaybePromise<void>;
has(account: { id: TAccount["id"] }): MaybePromise<boolean>;
has(accountId: TAccount["id"]): MaybePromise<boolean>;
values(): Iterable<TAccount>;
};
}
3 changes: 2 additions & 1 deletion packages/core/src/wallets/provider.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { MaybePromise } from "../types.js";
import type { Wallet } from "./wallet.js";

export abstract class WalletProvider {
abstract getWallets(): Wallet[] | Promise<Wallet[]>;
abstract getWallets(): MaybePromise<Wallet[]>;
}
9 changes: 5 additions & 4 deletions packages/core/src/wallets/wallet.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand All @@ -24,17 +25,17 @@ export abstract class Wallet<
this.#storage = (options?.storage ?? defaultStorage).join("wallet");
}

abstract initialize(): void | Promise<void>;
abstract initialize(): MaybePromise<void>;

abstract readonly connected$: Observable<boolean>;

abstract connect(): void | Promise<void>;
abstract connect(): MaybePromise<void>;

abstract disconnect(): void | Promise<void>;
abstract disconnect(): MaybePromise<void>;

abstract readonly accounts$: Observable<PolkadotSignerAccount[]>;

getAccounts(): PolkadotSignerAccount[] | Promise<PolkadotSignerAccount[]> {
getAccounts(): MaybePromise<PolkadotSignerAccount[]> {
return firstValueFrom(this.accounts$, { defaultValue: [] });
}
}

0 comments on commit 87f21cb

Please sign in to comment.