Skip to content

Commit

Permalink
fix: not being able to delete wc sessions
Browse files Browse the repository at this point in the history
  • Loading branch information
TravellerOnTheRun committed Feb 29, 2024
1 parent befe4a8 commit 3c09570
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 31 deletions.
10 changes: 7 additions & 3 deletions src/screens/walletConnect/walletConnect2.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { getEnvSetting } from 'core/config'
import { SETTINGS } from 'core/types'
import { AcceptedValue, MMKVStorage } from 'storage/MMKVStorage'

const WC2 = 'WC2'

export type WalletConnect2SdkErrorString = Parameters<typeof getSdkError>[0]

const WalletConnect2SdkErrorEnum: { [P in WalletConnect2SdkErrorString]: P } = {
Expand Down Expand Up @@ -36,11 +38,13 @@ const WalletConnect2SdkErrorEnum: { [P in WalletConnect2SdkErrorString]: P } = {

type StorageTypeFromCore = InstanceType<typeof Core>['storage']

const MMKVWC = new MMKVStorage('WC2')
export const deleteWCSessions = MMKVWC.deleteAll
export const deleteWCSessions = () => {
const storage = new MMKVStorage('WC2')
storage.deleteAll()
}

class MMKVCoreStorage implements StorageTypeFromCore {
storage = MMKVWC
storage = new MMKVStorage(WC2)

getEntries<T = never>(): Promise<[string, T][]> {
const keys = this.storage.getAllKeys()
Expand Down
8 changes: 3 additions & 5 deletions src/storage/ChainStorage.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { ChainID } from 'lib/eoaWallet'

import { MMKVStorage } from 'storage/MMKVStorage'

const ChainStorage = new MMKVStorage('chainStorage')
import { MainStorage } from './MainStorage'

export const getCurrentChainId: () => ChainID = () =>
ChainStorage.get('chainId') || 31
MainStorage.get('chainId') || 31

export const setCurrentChainId = (chainId: ChainID) =>
ChainStorage.set('chainId', chainId)
MainStorage.set('chainId', chainId)
33 changes: 10 additions & 23 deletions src/storage/MMKVStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import { initializeMMKVFlipper } from 'react-native-mmkv-flipper-plugin'
export type AcceptedValue = boolean | string | number | object

export class MMKVStorage {
private id = 'mmkv.default'
private storage: MMKV | null = null
private id: string
private storage: MMKV

constructor(id = 'mmkv.default', encryptionKey?: string) {
constructor(id = 'main_storage', encryptionKey?: string) {
this.id = id
this.storage = new MMKV({
id,
Expand All @@ -20,42 +20,29 @@ export class MMKVStorage {
}

public set(key: string, value: AcceptedValue) {
if (this.storage && typeof value !== 'undefined') {
if (typeof value !== 'undefined') {
this.storage.set(key, JSON.stringify(value))
}
}

public get(key = 'default') {
if (this.storage) {
const value = this.storage.getString(key)
return value && JSON.parse(value)
}
const value = this.storage.getString(key)
return value && JSON.parse(value)
}

public has(key = 'default') {
if (this.storage) {
return this.storage.contains(key)
} else {
return false
}
return this.storage.contains(key)
}

public delete(key: string) {
if (this.storage) {
this.storage.delete(key)
}
this.storage.delete(key)
}

public deleteAll() {
if (this.storage) {
this.storage.clearAll()
}
this.storage.clearAll()
}

public getAllKeys() {
if (this.storage) {
return this.storage.getAllKeys()
}
return []
return this.storage.getAllKeys() || []
}
}

0 comments on commit 3c09570

Please sign in to comment.