react-native-mmkv 2.12.2 → 3.0.0-beta.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -200
- package/MMKV/Core/Core.xcodeproj/project.pbxproj +4 -2
- package/MMKV/Core/MMKVPredef.h +1 -1
- package/MMKV/Core/MMKV_IO.cpp +5 -1
- package/MMKV/Core/MemoryFile.cpp +1 -0
- package/MMKV/Core/MemoryFile_OSX.cpp +2 -1
- package/MMKV/README.md +8 -8
- package/README.md +35 -4
- package/android/CMakeLists.txt +20 -28
- package/android/build.gradle +59 -89
- package/android/gradle.properties +4 -5
- package/android/src/main/AndroidManifest.xml +2 -2
- package/android/src/{hasNamespace/AndroidManifest.xml → main/AndroidManifestNew.xml} +0 -1
- package/android/src/main/cpp/AndroidLogger.cpp +16 -0
- package/android/src/main/cpp/cpp-adapter.cpp +4 -80
- package/android/src/main/java/com/mrousavy/mmkv/MmkvPackage.java +44 -0
- package/android/src/main/java/com/mrousavy/mmkv/MmkvPlatformContextModule.java +17 -0
- package/cpp/Logger.h +35 -0
- package/cpp/MMKVManagedBuffer.h +32 -0
- package/{android/src/main/cpp → cpp}/MmkvHostObject.cpp +126 -63
- package/{android/src/main/cpp → cpp}/MmkvHostObject.h +9 -3
- package/cpp/NativeMmkvModule.cpp +46 -0
- package/cpp/NativeMmkvModule.h +37 -0
- package/ios/AppleLogger.mm +16 -0
- package/ios/MmkvOnLoad.mm +25 -0
- package/ios/MmkvPlatformContext.h +19 -0
- package/ios/MmkvPlatformContextModule.mm +37 -0
- package/lib/commonjs/LazyTurboModule.js +49 -0
- package/lib/commonjs/LazyTurboModule.js.map +1 -0
- package/lib/commonjs/MMKV.js +28 -4
- package/lib/commonjs/MMKV.js.map +1 -1
- package/lib/commonjs/NativeMmkv.js +35 -0
- package/lib/commonjs/NativeMmkv.js.map +1 -0
- package/lib/commonjs/NativeMmkvPlatformContext.js +13 -0
- package/lib/commonjs/NativeMmkvPlatformContext.js.map +1 -0
- package/lib/commonjs/PlatformChecker.js.map +1 -1
- package/lib/commonjs/Types.js +2 -0
- package/lib/commonjs/Types.js.map +1 -0
- package/lib/commonjs/createMMKV.js +6 -43
- package/lib/commonjs/createMMKV.js.map +1 -1
- package/lib/commonjs/createMMKV.mock.js +5 -1
- package/lib/commonjs/createMMKV.mock.js.map +1 -1
- package/lib/commonjs/createMMKV.web.js +8 -4
- package/lib/commonjs/createMMKV.web.js.map +1 -1
- package/lib/commonjs/createTextEncoder.js.map +1 -1
- package/lib/commonjs/hooks.js +11 -10
- package/lib/commonjs/hooks.js.map +1 -1
- package/lib/commonjs/index.js.map +1 -1
- package/lib/module/LazyTurboModule.js +43 -0
- package/lib/module/LazyTurboModule.js.map +1 -0
- package/lib/module/MMKV.js +16 -4
- package/lib/module/MMKV.js.map +1 -1
- package/lib/module/NativeMmkv.js +32 -0
- package/lib/module/NativeMmkv.js.map +1 -0
- package/lib/module/NativeMmkvPlatformContext.js +7 -0
- package/lib/module/NativeMmkvPlatformContext.js.map +1 -0
- package/lib/module/PlatformChecker.js.map +1 -1
- package/lib/module/Types.js +2 -0
- package/lib/module/Types.js.map +1 -0
- package/lib/module/createMMKV.js +6 -43
- package/lib/module/createMMKV.js.map +1 -1
- package/lib/module/createMMKV.mock.js +5 -1
- package/lib/module/createMMKV.mock.js.map +1 -1
- package/lib/module/createMMKV.web.js +6 -4
- package/lib/module/createMMKV.web.js.map +1 -1
- package/lib/module/createTextEncoder.js.map +1 -1
- package/lib/module/hooks.js +7 -2
- package/lib/module/hooks.js.map +1 -1
- package/lib/module/index.js.map +1 -1
- package/lib/typescript/src/LazyTurboModule.d.ts +6 -0
- package/lib/typescript/src/LazyTurboModule.d.ts.map +1 -0
- package/lib/typescript/src/MMKV.d.ts +35 -0
- package/lib/typescript/src/MMKV.d.ts.map +1 -0
- package/lib/typescript/src/NativeMmkv.d.ts +83 -0
- package/lib/typescript/src/NativeMmkv.d.ts.map +1 -0
- package/lib/typescript/src/NativeMmkvPlatformContext.d.ts +9 -0
- package/lib/typescript/src/NativeMmkvPlatformContext.d.ts.map +1 -0
- package/lib/typescript/src/PlatformChecker.d.ts.map +1 -0
- package/lib/typescript/src/Types.d.ts +86 -0
- package/lib/typescript/src/Types.d.ts.map +1 -0
- package/lib/typescript/src/__tests__/hooks.test.d.ts +2 -0
- package/lib/typescript/src/__tests__/hooks.test.d.ts.map +1 -0
- package/lib/typescript/src/createMMKV.d.ts +4 -0
- package/lib/typescript/src/createMMKV.d.ts.map +1 -0
- package/lib/typescript/src/createMMKV.mock.d.ts +3 -0
- package/lib/typescript/src/createMMKV.mock.d.ts.map +1 -0
- package/lib/typescript/src/createMMKV.web.d.ts +4 -0
- package/lib/typescript/src/createMMKV.web.d.ts.map +1 -0
- package/lib/typescript/src/createTextEncoder.d.ts.map +1 -0
- package/lib/typescript/{hooks.d.ts → src/hooks.d.ts} +7 -4
- package/lib/typescript/src/hooks.d.ts.map +1 -0
- package/lib/typescript/src/index.d.ts.map +1 -0
- package/package.json +52 -40
- package/react-native-mmkv.podspec +17 -18
- package/react-native.config.js +20 -0
- package/src/LazyTurboModule.ts +62 -0
- package/src/MMKV.ts +19 -132
- package/src/NativeMmkv.ts +105 -0
- package/src/NativeMmkvPlatformContext.ts +14 -0
- package/src/Types.ts +89 -0
- package/src/__tests__/hooks.test.tsx +89 -0
- package/src/createMMKV.mock.ts +7 -3
- package/src/createMMKV.ts +10 -65
- package/src/createMMKV.web.ts +7 -2
- package/src/hooks.ts +17 -11
- package/android/src/main/java/com/reactnativemmkv/MmkvModule.java +0 -49
- package/android/src/main/java/com/reactnativemmkv/MmkvPackage.java +0 -26
- package/cpp/TypedArray.cpp +0 -322
- package/cpp/TypedArray.h +0 -153
- package/ios/JSIUtils.h +0 -38
- package/ios/JSIUtils.mm +0 -167
- package/ios/Mmkv.xcodeproj/project.pbxproj +0 -291
- package/ios/MmkvHostObject.h +0 -27
- package/ios/MmkvHostObject.mm +0 -295
- package/ios/MmkvModule.h +0 -5
- package/ios/MmkvModule.mm +0 -101
- package/lib/typescript/MMKV.d.ts +0 -142
- package/lib/typescript/MMKV.d.ts.map +0 -1
- package/lib/typescript/PlatformChecker.d.ts.map +0 -1
- package/lib/typescript/createMMKV.d.ts +0 -7
- package/lib/typescript/createMMKV.d.ts.map +0 -1
- package/lib/typescript/createMMKV.mock.d.ts +0 -3
- package/lib/typescript/createMMKV.mock.d.ts.map +0 -1
- package/lib/typescript/createMMKV.web.d.ts +0 -3
- package/lib/typescript/createMMKV.web.d.ts.map +0 -1
- package/lib/typescript/createTextEncoder.d.ts.map +0 -1
- package/lib/typescript/hooks.d.ts.map +0 -1
- package/lib/typescript/index.d.ts.map +0 -1
- /package/lib/typescript/{PlatformChecker.d.ts → src/PlatformChecker.d.ts} +0 -0
- /package/lib/typescript/{createTextEncoder.d.ts → src/createTextEncoder.d.ts} +0 -0
- /package/lib/typescript/{index.d.ts → src/index.d.ts} +0 -0
package/src/Types.ts
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Represents a single MMKV instance.
|
|
3
|
+
*/
|
|
4
|
+
export interface NativeMMKV {
|
|
5
|
+
/**
|
|
6
|
+
* Set a value for the given `key`.
|
|
7
|
+
*/
|
|
8
|
+
set: (key: string, value: boolean | string | number | ArrayBuffer) => void;
|
|
9
|
+
/**
|
|
10
|
+
* Get the boolean value for the given `key`, or `undefined` if it does not exist.
|
|
11
|
+
*
|
|
12
|
+
* @default undefined
|
|
13
|
+
*/
|
|
14
|
+
getBoolean: (key: string) => boolean | undefined;
|
|
15
|
+
/**
|
|
16
|
+
* Get the string value for the given `key`, or `undefined` if it does not exist.
|
|
17
|
+
*
|
|
18
|
+
* @default undefined
|
|
19
|
+
*/
|
|
20
|
+
getString: (key: string) => string | undefined;
|
|
21
|
+
/**
|
|
22
|
+
* Get the number value for the given `key`, or `undefined` if it does not exist.
|
|
23
|
+
*
|
|
24
|
+
* @default undefined
|
|
25
|
+
*/
|
|
26
|
+
getNumber: (key: string) => number | undefined;
|
|
27
|
+
/**
|
|
28
|
+
* Get a raw buffer of unsigned 8-bit (0-255) data.
|
|
29
|
+
*
|
|
30
|
+
* @default undefined
|
|
31
|
+
*/
|
|
32
|
+
getBuffer: (key: string) => ArrayBuffer | undefined;
|
|
33
|
+
/**
|
|
34
|
+
* Checks whether the given `key` is being stored in this MMKV instance.
|
|
35
|
+
*/
|
|
36
|
+
contains: (key: string) => boolean;
|
|
37
|
+
/**
|
|
38
|
+
* Delete the given `key`.
|
|
39
|
+
*/
|
|
40
|
+
delete: (key: string) => void;
|
|
41
|
+
/**
|
|
42
|
+
* Get all keys.
|
|
43
|
+
*
|
|
44
|
+
* @default []
|
|
45
|
+
*/
|
|
46
|
+
getAllKeys: () => string[];
|
|
47
|
+
/**
|
|
48
|
+
* Delete all keys.
|
|
49
|
+
*/
|
|
50
|
+
clearAll: () => void;
|
|
51
|
+
/**
|
|
52
|
+
* Sets (or updates) the encryption-key to encrypt all data in this MMKV instance with.
|
|
53
|
+
*
|
|
54
|
+
* To remove encryption, pass `undefined` as a key.
|
|
55
|
+
*
|
|
56
|
+
* Encryption keys can have a maximum length of 16 bytes.
|
|
57
|
+
*/
|
|
58
|
+
recrypt: (key: string | undefined) => void;
|
|
59
|
+
/**
|
|
60
|
+
* Trims the storage space and clears memory cache.
|
|
61
|
+
*
|
|
62
|
+
* Since MMKV does not resize itself after deleting keys, you can call `trim()`
|
|
63
|
+
* after deleting a bunch of keys to manually trim the memory- and
|
|
64
|
+
* disk-file to reduce storage and memory usage.
|
|
65
|
+
*
|
|
66
|
+
* In most applications, this is not needed at all.
|
|
67
|
+
*/
|
|
68
|
+
trim(): void;
|
|
69
|
+
/**
|
|
70
|
+
* Get the current total size of the storage, in bytes.
|
|
71
|
+
*/
|
|
72
|
+
readonly size: number;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export interface Listener {
|
|
76
|
+
remove: () => void;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export interface MMKVInterface extends NativeMMKV {
|
|
80
|
+
/**
|
|
81
|
+
* Adds a value changed listener. The Listener will be called whenever any value
|
|
82
|
+
* in this storage instance changes (set or delete).
|
|
83
|
+
*
|
|
84
|
+
* To unsubscribe from value changes, call `remove()` on the Listener.
|
|
85
|
+
*/
|
|
86
|
+
addOnValueChangedListener: (
|
|
87
|
+
onValueChanged: (key: string) => void
|
|
88
|
+
) => Listener;
|
|
89
|
+
}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { Button, Text } from 'react-native';
|
|
3
|
+
import {
|
|
4
|
+
act,
|
|
5
|
+
fireEvent,
|
|
6
|
+
render,
|
|
7
|
+
renderHook,
|
|
8
|
+
screen,
|
|
9
|
+
} from '@testing-library/react-native';
|
|
10
|
+
import { MMKV, useMMKVNumber, useMMKVString } from '..';
|
|
11
|
+
|
|
12
|
+
const mmkv = new MMKV();
|
|
13
|
+
|
|
14
|
+
beforeEach(() => {
|
|
15
|
+
mmkv.clearAll();
|
|
16
|
+
mmkv.trim();
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
test('hooks update when the value is changed directly through the instance', () => {
|
|
20
|
+
const { result } = renderHook(() => useMMKVString('string-key', mmkv));
|
|
21
|
+
|
|
22
|
+
expect(result.current[0]).toBeUndefined();
|
|
23
|
+
|
|
24
|
+
// First, make a "normal" change
|
|
25
|
+
act(() => {
|
|
26
|
+
result.current[1]('value 1');
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
expect(result.current[0]).toStrictEqual('value 1');
|
|
30
|
+
|
|
31
|
+
// Now, make the change directly through the instance.
|
|
32
|
+
act(() => {
|
|
33
|
+
mmkv.set('string-key', 'value 2');
|
|
34
|
+
});
|
|
35
|
+
expect(result.current[0]).toStrictEqual('value 2');
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
test('functional updates to hooks', () => {
|
|
39
|
+
const Component: React.FC = () => {
|
|
40
|
+
const [state, setState] = React.useState(0);
|
|
41
|
+
const [value, setValue] = useMMKVNumber('number-key', mmkv);
|
|
42
|
+
|
|
43
|
+
return (
|
|
44
|
+
<>
|
|
45
|
+
<Button
|
|
46
|
+
testID="button"
|
|
47
|
+
title="Double Increment Me"
|
|
48
|
+
onPress={() => {
|
|
49
|
+
// Increment the state value twice, using the function form of useState.
|
|
50
|
+
setState((current) => current + 1);
|
|
51
|
+
setState((current) => current + 1);
|
|
52
|
+
|
|
53
|
+
// Increment the MMKV value twice, using the same function form.
|
|
54
|
+
setValue((current) => (current ?? 0) + 1);
|
|
55
|
+
setValue((current) => (current ?? 0) + 1);
|
|
56
|
+
}}
|
|
57
|
+
/>
|
|
58
|
+
<Text testID="state-value">State: {state.toString()}</Text>
|
|
59
|
+
<Text testID="mmkv-value">MMKV: {(value ?? 0).toString()}</Text>
|
|
60
|
+
</>
|
|
61
|
+
);
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
render(<Component />);
|
|
65
|
+
|
|
66
|
+
const button = screen.getByTestId('button');
|
|
67
|
+
|
|
68
|
+
// Why these assertions:
|
|
69
|
+
// https://github.com/mrousavy/react-native-mmkv/issues/599
|
|
70
|
+
fireEvent.press(button);
|
|
71
|
+
expect(screen.getByTestId('state-value').children).toStrictEqual([
|
|
72
|
+
'State: ',
|
|
73
|
+
'2',
|
|
74
|
+
]);
|
|
75
|
+
expect(screen.getByTestId('mmkv-value').children).toStrictEqual([
|
|
76
|
+
'MMKV: ',
|
|
77
|
+
'2',
|
|
78
|
+
]);
|
|
79
|
+
|
|
80
|
+
fireEvent.press(button);
|
|
81
|
+
expect(screen.getByTestId('state-value').children).toStrictEqual([
|
|
82
|
+
'State: ',
|
|
83
|
+
'4',
|
|
84
|
+
]);
|
|
85
|
+
expect(screen.getByTestId('mmkv-value').children).toStrictEqual([
|
|
86
|
+
'MMKV: ',
|
|
87
|
+
'4',
|
|
88
|
+
]);
|
|
89
|
+
});
|
package/src/createMMKV.mock.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import type { NativeMMKV } from '
|
|
1
|
+
import type { NativeMMKV } from './Types';
|
|
2
2
|
|
|
3
3
|
/* Mock MMKV instance for use in tests */
|
|
4
4
|
export const createMockMMKV = (): NativeMMKV => {
|
|
5
|
-
const storage = new Map<string, string | boolean | number |
|
|
5
|
+
const storage = new Map<string, string | boolean | number | ArrayBuffer>();
|
|
6
6
|
|
|
7
7
|
return {
|
|
8
8
|
clearAll: () => storage.clear(),
|
|
@@ -22,12 +22,16 @@ export const createMockMMKV = (): NativeMMKV => {
|
|
|
22
22
|
},
|
|
23
23
|
getBuffer: (key) => {
|
|
24
24
|
const result = storage.get(key);
|
|
25
|
-
return result instanceof
|
|
25
|
+
return result instanceof ArrayBuffer ? result : undefined;
|
|
26
26
|
},
|
|
27
27
|
getAllKeys: () => Array.from(storage.keys()),
|
|
28
28
|
contains: (key) => storage.has(key),
|
|
29
29
|
recrypt: () => {
|
|
30
30
|
console.warn('Encryption is not supported in mocked MMKV instances!');
|
|
31
31
|
},
|
|
32
|
+
size: 0,
|
|
33
|
+
trim: () => {
|
|
34
|
+
// no-op
|
|
35
|
+
},
|
|
32
36
|
};
|
|
33
37
|
};
|
package/src/createMMKV.ts
CHANGED
|
@@ -1,70 +1,15 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
// Root directory of all MMKV stores
|
|
11
|
-
const ROOT_DIRECTORY: string | null = null;
|
|
12
|
-
|
|
13
|
-
export const createMMKV = (config: MMKVConfiguration): NativeMMKV => {
|
|
14
|
-
// Check if the constructor exists. If not, try installing the JSI bindings.
|
|
15
|
-
if (global.mmkvCreateNewInstance == null) {
|
|
16
|
-
// Get the native MMKV ReactModule
|
|
17
|
-
const MMKVModule = NativeModules.MMKV;
|
|
18
|
-
if (MMKVModule == null) {
|
|
19
|
-
let message =
|
|
20
|
-
'Failed to create a new MMKV instance: The native MMKV Module could not be found.';
|
|
21
|
-
message +=
|
|
22
|
-
'\n* Make sure react-native-mmkv is correctly autolinked (run `npx react-native config` to verify)';
|
|
23
|
-
if (Platform.OS === 'ios' || Platform.OS === 'macos') {
|
|
24
|
-
message += '\n* Make sure you ran `pod install` in the ios/ directory.';
|
|
25
|
-
}
|
|
26
|
-
if (Platform.OS === 'android') {
|
|
27
|
-
message += '\n* Make sure gradle is synced.';
|
|
28
|
-
}
|
|
29
|
-
// check if Expo
|
|
30
|
-
const ExpoConstants =
|
|
31
|
-
NativeModules.NativeUnimoduleProxy?.modulesConstants?.ExponentConstants;
|
|
32
|
-
if (ExpoConstants != null) {
|
|
33
|
-
if (ExpoConstants.appOwnership === 'expo') {
|
|
34
|
-
// We're running Expo Go
|
|
35
|
-
throw new Error(
|
|
36
|
-
'react-native-mmkv is not supported in Expo Go! Use EAS (`expo prebuild`) or eject to a bare workflow instead.'
|
|
37
|
-
);
|
|
38
|
-
} else {
|
|
39
|
-
// We're running Expo bare / standalone
|
|
40
|
-
message += '\n* Make sure you ran `expo prebuild`.';
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
message += '\n* Make sure you rebuilt the app.';
|
|
45
|
-
throw new Error(message);
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
// Check if we are running on-device (JSI)
|
|
49
|
-
if (global.nativeCallSyncHook == null || MMKVModule.install == null) {
|
|
1
|
+
import type { Configuration } from './MMKV';
|
|
2
|
+
import { MMKVTurboModule } from './NativeMmkv';
|
|
3
|
+
import type { NativeMMKV } from './Types';
|
|
4
|
+
|
|
5
|
+
export const createMMKV = (config: Configuration): NativeMMKV => {
|
|
6
|
+
const instance = MMKVTurboModule.createMMKV(config);
|
|
7
|
+
if (__DEV__) {
|
|
8
|
+
if (typeof instance !== 'object' || instance == null) {
|
|
50
9
|
throw new Error(
|
|
51
|
-
'Failed to create
|
|
10
|
+
'Failed to create MMKV instance - an unknown object was returned by createMMKV(..)!'
|
|
52
11
|
);
|
|
53
12
|
}
|
|
54
|
-
|
|
55
|
-
// Call the synchronous blocking install() function
|
|
56
|
-
const result = MMKVModule.install(ROOT_DIRECTORY);
|
|
57
|
-
if (result !== true)
|
|
58
|
-
throw new Error(
|
|
59
|
-
`Failed to create a new MMKV instance: The native MMKV Module could not be installed! Looks like something went wrong when installing JSI bindings: ${result}`
|
|
60
|
-
);
|
|
61
|
-
|
|
62
|
-
// Check again if the constructor now exists. If not, throw an error.
|
|
63
|
-
if (global.mmkvCreateNewInstance == null)
|
|
64
|
-
throw new Error(
|
|
65
|
-
'Failed to create a new MMKV instance, the native initializer function does not exist. Are you trying to use MMKV from different JS Runtimes?'
|
|
66
|
-
);
|
|
67
13
|
}
|
|
68
|
-
|
|
69
|
-
return global.mmkvCreateNewInstance(config);
|
|
14
|
+
return instance as NativeMMKV;
|
|
70
15
|
};
|
package/src/createMMKV.web.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
/* global localStorage */
|
|
2
|
-
import type {
|
|
2
|
+
import type { Configuration } from './MMKV';
|
|
3
|
+
import type { NativeMMKV } from './Types';
|
|
3
4
|
import { createTextEncoder } from './createTextEncoder';
|
|
4
5
|
|
|
5
6
|
const canUseDOM =
|
|
@@ -19,7 +20,7 @@ const hasAccessToLocalStorage = () => {
|
|
|
19
20
|
const KEY_WILDCARD = '\\';
|
|
20
21
|
const inMemoryStorage = new Map<string, string>();
|
|
21
22
|
|
|
22
|
-
export const createMMKV = (config:
|
|
23
|
+
export const createMMKV = (config: Configuration): NativeMMKV => {
|
|
23
24
|
if (config.encryptionKey != null) {
|
|
24
25
|
throw new Error("MMKV: 'encryptionKey' is not supported on Web!");
|
|
25
26
|
}
|
|
@@ -118,5 +119,9 @@ export const createMMKV = (config: MMKVConfiguration): NativeMMKV => {
|
|
|
118
119
|
recrypt: () => {
|
|
119
120
|
throw new Error('`recrypt(..)` is not supported on Web!');
|
|
120
121
|
},
|
|
122
|
+
size: 0,
|
|
123
|
+
trim: () => {
|
|
124
|
+
// no-op
|
|
125
|
+
},
|
|
121
126
|
};
|
|
122
127
|
};
|
package/src/hooks.ts
CHANGED
|
@@ -1,16 +1,17 @@
|
|
|
1
1
|
import { useRef, useState, useMemo, useCallback, useEffect } from 'react';
|
|
2
|
-
import { MMKV,
|
|
2
|
+
import { MMKV, Configuration } from './MMKV';
|
|
3
3
|
|
|
4
4
|
function isConfigurationEqual(
|
|
5
|
-
left?:
|
|
6
|
-
right?:
|
|
5
|
+
left?: Configuration,
|
|
6
|
+
right?: Configuration
|
|
7
7
|
): boolean {
|
|
8
8
|
if (left == null || right == null) return left == null && right == null;
|
|
9
9
|
|
|
10
10
|
return (
|
|
11
11
|
left.encryptionKey === right.encryptionKey &&
|
|
12
12
|
left.id === right.id &&
|
|
13
|
-
left.path === right.path
|
|
13
|
+
left.path === right.path &&
|
|
14
|
+
left.mode === right.mode
|
|
14
15
|
);
|
|
15
16
|
}
|
|
16
17
|
|
|
@@ -30,10 +31,10 @@ export function useMMKV(): MMKV;
|
|
|
30
31
|
* Use a custom MMKV instance with the given configuration.
|
|
31
32
|
* @param configuration The configuration to initialize the MMKV instance with. Does not have to be memoized.
|
|
32
33
|
*/
|
|
33
|
-
export function useMMKV(configuration:
|
|
34
|
-
export function useMMKV(configuration?:
|
|
34
|
+
export function useMMKV(configuration: Configuration): MMKV;
|
|
35
|
+
export function useMMKV(configuration?: Configuration): MMKV {
|
|
35
36
|
const instance = useRef<MMKV>();
|
|
36
|
-
const lastConfiguration = useRef<
|
|
37
|
+
const lastConfiguration = useRef<Configuration>();
|
|
37
38
|
|
|
38
39
|
if (configuration == null) return getDefaultInstance();
|
|
39
40
|
|
|
@@ -49,9 +50,9 @@ export function useMMKV(configuration?: MMKVConfiguration): MMKV {
|
|
|
49
50
|
}
|
|
50
51
|
|
|
51
52
|
function createMMKVHook<
|
|
52
|
-
T extends (boolean | number | string |
|
|
53
|
+
T extends (boolean | number | string | ArrayBuffer) | undefined,
|
|
53
54
|
TSet extends T | undefined,
|
|
54
|
-
TSetAction extends TSet | ((current: T) => TSet)
|
|
55
|
+
TSetAction extends TSet | ((current: T) => TSet),
|
|
55
56
|
>(getter: (instance: MMKV, key: string) => T) {
|
|
56
57
|
return (
|
|
57
58
|
key: string,
|
|
@@ -82,7 +83,7 @@ function createMMKVHook<
|
|
|
82
83
|
mmkv.delete(key);
|
|
83
84
|
break;
|
|
84
85
|
case 'object':
|
|
85
|
-
if (newValue instanceof
|
|
86
|
+
if (newValue instanceof ArrayBuffer) {
|
|
86
87
|
mmkv.set(key, newValue);
|
|
87
88
|
break;
|
|
88
89
|
} else {
|
|
@@ -179,7 +180,12 @@ export const useMMKVBuffer = createMMKVHook((instance, key) =>
|
|
|
179
180
|
export function useMMKVObject<T>(
|
|
180
181
|
key: string,
|
|
181
182
|
instance?: MMKV
|
|
182
|
-
): [
|
|
183
|
+
): [
|
|
184
|
+
value: T | undefined,
|
|
185
|
+
setValue: (
|
|
186
|
+
value: T | undefined | ((prevValue: T | undefined) => T | undefined)
|
|
187
|
+
) => void,
|
|
188
|
+
] {
|
|
183
189
|
const [json, setJson] = useMMKVString(key, instance);
|
|
184
190
|
|
|
185
191
|
const value = useMemo(() => {
|
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
package com.reactnativemmkv;
|
|
2
|
-
|
|
3
|
-
import android.util.Log;
|
|
4
|
-
|
|
5
|
-
import androidx.annotation.NonNull;
|
|
6
|
-
import androidx.annotation.Nullable;
|
|
7
|
-
|
|
8
|
-
import com.facebook.react.bridge.JavaScriptContextHolder;
|
|
9
|
-
import com.facebook.react.bridge.ReactContextBaseJavaModule;
|
|
10
|
-
import com.facebook.react.bridge.ReactApplicationContext;
|
|
11
|
-
import com.facebook.react.bridge.ReactMethod;
|
|
12
|
-
import com.facebook.react.module.annotations.ReactModule;
|
|
13
|
-
|
|
14
|
-
@ReactModule(name = MmkvModule.NAME)
|
|
15
|
-
public class MmkvModule extends ReactContextBaseJavaModule {
|
|
16
|
-
public static final String NAME = "MMKV";
|
|
17
|
-
|
|
18
|
-
public MmkvModule(ReactApplicationContext reactContext) {
|
|
19
|
-
super(reactContext);
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
@NonNull
|
|
23
|
-
@Override
|
|
24
|
-
public String getName() {
|
|
25
|
-
return NAME;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
@ReactMethod(isBlockingSynchronousMethod = true)
|
|
29
|
-
public boolean install(@Nullable String rootDirectory) {
|
|
30
|
-
try {
|
|
31
|
-
Log.i(NAME, "Loading C++ library...");
|
|
32
|
-
System.loadLibrary("reactnativemmkv");
|
|
33
|
-
|
|
34
|
-
JavaScriptContextHolder jsContext = getReactApplicationContext().getJavaScriptContextHolder();
|
|
35
|
-
if (rootDirectory == null) {
|
|
36
|
-
rootDirectory = getReactApplicationContext().getFilesDir().getAbsolutePath() + "/mmkv";
|
|
37
|
-
}
|
|
38
|
-
Log.i(NAME, "Installing MMKV JSI Bindings for MMKV root directory: " + rootDirectory);
|
|
39
|
-
nativeInstall(jsContext.get(), rootDirectory);
|
|
40
|
-
Log.i(NAME, "Successfully installed MMKV JSI Bindings!");
|
|
41
|
-
return true;
|
|
42
|
-
} catch (Exception exception) {
|
|
43
|
-
Log.e(NAME, "Failed to install MMKV JSI Bindings!", exception);
|
|
44
|
-
return false;
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
private static native void nativeInstall(long jsiPtr, String path);
|
|
49
|
-
}
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
package com.reactnativemmkv;
|
|
2
|
-
|
|
3
|
-
import androidx.annotation.NonNull;
|
|
4
|
-
|
|
5
|
-
import com.facebook.react.ReactPackage;
|
|
6
|
-
import com.facebook.react.bridge.NativeModule;
|
|
7
|
-
import com.facebook.react.bridge.ReactApplicationContext;
|
|
8
|
-
import com.facebook.react.uimanager.ViewManager;
|
|
9
|
-
|
|
10
|
-
import java.util.Collections;
|
|
11
|
-
import java.util.List;
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
public class MmkvPackage implements ReactPackage {
|
|
15
|
-
@NonNull
|
|
16
|
-
@Override
|
|
17
|
-
public List<NativeModule> createNativeModules(@NonNull ReactApplicationContext reactContext) {
|
|
18
|
-
return Collections.singletonList(new MmkvModule(reactContext));
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
@NonNull
|
|
22
|
-
@Override
|
|
23
|
-
public List<ViewManager> createViewManagers(@NonNull ReactApplicationContext reactContext) {
|
|
24
|
-
return Collections.emptyList();
|
|
25
|
-
}
|
|
26
|
-
}
|