Skip to content

Commit

Permalink
fix: try to fix bootstrap issue.
Browse files Browse the repository at this point in the history
  • Loading branch information
richardo2016x committed Jul 3, 2024
1 parent 39868e2 commit 77b70ba
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
6 changes: 5 additions & 1 deletion apps/mobile/src/core/storage/mmkv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import { MMKV, MMKVConfiguration } from 'react-native-mmkv';

import { stringUtils } from '@rabby-wallet/base-utils';
import { StorageAdapater } from '@rabby-wallet/persist-store';
import { atomWithStorage, createJSONStorage } from 'jotai/utils';
import { SyncStorage } from 'jotai/vanilla/utils/atomWithStorage';
Expand All @@ -12,7 +13,10 @@ export function makeAppStorage(options?: MMKVConfiguration) {

function getItem<T>(key: string): T | null {
const value = mmkv.getString(key);
return value ? JSON.parse(value) : null;

return !value
? null
: stringUtils.safeParseJSON(value, { defaultValue: null });
}

function setItem<T>(key: string, value: T): void {
Expand Down
8 changes: 8 additions & 0 deletions packages/base-utils/src/isomorphic/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,11 @@ export function unSuffix(str = '', suffix = '/') {
export function isStringOrNumber(data: any) {
return typeof data === 'string' || typeof data === 'number';
}

export function safeParseJSON<T = any>(json: string, options?: { defaultValue?: any }): T | null {
try {
return JSON.parse(json);
} catch (error) {
return options?.defaultValue ?? null;
}
}

0 comments on commit 77b70ba

Please sign in to comment.