Skip to content

Commit

Permalink
refactor: rename PrefixedStorage to Storage
Browse files Browse the repository at this point in the history
  • Loading branch information
tien committed Nov 3, 2024
1 parent bbda9ef commit 6b90af4
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 19 deletions.
5 changes: 5 additions & 0 deletions .changeset/real-moons-invent.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@reactive-dot/core": patch
---

Renamed `PrefixedStorage` to `Storage`.
2 changes: 1 addition & 1 deletion packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ export { defineConfig, type ChainConfig, type Config } from "./config.js";
export { MutationError, QueryError, ReactiveDotError } from "./errors.js";
export { Query } from "./query-builder.js";
export type { Register } from "./register.js";
export { PrefixedStorage } from "./storage.js";
export { Storage } from "./storage.js";
export { idle, pending } from "./symbols.js";
1 change: 1 addition & 0 deletions packages/core/src/simple-storage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type SimpleStorage = Pick<Storage, "getItem" | "removeItem" | "setItem">;
29 changes: 14 additions & 15 deletions packages/core/src/storage.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
export type SimpleStorage = Pick<Storage, "getItem" | "removeItem" | "setItem">;
import type { SimpleStorage } from "./simple-storage.js";

export type PrefixedStorageOptions = {
export type StorageOptions = {
prefix: string;
storage: SimpleStorage;
storage: SimpleStorage | (() => SimpleStorage);
};

export class PrefixedStorage<TKey extends string = string>
implements SimpleStorage
{
export class Storage<TKey extends string = string> implements SimpleStorage {
readonly prefix: string;

#storage: SimpleStorage;

constructor(options: PrefixedStorageOptions) {
constructor(options: StorageOptions) {
this.prefix = options.prefix;
this.#storage = options.storage;
this.#storage =
typeof options.storage === "function"
? options.storage()
: options.storage;
}

getItem(key: TKey) {
Expand All @@ -30,20 +31,18 @@ export class PrefixedStorage<TKey extends string = string>
}

join<TKeyOverride extends string | void = void>(path: string) {
return new PrefixedStorage<TKeyOverride extends void ? TKey : TKeyOverride>(
{
prefix: `${this.prefix}/${path}`,
storage: this.#storage,
},
);
return new Storage<TKeyOverride extends void ? TKey : TKeyOverride>({
prefix: `${this.prefix}/${path}`,
storage: this.#storage,
});
}

#prefixKey(key: string) {
return `${this.prefix}/${key}`;
}
}

export const defaultStorage = new PrefixedStorage({
export const defaultStorage = new Storage({
prefix: "@reactive-dot",
storage: globalThis.localStorage,
});
6 changes: 3 additions & 3 deletions packages/core/src/wallets/wallet.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { type PrefixedStorage, defaultStorage } from "../storage.js";
import { type Storage, defaultStorage } from "../storage.js";
import type { PolkadotSignerAccount } from "./account.js";
import { firstValueFrom, type Observable } from "rxjs";

export type WalletOptions = {
storage?: PrefixedStorage | undefined;
storage?: Storage | undefined;
};

export abstract class Wallet<
Expand All @@ -14,7 +14,7 @@ export abstract class Wallet<

abstract readonly name: string;

readonly #storage: PrefixedStorage;
readonly #storage: Storage;

protected get storage() {
return this.#storage.join<TStorageKey>(this.id);
Expand Down

0 comments on commit 6b90af4

Please sign in to comment.