-
-
Notifications
You must be signed in to change notification settings - Fork 263
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Trim MMKV on memoryWarning - GC safe via
WeakRef
!
- Loading branch information
Showing
2 changed files
with
31 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { AppState } from 'react-native'; | ||
import type { NativeEventSubscription } from 'react-native'; | ||
import { MMKVInterface } from './Types'; | ||
|
||
export function addMemoryWarningListener(mmkv: MMKVInterface): void { | ||
if (global.WeakRef != null && global.FinalizationRegistry != null) { | ||
// 1. Weakify MMKV so we can safely use it inside the memoryWarning event listener | ||
const weakMmkv = new WeakRef(mmkv); | ||
const listener = AppState.addEventListener('memoryWarning', () => { | ||
// 0. Everytime we receive a memoryWarning, we try to trim the MMKV instance (if it is still valid) | ||
weakMmkv.deref()?.trim(); | ||
}); | ||
// 2. Add a listener to when the MMKV instance is deleted | ||
const finalization = new FinalizationRegistry( | ||
(l: NativeEventSubscription) => { | ||
// 3. When MMKV is deleted, this listener will be called with the memoryWarning listener. | ||
l.remove(); | ||
} | ||
); | ||
// 2.1. Bind the listener to the actual MMKV instance. | ||
finalization.register(mmkv, listener); | ||
} else { | ||
// WeakRef/FinalizationRegistry is not implemented in this engine. | ||
// Just add the listener, even if it retains MMKV strong forever. | ||
AppState.addEventListener('memoryWarning', () => { | ||
mmkv.trim(); | ||
}); | ||
} | ||
} |