Skip to content

Commit

Permalink
Merge pull request #656 from ephemeraHQ/noe/mmkv-memory-leak
Browse files Browse the repository at this point in the history
Perf MMKV: Avoid multiple writes to same key, use different instances zustand / react-query
  • Loading branch information
nmalzieu authored Sep 3, 2024
2 parents f564328 + 28586be commit c8f1641
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions utils/mmkv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ export default storage;

export const zustandMMKVStorage: StateStorage = {
setItem: (name, value) => {
// Deleting before setting to avoid memory leak
// https://github.com/mrousavy/react-native-mmkv/issues/440
storage.delete(name);
return storage.set(name, value);
},
getItem: (name) => {
Expand Down Expand Up @@ -47,17 +50,22 @@ export const clearSecureMmkvForAccount = async (account: string) => {
delete secureMmkvByAccount[account];
};

const reactQueryPersister = new MMKV({ id: "converse-react-query" });

export const mmkvStoragePersister = createSyncStoragePersister({
storage: {
setItem: (key, value) => {
storage.set(key, value);
// Deleting before setting to avoid memory leak
// https://github.com/mrousavy/react-native-mmkv/issues/440
reactQueryPersister.delete(key);
reactQueryPersister.set(key, value);
},
getItem: (key) => {
const value = storage.getString(key);
const value = reactQueryPersister.getString(key);
return value === undefined ? null : value;
},
removeItem: (key) => {
storage.delete(key);
reactQueryPersister.delete(key);
},
},
serialize: stringify,
Expand Down

0 comments on commit c8f1641

Please sign in to comment.