react-native-mmkv 2.7.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 +14 -6
- package/android/build.gradle +2 -0
- package/android/src/main/AndroidManifest.xml +1 -2
- package/android/src/main/cpp/cpp-adapter.cpp +5 -0
- package/cpp/TypedArray.cpp +187 -199
- package/cpp/TypedArray.h +112 -109
- package/ios/JSIUtils.mm +2 -1
- package/ios/MmkvModule.mm +12 -7
- package/lib/commonjs/MMKV.js +0 -10
- package/lib/commonjs/MMKV.js.map +1 -1
- package/lib/commonjs/createMMKV.web.js +55 -12
- package/lib/commonjs/createMMKV.web.js.map +1 -1
- package/lib/commonjs/hooks.js +7 -0
- package/lib/commonjs/hooks.js.map +1 -1
- package/lib/module/MMKV.js +0 -10
- package/lib/module/MMKV.js.map +1 -1
- package/lib/module/createMMKV.web.js +55 -12
- package/lib/module/createMMKV.web.js.map +1 -1
- package/lib/module/hooks.js +7 -0
- package/lib/module/hooks.js.map +1 -1
- package/lib/typescript/MMKV.d.ts +0 -12
- package/lib/typescript/MMKV.d.ts.map +1 -1
- package/lib/typescript/createMMKV.web.d.ts.map +1 -1
- package/lib/typescript/hooks.d.ts.map +1 -1
- package/package.json +15 -10
- 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
- package/MMKV/LICENSE.TXT +0 -193
- package/MMKV/README.md +0 -291
|
@@ -3,21 +3,42 @@ var _window$document;
|
|
|
3
3
|
|
|
4
4
|
import { createTextEncoder } from './createTextEncoder';
|
|
5
5
|
const canUseDOM = typeof window !== 'undefined' && ((_window$document = window.document) === null || _window$document === void 0 ? void 0 : _window$document.createElement) != null;
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
6
|
+
const hasAccessToLocalStorage = () => {
|
|
7
|
+
try {
|
|
8
|
+
// throws ACCESS_DENIED error
|
|
9
|
+
window.localStorage;
|
|
10
|
+
return true;
|
|
11
|
+
} catch {
|
|
12
|
+
return false;
|
|
9
13
|
}
|
|
14
|
+
};
|
|
15
|
+
const KEY_WILDCARD = '\\';
|
|
16
|
+
const inMemoryStorage = new Map();
|
|
17
|
+
export const createMMKV = config => {
|
|
10
18
|
if (config.encryptionKey != null) {
|
|
11
19
|
throw new Error("MMKV: 'encryptionKey' is not supported on Web!");
|
|
12
20
|
}
|
|
13
21
|
if (config.path != null) {
|
|
14
22
|
throw new Error("MMKV: 'path' is not supported on Web!");
|
|
15
23
|
}
|
|
24
|
+
if (!hasAccessToLocalStorage()) {
|
|
25
|
+
console.warn('MMKV: LocalStorage has been disabled. Your experience will be limited to in-memory storage!');
|
|
26
|
+
}
|
|
16
27
|
const storage = () => {
|
|
17
28
|
var _global, _window;
|
|
18
29
|
if (!canUseDOM) {
|
|
19
30
|
throw new Error('Tried to access storage on the server. Did you forget to call this in useEffect?');
|
|
20
31
|
}
|
|
32
|
+
if (!hasAccessToLocalStorage()) {
|
|
33
|
+
return {
|
|
34
|
+
getItem: key => inMemoryStorage.get(key) ?? null,
|
|
35
|
+
setItem: (key, value) => inMemoryStorage.set(key, value),
|
|
36
|
+
removeItem: key => inMemoryStorage.delete(key),
|
|
37
|
+
clear: () => inMemoryStorage.clear(),
|
|
38
|
+
length: inMemoryStorage.size,
|
|
39
|
+
key: index => Object.keys(inMemoryStorage).at(index) ?? null
|
|
40
|
+
};
|
|
41
|
+
}
|
|
21
42
|
const domStorage = ((_global = global) === null || _global === void 0 ? void 0 : _global.localStorage) ?? ((_window = window) === null || _window === void 0 ? void 0 : _window.localStorage) ?? localStorage;
|
|
22
43
|
if (domStorage == null) {
|
|
23
44
|
throw new Error(`Could not find 'localStorage' instance!`);
|
|
@@ -25,28 +46,50 @@ export const createMMKV = config => {
|
|
|
25
46
|
return domStorage;
|
|
26
47
|
};
|
|
27
48
|
const textEncoder = createTextEncoder();
|
|
49
|
+
if (config.id.includes(KEY_WILDCARD)) {
|
|
50
|
+
throw new Error('MMKV: `id` cannot contain the backslash character (`\\`)!');
|
|
51
|
+
}
|
|
52
|
+
const keyPrefix = `${config.id}${KEY_WILDCARD}`; // mmkv.default\\
|
|
53
|
+
const prefixedKey = key => {
|
|
54
|
+
if (key.includes('\\')) {
|
|
55
|
+
throw new Error('MMKV: `key` cannot contain the backslash character (`\\`)!');
|
|
56
|
+
}
|
|
57
|
+
return `${keyPrefix}${key}`;
|
|
58
|
+
};
|
|
28
59
|
return {
|
|
29
|
-
clearAll: () =>
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
60
|
+
clearAll: () => {
|
|
61
|
+
const keys = Object.keys(storage());
|
|
62
|
+
for (const key of keys) {
|
|
63
|
+
if (key.startsWith(keyPrefix)) {
|
|
64
|
+
storage().removeItem(key);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
},
|
|
68
|
+
delete: key => storage().removeItem(prefixedKey(key)),
|
|
69
|
+
set: (key, value) => {
|
|
70
|
+
storage().setItem(prefixedKey(key), value.toString());
|
|
71
|
+
},
|
|
72
|
+
getString: key => storage().getItem(prefixedKey(key)) ?? undefined,
|
|
33
73
|
getNumber: key => {
|
|
34
|
-
const value = storage().getItem(key);
|
|
74
|
+
const value = storage().getItem(prefixedKey(key));
|
|
35
75
|
if (value == null) return undefined;
|
|
36
76
|
return Number(value);
|
|
37
77
|
},
|
|
38
78
|
getBoolean: key => {
|
|
39
|
-
const value = storage().getItem(key);
|
|
79
|
+
const value = storage().getItem(prefixedKey(key));
|
|
40
80
|
if (value == null) return undefined;
|
|
41
81
|
return value === 'true';
|
|
42
82
|
},
|
|
43
83
|
getBuffer: key => {
|
|
44
|
-
const value = storage().getItem(key);
|
|
84
|
+
const value = storage().getItem(prefixedKey(key));
|
|
45
85
|
if (value == null) return undefined;
|
|
46
86
|
return textEncoder.encode(value);
|
|
47
87
|
},
|
|
48
|
-
getAllKeys: () =>
|
|
49
|
-
|
|
88
|
+
getAllKeys: () => {
|
|
89
|
+
const keys = Object.keys(storage());
|
|
90
|
+
return keys.filter(key => key.startsWith(keyPrefix));
|
|
91
|
+
},
|
|
92
|
+
contains: key => storage().getItem(prefixedKey(key)) != null,
|
|
50
93
|
recrypt: () => {
|
|
51
94
|
throw new Error('`recrypt(..)` is not supported on Web!');
|
|
52
95
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["createTextEncoder","canUseDOM","window","document","createElement","createMMKV","config","
|
|
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,IAAIY,KAAK,CACb,kFAAkF,CACnF;IACH;IAEA,IAAI,CAACR,uBAAuB,EAAE,EAAE;MAC9B,OAAO;QACLa,OAAO,EAAGC,GAAW,IAAKX,eAAe,CAACY,GAAG,CAACD,GAAG,CAAC,IAAI,IAAI;QAC1DE,OAAO,EAAE,CAACF,GAAW,EAAEG,KAAa,KAClCd,eAAe,CAACe,GAAG,CAACJ,GAAG,EAAEG,KAAK,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AAG,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"}
|
package/lib/module/hooks.js
CHANGED
|
@@ -45,6 +45,13 @@ function createMMKVHook(getter) {
|
|
|
45
45
|
case 'undefined':
|
|
46
46
|
mmkv.delete(key);
|
|
47
47
|
break;
|
|
48
|
+
case 'object':
|
|
49
|
+
if (newValue instanceof Uint8Array) {
|
|
50
|
+
mmkv.set(key, newValue);
|
|
51
|
+
break;
|
|
52
|
+
} else {
|
|
53
|
+
throw new Error(`MMKV: Type object (${newValue}) is not supported!`);
|
|
54
|
+
}
|
|
48
55
|
default:
|
|
49
56
|
throw new Error(`MMKV: Type ${typeof newValue} is not supported!`);
|
|
50
57
|
}
|
package/lib/module/hooks.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["useRef","useState","useMemo","useCallback","useEffect","MMKV","isConfigurationEqual","left","right","encryptionKey","id","path","defaultInstance","getDefaultInstance","useMMKV","configuration","instance","lastConfiguration","current","createMMKVHook","getter","key","mmkv","value","setValue","valueRef","set","v","newValue","delete","Error","listener","addOnValueChangedListener","changedKey","remove","useMMKVString","getString","useMMKVNumber","getNumber","useMMKVBoolean","getBoolean","useMMKVBuffer","getBuffer","useMMKVObject","string","setString","undefined","JSON","parse","stringify","useMMKVListener","valueChangedListener","ref"],"sourceRoot":"../../src","sources":["hooks.ts"],"mappings":"AAAA,SAASA,MAAM,EAAEC,QAAQ,EAAEC,OAAO,EAAEC,WAAW,EAAEC,SAAS,QAAQ,OAAO;AACzE,SAASC,IAAI,QAA2B,QAAQ;AAEhD,SAASC,oBAAoB,CAC3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AGzB,MAAM,CAAIuB,KAAK,CAAC;IACjCE,QAAQ,CAACP,OAAO,GAAGK,KAAK;;IAExB;IACA,MAAMG,GAAG,GAAGvB,WAAW,CACpBwB,CAAa,IAAK;MACjB,MAAMC,QAAQ,GAAG,OAAOD,CAAC,KAAK,UAAU,GAAGA,CAAC,CAACF,QAAQ,CAACP,OAAO,CAAC,GAAGS,CAAC;MAClE,QAAQ,OAAOC,QAAQ;QACrB,KAAK,QAAQ;QACb,KAAK,QAAQ;QACb,KAAK,SAAS;UACZN,IAAI,CAACI,GAAG,CAACL,GAAG,EAAEO,QAAQ,CAAC;UACvB;QACF,KAAK,WAAW;UACdN,IAAI,CAACO,MAAM,CAACR,GAAG,CAAC;UAChB;QACF;UACE,MAAM,
|
|
1
|
+
{"version":3,"names":["useRef","useState","useMemo","useCallback","useEffect","MMKV","isConfigurationEqual","left","right","encryptionKey","id","path","defaultInstance","getDefaultInstance","useMMKV","configuration","instance","lastConfiguration","current","createMMKVHook","getter","key","mmkv","value","setValue","valueRef","set","v","newValue","delete","Uint8Array","Error","listener","addOnValueChangedListener","changedKey","remove","useMMKVString","getString","useMMKVNumber","getNumber","useMMKVBoolean","getBoolean","useMMKVBuffer","getBuffer","useMMKVObject","string","setString","undefined","JSON","parse","stringify","useMMKVListener","valueChangedListener","ref"],"sourceRoot":"../../src","sources":["hooks.ts"],"mappings":"AAAA,SAASA,MAAM,EAAEC,QAAQ,EAAEC,OAAO,EAAEC,WAAW,EAAEC,SAAS,QAAQ,OAAO;AACzE,SAASC,IAAI,QAA2B,QAAQ;AAEhD,SAASC,oBAAoB,CAC3BC,IAAwB,EACxBC,KAAyB,EAChB;EACT,IAAID,IAAI,IAAI,IAAI,IAAIC,KAAK,IAAI,IAAI,EAAE,OAAOD,IAAI,IAAI,IAAI,IAAIC,KAAK,IAAI,IAAI;EAEvE,OACED,IAAI,CAACE,aAAa,KAAKD,KAAK,CAACC,aAAa,IAC1CF,IAAI,CAACG,EAAE,KAAKF,KAAK,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AK,EAAEC,QAAQ,CAAC;AAC1B;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAAS2B,eAAe,CAC7BC,oBAA2C,EAC3CpC,QAAe,EACT;EACN,MAAMqC,GAAG,GAAGrD,MAAM,CAACoD,oBAAoB,CAAC;EACxCC,GAAG,CAACnC,OAAO,GAAGkC,oBAAoB;EAElC,MAAM9B,IAAI,GAAGN,QAAQ,IAAIH,kBAAkB,EAAE;EAE7CT,SAAS,CAAC,MAAM;IACd,MAAM4B,QAAQ,GAAGV,IAAI,CAACW,yBAAyB,CAAEC,UAAU,IAAK;MAC9DmB,GAAG,CAACnC,OAAO,CAACgB,UAAU,CAAC;IACzB,CAAC,CAAC;IACF,OAAO,MAAMF,QAAQ,CAACG,MAAM,EAAE;EAChC,CAAC,EAAE,CAACb,IAAI,CAAC,CAAC;AACZ"}
|
package/lib/typescript/MMKV.d.ts
CHANGED
|
@@ -37,17 +37,6 @@ export interface MMKVConfiguration {
|
|
|
37
37
|
* ```
|
|
38
38
|
*/
|
|
39
39
|
encryptionKey?: string;
|
|
40
|
-
/**
|
|
41
|
-
* Configure fast writes for MMKV.
|
|
42
|
-
*
|
|
43
|
-
* When set to `true`, all calls to `set(..)` do not overwrite the previous
|
|
44
|
-
* value to avoid storage resizing, this speeds up writes.
|
|
45
|
-
*
|
|
46
|
-
* Only enable this if you use a small MMKV storage, as this uses more RAM.
|
|
47
|
-
*
|
|
48
|
-
* @default false
|
|
49
|
-
*/
|
|
50
|
-
fastWrites?: boolean;
|
|
51
40
|
}
|
|
52
41
|
/**
|
|
53
42
|
* Represents a single MMKV instance.
|
|
@@ -123,7 +112,6 @@ export declare class MMKV implements MMKVInterface {
|
|
|
123
112
|
private nativeInstance;
|
|
124
113
|
private functionCache;
|
|
125
114
|
private id;
|
|
126
|
-
private fastWrites;
|
|
127
115
|
/**
|
|
128
116
|
* Creates a new MMKV instance with the given Configuration.
|
|
129
117
|
* If no custom `id` is supplied, `'mmkv.default'` will be used.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"MMKV.d.ts","sourceRoot":"","sources":["../../src/MMKV.ts"],"names":[],"mappings":"AAIA,UAAU,QAAQ;IAChB,MAAM,EAAE,MAAM,IAAI,CAAC;CACpB;AAED;;GAEG;AACH,MAAM,WAAW,iBAAiB;IAChC;;;;;;;;;;OAUG;IACH,EAAE,EAAE,MAAM,CAAC;IACX;;;;;;;OAOG;IACH,IAAI,CAAC,EAAE,MAAM,CAAC;IACd;;;;;;;;;OASG;IACH,aAAa,CAAC,EAAE,MAAM,CAAC;
|
|
1
|
+
{"version":3,"file":"MMKV.d.ts","sourceRoot":"","sources":["../../src/MMKV.ts"],"names":[],"mappings":"AAIA,UAAU,QAAQ;IAChB,MAAM,EAAE,MAAM,IAAI,CAAC;CACpB;AAED;;GAEG;AACH,MAAM,WAAW,iBAAiB;IAChC;;;;;;;;;;OAUG;IACH,EAAE,EAAE,MAAM,CAAC;IACX;;;;;;;OAOG;IACH,IAAI,CAAC,EAAE,MAAM,CAAC;IACd;;;;;;;;;OASG;IACH,aAAa,CAAC,EAAE,MAAM,CAAC;CACxB;AAED;;GAEG;AACH,UAAU,aAAa;IACrB;;OAEG;IACH,GAAG,EAAE,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,GAAG,MAAM,GAAG,MAAM,GAAG,UAAU,KAAK,IAAI,CAAC;IAC1E;;;;OAIG;IACH,UAAU,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,OAAO,GAAG,SAAS,CAAC;IACjD;;;;OAIG;IACH,SAAS,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,MAAM,GAAG,SAAS,CAAC;IAC/C;;;;OAIG;IACH,SAAS,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,MAAM,GAAG,SAAS,CAAC;IAC/C;;;;OAIG;IACH,SAAS,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,UAAU,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AAE,MAAM,GAAG,UAAU,GAAG,SAAS;IAI9C,QAAQ,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO;IAI9B,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI;IAMzB,UAAU,IAAI,MAAM,EAAE;IAItB,QAAQ,IAAI,IAAI;IAQhB,OAAO,CAAC,GAAG,EAAE,MAAM,GAAG,SAAS,GAAG,IAAI;IAKtC,QAAQ,IAAI,MAAM;IAGlB,MAAM,IAAI,MAAM;IAMhB,yBAAyB,CAAC,cAAc,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,IAAI,GAAG,QAAQ;CAY3E"}
|
|
@@ -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"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"hooks.d.ts","sourceRoot":"","sources":["../../src/hooks.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,IAAI,EAAE,iBAAiB,EAAE,MAAM,QAAQ,CAAC;AAuBjD;;GAEG;AACH,wBAAgB,OAAO,IAAI,IAAI,CAAC;AAChC;;;GAGG;AACH,wBAAgB,OAAO,CAAC,aAAa,EAAE,iBAAiB,GAAG,IAAI,CAAC;
|
|
1
|
+
{"version":3,"file":"hooks.d.ts","sourceRoot":"","sources":["../../src/hooks.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,IAAI,EAAE,iBAAiB,EAAE,MAAM,QAAQ,CAAC;AAuBjD;;GAEG;AACH,wBAAgB,OAAO,IAAI,IAAI,CAAC;AAChC;;;GAGG;AACH,wBAAgB,OAAO,CAAC,aAAa,EAAE,iBAAiB,GAAG,IAAI,CAAC;AAgFhE;;;;;;;;;GASG;AACH,eAAO,MAAM,aAAa,QAlEjB,MAAM,aACA,IAAI,kIAC8B,IAAI,CAkEpD,CAAC;AAEF;;;;;;;;;GASG;AACH,eAAO,MAAM,aAAa,QAhFjB,MAAM,aACA,IAAI,kIAC8B,IAAI,CAgFpD,CAAC;AACF;;;;;;;;;GASG;AACH,eAAO,MAAM,cAAc,QA7FlB,MAAM,aACA,IAAI,sIAC8B,IAAI,CA6FpD,CAAC;AACF;;;;;;;;;GASG;AACH,eAAO,MAAM,aAAa,QA1GjB,MAAM,aACA,IAAI,kJAC8B,IAAI,CA0GpD,CAAC;AACF;;;;;;;;;;;GAWG;AACH,wBAAgB,aAAa,CAAC,CAAC,EAC7B,GAAG,EAAE,MAAM,EACX,QAAQ,CAAC,EAAE,IAAI,GACd,CAAC,KAAK,EAAE,CAAC,GAAG,SAAS,EAAE,QAAQ,EAAE,CAAC,KAAK,EAAE,CAAC,GAAG,SAAS,KAAK,IAAI,CAAC,CAqBlE;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,eAAe,CAC7B,oBAAoB,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,IAAI,EAC3C,QAAQ,CAAC,EAAE,IAAI,GACd,IAAI,CAYN"}
|
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,20 +54,21 @@
|
|
|
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.
|
|
65
|
-
"
|
|
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",
|
|
66
|
+
"eslint-plugin-jest": "^27.2.1",
|
|
67
|
+
"prettier": "^2.8.7",
|
|
66
68
|
"react": "^18.2.0",
|
|
67
|
-
"react-native": "^0.71.
|
|
68
|
-
"react-native-builder-bob": "^0.20.
|
|
69
|
-
"release-it": "^15.
|
|
69
|
+
"react-native": "^0.71.6",
|
|
70
|
+
"react-native-builder-bob": "^0.20.4",
|
|
71
|
+
"release-it": "^15.10.1",
|
|
70
72
|
"typescript": "^4.9.4"
|
|
71
73
|
},
|
|
72
74
|
"peerDependencies": {
|
|
@@ -127,6 +129,9 @@
|
|
|
127
129
|
"@react-native-community",
|
|
128
130
|
"prettier"
|
|
129
131
|
],
|
|
132
|
+
"plugins": [
|
|
133
|
+
"jest"
|
|
134
|
+
],
|
|
130
135
|
"rules": {
|
|
131
136
|
"prettier/prettier": [
|
|
132
137
|
"error",
|
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
|
+
};
|