Refactor wallet storage and subscription handling
- Updated WalletStore to manage localStorage and handle storage events more effectively. - Introduced mockAccountsSubscribe in constants for better account subscription handling. - Removed unnecessary storage subscription from InjectionConfig. - Enhanced Wallet class to utilize new account and metadata management methods.
This commit is contained in:
1 parent
1966988769
commit
38574daace
6 files changed
+78
-44
No files matched your search
+11
-6
@@ -20,9 +20,14 @@ const defaultStore: WalletStoreType = {
|
||||
}
|
||||
|
||||
export class WalletStore {
|
||||
constructor(
|
||||
private readonly store: WalletStorage,
|
||||
) {
|
||||
private readonly store: WalletStorage;
|
||||
private readonly storeSubscribe: (cb: (ev: StorageEvent) => void) => void;
|
||||
|
||||
constructor() {
|
||||
this.store = window.localStorage as WalletStorage;
|
||||
this.storeSubscribe = (cb): void => {
|
||||
window.addEventListener('storage', cb);
|
||||
};
|
||||
this._setupListeners();
|
||||
}
|
||||
|
||||
@@ -38,12 +43,12 @@ export class WalletStore {
|
||||
};
|
||||
|
||||
private get _store(): WalletStoreType {
|
||||
return JSON.parse(this.store.getItem(VARAN_WALLET_LS_KEY) || '{}') as WalletStoreType;
|
||||
return Object.assign(defaultStore, JSON.parse(this.store.getItem(VARAN_WALLET_LS_KEY) || '{}')) as WalletStoreType;
|
||||
}
|
||||
|
||||
private _setupListeners(): void {
|
||||
let currentState = this._store;
|
||||
this.store.subscribe(ev => {
|
||||
this.storeSubscribe(ev => {
|
||||
ev.stopImmediatePropagation();
|
||||
if (ev.key !== VARAN_WALLET_LS_KEY) return;
|
||||
|
||||
@@ -103,7 +108,7 @@ export class WalletStore {
|
||||
}
|
||||
|
||||
get<Key extends keyof WalletStoreType>(key: Key): WalletStoreType[Key] {
|
||||
return this._store[key];
|
||||
return this._store[key] || defaultStore[key];
|
||||
}
|
||||
|
||||
set<Key extends keyof WalletStoreType>(key: Key, value: WalletStoreType[Key]): void {
|
||||
|
||||
Reference in new issue
Block a user