react-native-mmkv 3.0.0-beta.1 → 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/README.md +28 -3
- package/cpp/MMKVManagedBuffer.h +1 -6
- package/cpp/MmkvHostObject.h +1 -6
- package/cpp/NativeMmkvModule.cpp +7 -1
- package/lib/commonjs/LazyTurboModule.js +49 -0
- package/lib/commonjs/LazyTurboModule.js.map +1 -0
- package/lib/commonjs/MMKV.js +4 -10
- package/lib/commonjs/MMKV.js.map +1 -1
- package/lib/commonjs/NativeMmkv.js +13 -44
- package/lib/commonjs/NativeMmkv.js.map +1 -1
- package/lib/commonjs/NativeMmkvPlatformContext.js +5 -1
- package/lib/commonjs/NativeMmkvPlatformContext.js.map +1 -1
- package/lib/commonjs/Types.js +2 -0
- package/lib/commonjs/Types.js.map +1 -0
- package/lib/commonjs/createMMKV.js +1 -2
- package/lib/commonjs/createMMKV.js.map +1 -1
- package/lib/commonjs/createMMKV.web.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 +4 -11
- package/lib/module/MMKV.js.map +1 -1
- package/lib/module/NativeMmkv.js +10 -41
- package/lib/module/NativeMmkv.js.map +1 -1
- package/lib/module/NativeMmkvPlatformContext.js +5 -1
- package/lib/module/NativeMmkvPlatformContext.js.map +1 -1
- package/lib/module/Types.js +2 -0
- package/lib/module/Types.js.map +1 -0
- package/lib/module/createMMKV.js +2 -3
- package/lib/module/createMMKV.js.map +1 -1
- package/lib/module/createMMKV.web.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 +1 -85
- package/lib/typescript/src/MMKV.d.ts.map +1 -1
- package/lib/typescript/src/NativeMmkv.d.ts +9 -5
- package/lib/typescript/src/NativeMmkv.d.ts.map +1 -1
- package/lib/typescript/src/NativeMmkvPlatformContext.d.ts.map +1 -1
- package/lib/typescript/src/Types.d.ts +86 -0
- package/lib/typescript/src/Types.d.ts.map +1 -0
- package/lib/typescript/src/createMMKV.d.ts +2 -1
- package/lib/typescript/src/createMMKV.d.ts.map +1 -1
- package/lib/typescript/src/createMMKV.mock.d.ts +1 -1
- package/lib/typescript/src/createMMKV.mock.d.ts.map +1 -1
- package/lib/typescript/src/createMMKV.web.d.ts +2 -1
- package/lib/typescript/src/createMMKV.web.d.ts.map +1 -1
- package/lib/typescript/src/index.d.ts.map +1 -1
- package/package.json +17 -14
- package/react-native.config.js +20 -0
- package/src/LazyTurboModule.ts +62 -0
- package/src/MMKV.ts +5 -96
- package/src/NativeMmkv.ts +19 -46
- package/src/NativeMmkvPlatformContext.ts +5 -3
- package/src/Types.ts +89 -0
- package/src/__tests__/hooks.test.tsx +1 -0
- package/src/createMMKV.mock.ts +1 -1
- package/src/createMMKV.ts +4 -5
- package/src/createMMKV.web.ts +2 -1
- /package/src/{index.tsx → index.ts} +0 -0
package/src/NativeMmkv.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { TurboModule } from 'react-native';
|
|
2
2
|
import { TurboModuleRegistry } from 'react-native';
|
|
3
3
|
import { UnsafeObject } from 'react-native/Libraries/Types/CodegenTypes';
|
|
4
|
+
import { getLazyTurboModule } from './LazyTurboModule';
|
|
4
5
|
import { PlatformContext } from './NativeMmkvPlatformContext';
|
|
5
6
|
|
|
6
7
|
/**
|
|
@@ -72,61 +73,33 @@ export interface Configuration {
|
|
|
72
73
|
}
|
|
73
74
|
|
|
74
75
|
export interface Spec extends TurboModule {
|
|
76
|
+
/**
|
|
77
|
+
* Initialize MMKV with the given base storage directory.
|
|
78
|
+
* This should be the documents directory by default.
|
|
79
|
+
*/
|
|
75
80
|
initialize(basePath: string): boolean;
|
|
81
|
+
/**
|
|
82
|
+
* Create a new instance of MMKV.
|
|
83
|
+
* The returned {@linkcode UnsafeObject} is a `jsi::HostObject`.
|
|
84
|
+
*/
|
|
76
85
|
createMMKV(configuration: Configuration): UnsafeObject;
|
|
77
86
|
}
|
|
78
87
|
|
|
79
|
-
let module: Spec | null = null;
|
|
80
88
|
let basePath: string | null = null;
|
|
81
89
|
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
if (module == null) {
|
|
88
|
-
// try to find the turbomodule
|
|
89
|
-
module = TurboModuleRegistry.get<Spec>('MmkvCxx');
|
|
90
|
-
|
|
91
|
-
if (module == null) {
|
|
92
|
-
// if it still is null, something went wrong!
|
|
93
|
-
let message =
|
|
94
|
-
'Failed to create a new MMKV instance: The native MMKV Module could not be found.';
|
|
95
|
-
message +=
|
|
96
|
-
'\n* Make sure react-native-mmkv is correctly autolinked (run `npx react-native config` to verify)';
|
|
97
|
-
if (Platform.OS === 'ios' || Platform.OS === 'macos') {
|
|
98
|
-
message += '\n* Make sure you ran `pod install` in the ios/ directory.';
|
|
99
|
-
}
|
|
100
|
-
if (Platform.OS === 'android') {
|
|
101
|
-
message += '\n* Make sure gradle is synced.';
|
|
102
|
-
}
|
|
103
|
-
// check if Expo
|
|
104
|
-
const ExpoConstants =
|
|
105
|
-
NativeModules.NativeUnimoduleProxy?.modulesConstants?.ExponentConstants;
|
|
106
|
-
if (ExpoConstants != null) {
|
|
107
|
-
if (ExpoConstants.appOwnership === 'expo') {
|
|
108
|
-
// We're running Expo Go
|
|
109
|
-
throw new Error(
|
|
110
|
-
'react-native-mmkv is not supported in Expo Go! Use EAS (`expo prebuild`) or eject to a bare workflow instead.'
|
|
111
|
-
);
|
|
112
|
-
} else {
|
|
113
|
-
// We're running Expo bare / standalone
|
|
114
|
-
message += '\n* Make sure you ran `expo prebuild`.';
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
message += '\n* Make sure you rebuilt the app.';
|
|
119
|
-
throw new Error(message);
|
|
120
|
-
}
|
|
90
|
+
function getNativeModule(): Spec | null {
|
|
91
|
+
if (basePath == null) {
|
|
92
|
+
// use default base path from the Platform (iOS/Android)
|
|
93
|
+
basePath = PlatformContext.getBaseDirectory();
|
|
94
|
+
}
|
|
121
95
|
|
|
122
|
-
|
|
123
|
-
// Get base path from platform specific context
|
|
124
|
-
basePath = PlatformContext.getBaseDirectory();
|
|
125
|
-
}
|
|
96
|
+
const module = TurboModuleRegistry.get<Spec>('MmkvCxx');
|
|
126
97
|
|
|
127
|
-
|
|
98
|
+
if (module != null) {
|
|
99
|
+
// initialize MMKV
|
|
128
100
|
module.initialize(basePath);
|
|
129
101
|
}
|
|
130
102
|
|
|
131
103
|
return module;
|
|
132
104
|
}
|
|
105
|
+
export const MMKVTurboModule = getLazyTurboModule(getNativeModule);
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { TurboModule, TurboModuleRegistry } from 'react-native';
|
|
2
|
+
import { getLazyTurboModule } from './LazyTurboModule';
|
|
2
3
|
|
|
3
4
|
export interface Spec extends TurboModule {
|
|
4
5
|
/**
|
|
@@ -7,6 +8,7 @@ export interface Spec extends TurboModule {
|
|
|
7
8
|
getBaseDirectory(): string;
|
|
8
9
|
}
|
|
9
10
|
|
|
10
|
-
|
|
11
|
-
'MmkvPlatformContext'
|
|
12
|
-
|
|
11
|
+
function getModule(): Spec | null {
|
|
12
|
+
return TurboModuleRegistry.get<Spec>('MmkvPlatformContext');
|
|
13
|
+
}
|
|
14
|
+
export const PlatformContext = getLazyTurboModule(getModule);
|
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
|
+
}
|
package/src/createMMKV.mock.ts
CHANGED
package/src/createMMKV.ts
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
|
-
import type { Configuration
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
const module = getMMKVTurboModule();
|
|
1
|
+
import type { Configuration } from './MMKV';
|
|
2
|
+
import { MMKVTurboModule } from './NativeMmkv';
|
|
3
|
+
import type { NativeMMKV } from './Types';
|
|
5
4
|
|
|
6
5
|
export const createMMKV = (config: Configuration): NativeMMKV => {
|
|
7
|
-
const instance =
|
|
6
|
+
const instance = MMKVTurboModule.createMMKV(config);
|
|
8
7
|
if (__DEV__) {
|
|
9
8
|
if (typeof instance !== 'object' || instance == null) {
|
|
10
9
|
throw new Error(
|
package/src/createMMKV.web.ts
CHANGED
|
File without changes
|