Skip to content

Commit

Permalink
Merge pull request #150 from ospfranco/mmkv
Browse files Browse the repository at this point in the history
Use MMKV instead of async storage
  • Loading branch information
ospfranco authored Jan 23, 2025
2 parents cf5d8a5 + 0b43d17 commit ec0c060
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 17 deletions.
49 changes: 41 additions & 8 deletions macos/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ PODS:
- hermes-engine/Pre-built (= 0.76.6)
- hermes-engine/Pre-built (0.76.6)
- HotKey (0.2.1)
- MMKV (2.0.2):
- MMKVCore (~> 2.0.2)
- MMKVCore (2.0.2)
- RCT-Folly (2024.01.01.00):
- boost
- DoubleConversion
Expand Down Expand Up @@ -1246,6 +1249,28 @@ PODS:
- Yoga
- react-native-get-random-values (1.11.0):
- React-Core
- react-native-mmkv (2.12.2):
- DoubleConversion
- glog
- hermes-engine
- MMKV (>= 1.3.3)
- RCT-Folly (= 2024.01.01.00)
- RCTRequired
- RCTTypeSafety
- React-Core
- React-debug
- React-Fabric
- React-featureflags
- React-graphics
- React-ImageManager
- React-NativeModulesApple
- React-RCTFabric
- React-rendererdebug
- React-utils
- ReactCodegen
- ReactCommon/turbomodule/bridging
- ReactCommon/turbomodule/core
- Yoga
- React-nativeconfig (0.76.6)
- React-NativeModulesApple (0.76.6):
- glog
Expand Down Expand Up @@ -1515,7 +1540,7 @@ PODS:
- React-utils (= 0.76.6)
- RNCAsyncStorage (1.24.0):
- React-Core
- RNReanimated (3.16.6):
- RNReanimated (3.16.7):
- DoubleConversion
- glog
- hermes-engine
Expand All @@ -1535,10 +1560,10 @@ PODS:
- ReactCodegen
- ReactCommon/turbomodule/bridging
- ReactCommon/turbomodule/core
- RNReanimated/reanimated (= 3.16.6)
- RNReanimated/worklets (= 3.16.6)
- RNReanimated/reanimated (= 3.16.7)
- RNReanimated/worklets (= 3.16.7)
- Yoga
- RNReanimated/reanimated (3.16.6):
- RNReanimated/reanimated (3.16.7):
- DoubleConversion
- glog
- hermes-engine
Expand All @@ -1558,9 +1583,9 @@ PODS:
- ReactCodegen
- ReactCommon/turbomodule/bridging
- ReactCommon/turbomodule/core
- RNReanimated/reanimated/apple (= 3.16.6)
- RNReanimated/reanimated/apple (= 3.16.7)
- Yoga
- RNReanimated/reanimated/apple (3.16.6):
- RNReanimated/reanimated/apple (3.16.7):
- DoubleConversion
- glog
- hermes-engine
Expand All @@ -1581,7 +1606,7 @@ PODS:
- ReactCommon/turbomodule/bridging
- ReactCommon/turbomodule/core
- Yoga
- RNReanimated/worklets (3.16.6):
- RNReanimated/worklets (3.16.7):
- DoubleConversion
- glog
- hermes-engine
Expand Down Expand Up @@ -1670,6 +1695,7 @@ DEPENDENCIES:
- React-Mapbuffer (from `../node_modules/react-native-macos/ReactCommon`)
- React-microtasksnativemodule (from `../node_modules/react-native-macos/ReactCommon/react/nativemodule/microtasks`)
- react-native-get-random-values (from `../node_modules/react-native-get-random-values`)
- react-native-mmkv (from `../node_modules/react-native-mmkv`)
- React-nativeconfig (from `../node_modules/react-native-macos/ReactCommon`)
- React-NativeModulesApple (from `../node_modules/react-native-macos/ReactCommon/react/nativemodule/core/platform/ios`)
- React-perflogger (from `../node_modules/react-native-macos/ReactCommon/reactperflogger`)
Expand Down Expand Up @@ -1707,6 +1733,8 @@ DEPENDENCIES:
SPEC REPOS:
trunk:
- HotKey
- MMKV
- MMKVCore
- Sentry
- Sparkle

Expand Down Expand Up @@ -1784,6 +1812,8 @@ EXTERNAL SOURCES:
:path: "../node_modules/react-native-macos/ReactCommon/react/nativemodule/microtasks"
react-native-get-random-values:
:path: "../node_modules/react-native-get-random-values"
react-native-mmkv:
:path: "../node_modules/react-native-mmkv"
React-nativeconfig:
:path: "../node_modules/react-native-macos/ReactCommon"
React-NativeModulesApple:
Expand Down Expand Up @@ -1857,6 +1887,8 @@ SPEC CHECKSUMS:
glog: 750f96a379703a2d724d2f1fc6cb3a3eff000817
hermes-engine: 1949ca944b195a8bde7cbf6316b9068e19cf53c6
HotKey: 400beb7caa29054ea8d864c96f5ba7e5b4852277
MMKV: 3eacda84cd1c4fc95cf848d3ecb69d85ed56006c
MMKVCore: 508b4d3a8ce031f1b5c8bd235f0517fb3f4c73a9
RCT-Folly: 38dded4465d7da30a12a3087110693a6799091d4
RCTDeprecation: 063fc281b30b7dc944c98fe53a7e266dab1a8706
RCTRequired: 74d3e5dbded3e4e62113ef04f5534b5aebe0f619
Expand Down Expand Up @@ -1887,6 +1919,7 @@ SPEC CHECKSUMS:
React-Mapbuffer: 54c6c6ef2f1aa1325437d7ff9b98d4d9074bc20c
React-microtasksnativemodule: 21e9f7479e65e2244302cb89872e17b0ada3be7e
react-native-get-random-values: d16467cf726c618e9c7a8c3c39c31faa2244bbba
react-native-mmkv: f84bf6f48c906911695f9cd16d39d4fb78fcd5a4
React-nativeconfig: f751aaeabc49b11e5a0dae4c0e1a709ab55c128c
React-NativeModulesApple: 624a15426130c68d4097088bc6950ef6ff8668ba
React-perflogger: 2db18ff24632e5ab5093e4f7f6b61ed830c9f17b
Expand Down Expand Up @@ -1915,7 +1948,7 @@ SPEC CHECKSUMS:
ReactCodegen: 489a96e05e7564c2bf0363be8319c07e762a3f4d
ReactCommon: a7a5981602782da52c9573e845c485dae2b37521
RNCAsyncStorage: b6410dead2732b5c72a7fdb1ecb5651bbcf4674b
RNReanimated: 2f762cd9e6b74c1f6ec88c0545d773a68b8e1085
RNReanimated: 66cf0f600a26d2b5e74c6e0b1c77c1ab1f62fc05
RNSentry: b1d9f61f725d3aba2ce943bfea23ac74b860ee89
Sentry: ee060c09b2f7ec1240e95c766ab44c04c7bdb052
SocketRocket: 03f7111df1a343b162bf5b06ead333be808e1e0a
Expand Down
10 changes: 10 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"react": "18.3.1",
"react-native": "^0.76.6",
"react-native-get-random-values": "^1.8.0",
"react-native-mmkv": "2.12.2",
"react-native-macos": "^0.76.6",
"react-native-reanimated": "^3.8.1",
"uuid": "^9.0.0"
Expand Down
12 changes: 11 additions & 1 deletion src/stores/clipboard.store.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {EmitterSubscription} from 'react-native'
import {IRootStore} from 'store'
import {Widget} from './ui.store'
import MiniSearch from 'minisearch'
import {storage} from './storage'

const MAX_ITEMS = 1000

Expand Down Expand Up @@ -85,7 +86,16 @@ export const createClipboardStore = (root: IRootStore) => {
onCopyListener = solNative.addListener('onTextCopied', store.onTextCopied)

const hydrate = async () => {
const state = await AsyncStorage.getItem('@clipboard.store')
let state: string | null | undefined
try {
state = storage.getString('@clipboard.store')
} catch {
// intentionally left blank
}
if (!state) {
state = await AsyncStorage.getItem('@clipboard.store')
}

if (state) {
const parsedStore = JSON.parse(state)
store.saveHistory = parsedStore.saveHistory
Expand Down
11 changes: 10 additions & 1 deletion src/stores/emoji.store.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {IRootStore} from 'store'
import {emojis as rawEmojis_} from '../lib/emojis'
import {solNative} from 'lib/SolNative'
import MiniSearch from 'minisearch'
import {storage} from './storage'

let rawEmojis = rawEmojis_.map((emoji: any, idx) => ({id: idx, ...emoji}))

Expand Down Expand Up @@ -55,7 +56,15 @@ export const createEmojiStore = (root: IRootStore) => {
}

let hydrate = async () => {
const storeState = await AsyncStorage.getItem('@emoji.store')
let storeState: string | null | undefined
try {
storeState = storage.getString('@emoji.store')
} catch {
// intentionally left blank
}
if (!storeState) {
storeState = await AsyncStorage.getItem('@emoji.store')
}

if (storeState) {
let parsedStore = JSON.parse(storeState)
Expand Down
3 changes: 3 additions & 0 deletions src/stores/storage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import {MMKV} from 'react-native-mmkv'

export const storage = new MMKV()
20 changes: 13 additions & 7 deletions src/stores/ui.store.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {createBaseItems} from './items'
import plist from '@expo/plist'
import MiniSearch from 'minisearch'
import * as Sentry from '@sentry/react-native'
import {storage} from './storage'
import {defaultShortcuts, validShortcutTokensRegex} from 'lib/shorcuts'

const exprParser = new Parser()
Expand Down Expand Up @@ -138,17 +139,22 @@ export const createUIStore = (root: IRootStore) => {
let persist = async () => {
let plainState = toJS(store)
try {
await AsyncStorage.setItem('@ui.store', JSON.stringify(plainState))
} catch (error) {
await AsyncStorage.clear()
await AsyncStorage.setItem('@ui.store', JSON.stringify(plainState)).catch(
e => console.warn('Could re-persist persist ui store', e),
)
storage.set('@ui.store', JSON.stringify(plainState))
} catch (e) {
Sentry.captureException(e)
}
}

let hydrate = async () => {
const storeState = await AsyncStorage.getItem('@ui.store')
let storeState: string | null | undefined
try {
storeState = storage.getString('@ui.store')
} catch {
// intentionally left blank
}
if (!storeState) {
storeState = await AsyncStorage.getItem('@ui.store')
}

if (storeState) {
let parsedStore = JSON.parse(storeState)
Expand Down

0 comments on commit ec0c060

Please sign in to comment.