react-native-mmkv 2.8.0 → 2.9.0
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/MMKV/Core/Core.xcodeproj/project.pbxproj +15 -19
- package/MMKV/Core/Core.xcodeproj/xcshareddata/xcschemes/Core.xcscheme +1 -1
- package/MMKV/Core/Core.xcodeproj/xcshareddata/xcschemes/MMKVWatchCore.xcscheme +1 -1
- package/MMKV/Core/MMBuffer.cpp +18 -0
- package/MMKV/Core/MMBuffer.h +1 -0
- package/MMKV/Core/MMKV.cpp +173 -33
- package/MMKV/Core/MMKV.h +44 -0
- package/MMKV/Core/MMKVMetaInfo.hpp +18 -0
- package/MMKV/Core/MMKVPredef.h +2 -2
- package/MMKV/Core/MMKV_IO.cpp +477 -68
- package/MMKV/Core/MMKV_OSX.cpp +65 -14
- package/MMKV/Core/MemoryFile_Win32.cpp +39 -35
- package/MMKV/Core/MiniPBCoder.cpp +3 -7
- package/MMKV/Core/MiniPBCoder_OSX.cpp +4 -4
- package/MMKV/Core/PBUtility.cpp +1 -1
- package/MMKV/Core/PBUtility.h +7 -0
- package/MMKV/Core/aes/AESCrypt.h +1 -1
- package/MMKV/Core/aes/openssl/openssl_aes-armv4.S +4 -0
- package/README.md +12 -3
- package/android/build.gradle +1 -0
- package/android/src/main/AndroidManifest.xml +1 -2
- package/lib/commonjs/createMMKV.web.js +23 -0
- package/lib/commonjs/createMMKV.web.js.map +1 -1
- package/lib/module/createMMKV.web.js +23 -0
- package/lib/module/createMMKV.web.js.map +1 -1
- package/lib/typescript/createMMKV.web.d.ts.map +1 -1
- package/package.json +7 -6
- package/src/MMKV.ts +248 -0
- package/src/PlatformChecker.ts +7 -0
- package/src/createMMKV.mock.ts +33 -0
- package/src/createMMKV.ts +70 -0
- package/src/createMMKV.web.ts +119 -0
- package/src/createTextEncoder.ts +16 -0
- package/src/hooks.ts +232 -0
- package/src/index.ts +2 -0
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["createTextEncoder","canUseDOM","window","document","createElement","KEY_WILDCARD","createMMKV","config","encryptionKey","Error","path","storage","
|
|
1
|
+
{"version":3,"names":["createTextEncoder","canUseDOM","window","document","createElement","hasAccessToLocalStorage","localStorage","KEY_WILDCARD","inMemoryStorage","Map","createMMKV","config","encryptionKey","Error","path","console","warn","storage","getItem","key","get","setItem","value","set","removeItem","delete","clear","length","size","index","Object","keys","at","domStorage","global","textEncoder","id","includes","keyPrefix","prefixedKey","clearAll","startsWith","toString","getString","undefined","getNumber","Number","getBoolean","getBuffer","encode","getAllKeys","filter","contains","recrypt"],"sourceRoot":"../../src","sources":["createMMKV.web.ts"],"mappings":";AAAA;;AAEA,SAASA,iBAAiB,QAAQ,qBAAqB;AAEvD,MAAMC,SAAS,GACb,OAAOC,MAAM,KAAK,WAAW,IAAI,qBAAAA,MAAM,CAACC,QAAQ,qDAAf,iBAAiBC,aAAa,KAAI,IAAI;AAEzE,MAAMC,uBAAuB,GAAG,MAAM;EACpC,IAAI;IACF;IACAH,MAAM,CAACI,YAAY;IAEnB,OAAO,IAAI;EACb,CAAC,CAAC,MAAM;IACN,OAAO,KAAK;EACd;AACF,CAAC;AAED,MAAMC,YAAY,GAAG,IAAI;AACzB,MAAMC,eAAe,GAAG,IAAIC,GAAG,EAAkB;AAEjD,OAAO,MAAMC,UAAU,GAAIC,MAAyB,IAAiB;EACnE,IAAIA,MAAM,CAACC,aAAa,IAAI,IAAI,EAAE;IAChC,MAAM,IAAIC,KAAK,CAAC,gDAAgD,CAAC;EACnE;EACA,IAAIF,MAAM,CAACG,IAAI,IAAI,IAAI,EAAE;IACvB,MAAM,IAAID,KAAK,CAAC,uCAAuC,CAAC;EAC1D;EAEA,IAAI,CAACR,uBAAuB,EAAE,EAAE;IAC9BU,OAAO,CAACC,IAAI,CACV,6FAA6F,CAC9F;EACH;EAEA,MAAMC,OAAO,GAAG,MAAM;IAAA;IACpB,IAAI,CAAChB,SAAS,EAAE;MACd,MAAM,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G,CAAC,CAAC;MACjD,IAAIG,KAAK,IAAI,IAAI,EAAE,OAAOsB,SAAS;MACnC,OAAOT,WAAW,CAACc,MAAM,CAAC3B,KAAK,CAAC;IAClC,CAAC;IACD4B,UAAU,EAAE,MAAM;MAChB,MAAMnB,IAAI,GAAGD,MAAM,CAACC,IAAI,CAACd,OAAO,EAAE,CAAC;MACnC,OAAOc,IAAI,CAACoB,MAAM,CAAEhC,GAAG,IAAKA,GAAG,CAACsB,UAAU,CAACH,SAAS,CAAC,CAAC;IACxD,CAAC;IACDc,QAAQ,EAAGjC,GAAG,IAAKF,OAAO,EAAE,CAACC,OAAO,CAACqB,WAAW,CAACpB,GAAG,CAAC,CAAC,IAAI,IAAI;IAC9DkC,OAAO,EAAE,MAAM;MACb,MAAM,IAAIxC,KAAK,CAAC,wCAAwC,CAAC;IAC3D;EACF,CAAC;AACH,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createMMKV.web.d.ts","sourceRoot":"","sources":["../../src/createMMKV.web.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,iBAAiB,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;
|
|
1
|
+
{"version":3,"file":"createMMKV.web.d.ts","sourceRoot":"","sources":["../../src/createMMKV.web.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,iBAAiB,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAoBvE,eAAO,MAAM,UAAU,WAAY,iBAAiB,KAAG,UAiGtD,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-native-mmkv",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.9.0",
|
|
4
4
|
"description": "The fastest key/value storage for React Native. ~30x faster than AsyncStorage! Works on Android, iOS and Web.",
|
|
5
5
|
"main": "lib/commonjs/index",
|
|
6
6
|
"module": "lib/module/index",
|
|
@@ -22,6 +22,7 @@
|
|
|
22
22
|
"ios/**/*.mm",
|
|
23
23
|
"ios/**/*.cpp",
|
|
24
24
|
"ios/Mmkv.xcodeproj/project.pbxproj",
|
|
25
|
+
"src",
|
|
25
26
|
"react-native-mmkv.podspec",
|
|
26
27
|
"README.md"
|
|
27
28
|
],
|
|
@@ -53,15 +54,15 @@
|
|
|
53
54
|
"registry": "https://registry.npmjs.org/"
|
|
54
55
|
},
|
|
55
56
|
"devDependencies": {
|
|
56
|
-
"@jamesacarr/eslint-formatter-github-actions": "^0.
|
|
57
|
+
"@jamesacarr/eslint-formatter-github-actions": "^0.2.0",
|
|
57
58
|
"@react-native-community/eslint-config": "^3.2.0",
|
|
58
59
|
"@react-native-community/eslint-plugin": "^1.3.0",
|
|
59
60
|
"@release-it/conventional-changelog": "^5.1.1",
|
|
60
61
|
"@tsconfig/react-native": "^2.0.3",
|
|
61
|
-
"@types/react": "^18.0.
|
|
62
|
-
"@typescript-eslint/eslint-plugin": "^5.
|
|
63
|
-
"@typescript-eslint/parser": "^5.
|
|
64
|
-
"eslint": "^8.
|
|
62
|
+
"@types/react": "^18.0.34",
|
|
63
|
+
"@typescript-eslint/eslint-plugin": "^5.58.0",
|
|
64
|
+
"@typescript-eslint/parser": "^5.58.0",
|
|
65
|
+
"eslint": "^8.38.0",
|
|
65
66
|
"eslint-plugin-jest": "^27.2.1",
|
|
66
67
|
"prettier": "^2.8.7",
|
|
67
68
|
"react": "^18.2.0",
|
package/src/MMKV.ts
ADDED
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
import { createMMKV } from './createMMKV';
|
|
2
|
+
import { createMockMMKV } from './createMMKV.mock';
|
|
3
|
+
import { isJest } from './PlatformChecker';
|
|
4
|
+
|
|
5
|
+
interface Listener {
|
|
6
|
+
remove: () => void;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Used for configuration of a single MMKV instance.
|
|
11
|
+
*/
|
|
12
|
+
export interface MMKVConfiguration {
|
|
13
|
+
/**
|
|
14
|
+
* The MMKV instance's ID. If you want to use multiple instances, make sure to use different IDs!
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```ts
|
|
18
|
+
* const userStorage = new MMKV({ id: `user-${userId}-storage` })
|
|
19
|
+
* const globalStorage = new MMKV({ id: 'global-app-storage' })
|
|
20
|
+
* ```
|
|
21
|
+
*
|
|
22
|
+
* @default 'mmkv.default'
|
|
23
|
+
*/
|
|
24
|
+
id: string;
|
|
25
|
+
/**
|
|
26
|
+
* The MMKV instance's root path. By default, MMKV stores file inside `$(Documents)/mmkv/`. You can customize MMKV's root directory on MMKV initialization:
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* ```ts
|
|
30
|
+
* const temporaryStorage = new MMKV({ path: '/tmp/' })
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
33
|
+
path?: string;
|
|
34
|
+
/**
|
|
35
|
+
* The MMKV instance's encryption/decryption key. By default, MMKV stores all key-values in plain text on file, relying on iOS's sandbox to make sure the file is encrypted. Should you worry about information leaking, you can choose to encrypt MMKV.
|
|
36
|
+
*
|
|
37
|
+
* Encryption keys can have a maximum length of 16 bytes.
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* ```ts
|
|
41
|
+
* const secureStorage = new MMKV({ encryptionKey: 'my-encryption-key!' })
|
|
42
|
+
* ```
|
|
43
|
+
*/
|
|
44
|
+
encryptionKey?: string;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Represents a single MMKV instance.
|
|
49
|
+
*/
|
|
50
|
+
interface MMKVInterface {
|
|
51
|
+
/**
|
|
52
|
+
* Set a value for the given `key`.
|
|
53
|
+
*/
|
|
54
|
+
set: (key: string, value: boolean | string | number | Uint8Array) => void;
|
|
55
|
+
/**
|
|
56
|
+
* Get the boolean value for the given `key`, or `undefined` if it does not exist.
|
|
57
|
+
*
|
|
58
|
+
* @default undefined
|
|
59
|
+
*/
|
|
60
|
+
getBoolean: (key: string) => boolean | undefined;
|
|
61
|
+
/**
|
|
62
|
+
* Get the string value for the given `key`, or `undefined` if it does not exist.
|
|
63
|
+
*
|
|
64
|
+
* @default undefined
|
|
65
|
+
*/
|
|
66
|
+
getString: (key: string) => string | undefined;
|
|
67
|
+
/**
|
|
68
|
+
* Get the number value for the given `key`, or `undefined` if it does not exist.
|
|
69
|
+
*
|
|
70
|
+
* @default undefined
|
|
71
|
+
*/
|
|
72
|
+
getNumber: (key: string) => number | undefined;
|
|
73
|
+
/**
|
|
74
|
+
* Get a raw buffer of unsigned 8-bit (0-255) data.
|
|
75
|
+
*
|
|
76
|
+
* @default undefined
|
|
77
|
+
*/
|
|
78
|
+
getBuffer: (key: string) => Uint8Array | undefined;
|
|
79
|
+
/**
|
|
80
|
+
* Checks whether the given `key` is being stored in this MMKV instance.
|
|
81
|
+
*/
|
|
82
|
+
contains: (key: string) => boolean;
|
|
83
|
+
/**
|
|
84
|
+
* Delete the given `key`.
|
|
85
|
+
*/
|
|
86
|
+
delete: (key: string) => void;
|
|
87
|
+
/**
|
|
88
|
+
* Get all keys.
|
|
89
|
+
*
|
|
90
|
+
* @default []
|
|
91
|
+
*/
|
|
92
|
+
getAllKeys: () => string[];
|
|
93
|
+
/**
|
|
94
|
+
* Delete all keys.
|
|
95
|
+
*/
|
|
96
|
+
clearAll: () => void;
|
|
97
|
+
/**
|
|
98
|
+
* Sets (or updates) the encryption-key to encrypt all data in this MMKV instance with.
|
|
99
|
+
*
|
|
100
|
+
* To remove encryption, pass `undefined` as a key.
|
|
101
|
+
*
|
|
102
|
+
* Encryption keys can have a maximum length of 16 bytes.
|
|
103
|
+
*/
|
|
104
|
+
recrypt: (key: string | undefined) => void;
|
|
105
|
+
/**
|
|
106
|
+
* Adds a value changed listener. The Listener will be called whenever any value
|
|
107
|
+
* in this storage instance changes (set or delete).
|
|
108
|
+
*
|
|
109
|
+
* To unsubscribe from value changes, call `remove()` on the Listener.
|
|
110
|
+
*/
|
|
111
|
+
addOnValueChangedListener: (
|
|
112
|
+
onValueChanged: (key: string) => void
|
|
113
|
+
) => Listener;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export type NativeMMKV = Pick<
|
|
117
|
+
MMKVInterface,
|
|
118
|
+
| 'clearAll'
|
|
119
|
+
| 'contains'
|
|
120
|
+
| 'delete'
|
|
121
|
+
| 'getAllKeys'
|
|
122
|
+
| 'getBoolean'
|
|
123
|
+
| 'getNumber'
|
|
124
|
+
| 'getString'
|
|
125
|
+
| 'getBuffer'
|
|
126
|
+
| 'set'
|
|
127
|
+
| 'recrypt'
|
|
128
|
+
>;
|
|
129
|
+
|
|
130
|
+
const onValueChangedListeners = new Map<string, ((key: string) => void)[]>();
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* A single MMKV instance.
|
|
134
|
+
*/
|
|
135
|
+
export class MMKV implements MMKVInterface {
|
|
136
|
+
private nativeInstance: NativeMMKV;
|
|
137
|
+
private functionCache: Partial<NativeMMKV>;
|
|
138
|
+
private id: string;
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Creates a new MMKV instance with the given Configuration.
|
|
142
|
+
* If no custom `id` is supplied, `'mmkv.default'` will be used.
|
|
143
|
+
*/
|
|
144
|
+
constructor(configuration: MMKVConfiguration = { id: 'mmkv.default' }) {
|
|
145
|
+
this.id = configuration.id;
|
|
146
|
+
this.nativeInstance = isJest()
|
|
147
|
+
? createMockMMKV()
|
|
148
|
+
: createMMKV(configuration);
|
|
149
|
+
this.functionCache = {};
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
private get onValueChangedListeners() {
|
|
153
|
+
if (!onValueChangedListeners.has(this.id)) {
|
|
154
|
+
onValueChangedListeners.set(this.id, []);
|
|
155
|
+
}
|
|
156
|
+
return onValueChangedListeners.get(this.id)!;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
private getFunctionFromCache<T extends keyof NativeMMKV>(
|
|
160
|
+
functionName: T
|
|
161
|
+
): NativeMMKV[T] {
|
|
162
|
+
if (this.functionCache[functionName] == null) {
|
|
163
|
+
this.functionCache[functionName] = this.nativeInstance[functionName];
|
|
164
|
+
}
|
|
165
|
+
return this.functionCache[functionName] as NativeMMKV[T];
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
private onValuesChanged(keys: string[]) {
|
|
169
|
+
if (this.onValueChangedListeners.length === 0) return;
|
|
170
|
+
|
|
171
|
+
for (const key of keys) {
|
|
172
|
+
for (const listener of this.onValueChangedListeners) {
|
|
173
|
+
listener(key);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
set(key: string, value: boolean | string | number | Uint8Array): void {
|
|
179
|
+
const func = this.getFunctionFromCache('set');
|
|
180
|
+
func(key, value);
|
|
181
|
+
|
|
182
|
+
this.onValuesChanged([key]);
|
|
183
|
+
}
|
|
184
|
+
getBoolean(key: string): boolean | undefined {
|
|
185
|
+
const func = this.getFunctionFromCache('getBoolean');
|
|
186
|
+
return func(key);
|
|
187
|
+
}
|
|
188
|
+
getString(key: string): string | undefined {
|
|
189
|
+
const func = this.getFunctionFromCache('getString');
|
|
190
|
+
return func(key);
|
|
191
|
+
}
|
|
192
|
+
getNumber(key: string): number | undefined {
|
|
193
|
+
const func = this.getFunctionFromCache('getNumber');
|
|
194
|
+
return func(key);
|
|
195
|
+
}
|
|
196
|
+
getBuffer(key: string): Uint8Array | undefined {
|
|
197
|
+
const func = this.getFunctionFromCache('getBuffer');
|
|
198
|
+
return func(key);
|
|
199
|
+
}
|
|
200
|
+
contains(key: string): boolean {
|
|
201
|
+
const func = this.getFunctionFromCache('contains');
|
|
202
|
+
return func(key);
|
|
203
|
+
}
|
|
204
|
+
delete(key: string): void {
|
|
205
|
+
const func = this.getFunctionFromCache('delete');
|
|
206
|
+
func(key);
|
|
207
|
+
|
|
208
|
+
this.onValuesChanged([key]);
|
|
209
|
+
}
|
|
210
|
+
getAllKeys(): string[] {
|
|
211
|
+
const func = this.getFunctionFromCache('getAllKeys');
|
|
212
|
+
return func();
|
|
213
|
+
}
|
|
214
|
+
clearAll(): void {
|
|
215
|
+
const keys = this.getAllKeys();
|
|
216
|
+
|
|
217
|
+
const func = this.getFunctionFromCache('clearAll');
|
|
218
|
+
func();
|
|
219
|
+
|
|
220
|
+
this.onValuesChanged(keys);
|
|
221
|
+
}
|
|
222
|
+
recrypt(key: string | undefined): void {
|
|
223
|
+
const func = this.getFunctionFromCache('recrypt');
|
|
224
|
+
return func(key);
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
toString(): string {
|
|
228
|
+
return `MMKV (${this.id}): [${this.getAllKeys().join(', ')}]`;
|
|
229
|
+
}
|
|
230
|
+
toJSON(): object {
|
|
231
|
+
return {
|
|
232
|
+
[this.id]: this.getAllKeys(),
|
|
233
|
+
};
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
addOnValueChangedListener(onValueChanged: (key: string) => void): Listener {
|
|
237
|
+
this.onValueChangedListeners.push(onValueChanged);
|
|
238
|
+
|
|
239
|
+
return {
|
|
240
|
+
remove: () => {
|
|
241
|
+
const index = this.onValueChangedListeners.indexOf(onValueChanged);
|
|
242
|
+
if (index !== -1) {
|
|
243
|
+
this.onValueChangedListeners.splice(index, 1);
|
|
244
|
+
}
|
|
245
|
+
},
|
|
246
|
+
};
|
|
247
|
+
}
|
|
248
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { NativeMMKV } from 'react-native-mmkv';
|
|
2
|
+
|
|
3
|
+
/* Mock MMKV instance for use in tests */
|
|
4
|
+
export const createMockMMKV = (): NativeMMKV => {
|
|
5
|
+
const storage = new Map<string, string | boolean | number | Uint8Array>();
|
|
6
|
+
|
|
7
|
+
return {
|
|
8
|
+
clearAll: () => storage.clear(),
|
|
9
|
+
delete: (key) => storage.delete(key),
|
|
10
|
+
set: (key, value) => storage.set(key, value),
|
|
11
|
+
getString: (key) => {
|
|
12
|
+
const result = storage.get(key);
|
|
13
|
+
return typeof result === 'string' ? result : undefined;
|
|
14
|
+
},
|
|
15
|
+
getNumber: (key) => {
|
|
16
|
+
const result = storage.get(key);
|
|
17
|
+
return typeof result === 'number' ? result : undefined;
|
|
18
|
+
},
|
|
19
|
+
getBoolean: (key) => {
|
|
20
|
+
const result = storage.get(key);
|
|
21
|
+
return typeof result === 'boolean' ? result : undefined;
|
|
22
|
+
},
|
|
23
|
+
getBuffer: (key) => {
|
|
24
|
+
const result = storage.get(key);
|
|
25
|
+
return result instanceof Uint8Array ? result : undefined;
|
|
26
|
+
},
|
|
27
|
+
getAllKeys: () => Array.from(storage.keys()),
|
|
28
|
+
contains: (key) => storage.has(key),
|
|
29
|
+
recrypt: () => {
|
|
30
|
+
console.warn('Encryption is not supported in mocked MMKV instances!');
|
|
31
|
+
},
|
|
32
|
+
};
|
|
33
|
+
};
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { NativeModules, Platform } from 'react-native';
|
|
2
|
+
import type { MMKVConfiguration, NativeMMKV } from 'react-native-mmkv';
|
|
3
|
+
|
|
4
|
+
// global func declaration for JSI functions
|
|
5
|
+
declare global {
|
|
6
|
+
function mmkvCreateNewInstance(configuration: MMKVConfiguration): NativeMMKV;
|
|
7
|
+
function nativeCallSyncHook(): unknown;
|
|
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) {
|
|
50
|
+
throw new Error(
|
|
51
|
+
'Failed to create a new MMKV instance: React Native is not running on-device. MMKV can only be used when synchronous method invocations (JSI) are possible. If you are using a remote debugger (e.g. Chrome), switch to an on-device debugger (e.g. Flipper) instead.'
|
|
52
|
+
);
|
|
53
|
+
}
|
|
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
|
+
}
|
|
68
|
+
|
|
69
|
+
return global.mmkvCreateNewInstance(config);
|
|
70
|
+
};
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
/* global localStorage */
|
|
2
|
+
import type { MMKVConfiguration, NativeMMKV } from 'react-native-mmkv';
|
|
3
|
+
import { createTextEncoder } from './createTextEncoder';
|
|
4
|
+
|
|
5
|
+
const canUseDOM =
|
|
6
|
+
typeof window !== 'undefined' && window.document?.createElement != null;
|
|
7
|
+
|
|
8
|
+
const hasAccessToLocalStorage = () => {
|
|
9
|
+
try {
|
|
10
|
+
// throws ACCESS_DENIED error
|
|
11
|
+
window.localStorage;
|
|
12
|
+
|
|
13
|
+
return true;
|
|
14
|
+
} catch {
|
|
15
|
+
return false;
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
const KEY_WILDCARD = '\\';
|
|
20
|
+
const inMemoryStorage = new Map<string, string>();
|
|
21
|
+
|
|
22
|
+
export const createMMKV = (config: MMKVConfiguration): NativeMMKV => {
|
|
23
|
+
if (config.encryptionKey != null) {
|
|
24
|
+
throw new Error("MMKV: 'encryptionKey' is not supported on Web!");
|
|
25
|
+
}
|
|
26
|
+
if (config.path != null) {
|
|
27
|
+
throw new Error("MMKV: 'path' is not supported on Web!");
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
if (!hasAccessToLocalStorage()) {
|
|
31
|
+
console.warn(
|
|
32
|
+
'MMKV: LocalStorage has been disabled. Your experience will be limited to in-memory storage!'
|
|
33
|
+
);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const storage = () => {
|
|
37
|
+
if (!canUseDOM) {
|
|
38
|
+
throw new Error(
|
|
39
|
+
'Tried to access storage on the server. Did you forget to call this in useEffect?'
|
|
40
|
+
);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
if (!hasAccessToLocalStorage()) {
|
|
44
|
+
return {
|
|
45
|
+
getItem: (key: string) => inMemoryStorage.get(key) ?? null,
|
|
46
|
+
setItem: (key: string, value: string) =>
|
|
47
|
+
inMemoryStorage.set(key, value),
|
|
48
|
+
removeItem: (key: string) => inMemoryStorage.delete(key),
|
|
49
|
+
clear: () => inMemoryStorage.clear(),
|
|
50
|
+
length: inMemoryStorage.size,
|
|
51
|
+
key: (index: number) => Object.keys(inMemoryStorage).at(index) ?? null,
|
|
52
|
+
} as Storage;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const domStorage =
|
|
56
|
+
global?.localStorage ?? window?.localStorage ?? localStorage;
|
|
57
|
+
if (domStorage == null) {
|
|
58
|
+
throw new Error(`Could not find 'localStorage' instance!`);
|
|
59
|
+
}
|
|
60
|
+
return domStorage;
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
const textEncoder = createTextEncoder();
|
|
64
|
+
|
|
65
|
+
if (config.id.includes(KEY_WILDCARD)) {
|
|
66
|
+
throw new Error(
|
|
67
|
+
'MMKV: `id` cannot contain the backslash character (`\\`)!'
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const keyPrefix = `${config.id}${KEY_WILDCARD}`; // mmkv.default\\
|
|
72
|
+
const prefixedKey = (key: string) => {
|
|
73
|
+
if (key.includes('\\')) {
|
|
74
|
+
throw new Error(
|
|
75
|
+
'MMKV: `key` cannot contain the backslash character (`\\`)!'
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
return `${keyPrefix}${key}`;
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
return {
|
|
82
|
+
clearAll: () => {
|
|
83
|
+
const keys = Object.keys(storage());
|
|
84
|
+
for (const key of keys) {
|
|
85
|
+
if (key.startsWith(keyPrefix)) {
|
|
86
|
+
storage().removeItem(key);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
},
|
|
90
|
+
delete: (key) => storage().removeItem(prefixedKey(key)),
|
|
91
|
+
set: (key, value) => {
|
|
92
|
+
storage().setItem(prefixedKey(key), value.toString());
|
|
93
|
+
},
|
|
94
|
+
getString: (key) => storage().getItem(prefixedKey(key)) ?? undefined,
|
|
95
|
+
getNumber: (key) => {
|
|
96
|
+
const value = storage().getItem(prefixedKey(key));
|
|
97
|
+
if (value == null) return undefined;
|
|
98
|
+
return Number(value);
|
|
99
|
+
},
|
|
100
|
+
getBoolean: (key) => {
|
|
101
|
+
const value = storage().getItem(prefixedKey(key));
|
|
102
|
+
if (value == null) return undefined;
|
|
103
|
+
return value === 'true';
|
|
104
|
+
},
|
|
105
|
+
getBuffer: (key) => {
|
|
106
|
+
const value = storage().getItem(prefixedKey(key));
|
|
107
|
+
if (value == null) return undefined;
|
|
108
|
+
return textEncoder.encode(value);
|
|
109
|
+
},
|
|
110
|
+
getAllKeys: () => {
|
|
111
|
+
const keys = Object.keys(storage());
|
|
112
|
+
return keys.filter((key) => key.startsWith(keyPrefix));
|
|
113
|
+
},
|
|
114
|
+
contains: (key) => storage().getItem(prefixedKey(key)) != null,
|
|
115
|
+
recrypt: () => {
|
|
116
|
+
throw new Error('`recrypt(..)` is not supported on Web!');
|
|
117
|
+
},
|
|
118
|
+
};
|
|
119
|
+
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/* global TextEncoder */
|
|
2
|
+
export function createTextEncoder(): TextEncoder {
|
|
3
|
+
if (global.TextEncoder != null) {
|
|
4
|
+
return new global.TextEncoder();
|
|
5
|
+
} else {
|
|
6
|
+
return {
|
|
7
|
+
encode: () => {
|
|
8
|
+
throw new Error('TextEncoder is not supported in this environment!');
|
|
9
|
+
},
|
|
10
|
+
encodeInto: () => {
|
|
11
|
+
throw new Error('TextEncoder is not supported in this environment!');
|
|
12
|
+
},
|
|
13
|
+
encoding: 'utf-8',
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
}
|