react-native-mmkv-dz 2.5.1
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 +219 -0
- package/MMKV/CHANGELOG.md +553 -0
- package/MMKV/Core/CMakeLists.txt +153 -0
- package/MMKV/Core/CodedInputData.cpp +228 -0
- package/MMKV/Core/CodedInputData.h +83 -0
- package/MMKV/Core/CodedInputDataCrypt.cpp +280 -0
- package/MMKV/Core/CodedInputDataCrypt.h +87 -0
- package/MMKV/Core/CodedInputDataCrypt_OSX.cpp +62 -0
- package/MMKV/Core/CodedInputData_OSX.cpp +92 -0
- package/MMKV/Core/CodedOutputData.cpp +174 -0
- package/MMKV/Core/CodedOutputData.h +82 -0
- package/MMKV/Core/Core.xcodeproj/project.pbxproj +702 -0
- package/MMKV/Core/Core.xcodeproj/project.xcworkspace/contents.xcworkspacedata +7 -0
- package/MMKV/Core/Core.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist +8 -0
- package/MMKV/Core/Core.xcodeproj/xcshareddata/xcschemes/Core.xcscheme +67 -0
- package/MMKV/Core/Core.xcodeproj/xcshareddata/xcschemes/MMKVWatchCore.xcscheme +67 -0
- package/MMKV/Core/InterProcessLock.cpp +186 -0
- package/MMKV/Core/InterProcessLock.h +119 -0
- package/MMKV/Core/InterProcessLock_Android.cpp +103 -0
- package/MMKV/Core/InterProcessLock_Win32.cpp +108 -0
- package/MMKV/Core/KeyValueHolder.cpp +236 -0
- package/MMKV/Core/KeyValueHolder.h +118 -0
- package/MMKV/Core/MMBuffer.cpp +185 -0
- package/MMKV/Core/MMBuffer.h +107 -0
- package/MMKV/Core/MMKV.cpp +1418 -0
- package/MMKV/Core/MMKV.h +386 -0
- package/MMKV/Core/MMKVLog.cpp +127 -0
- package/MMKV/Core/MMKVLog.h +86 -0
- package/MMKV/Core/MMKVLog_Android.cpp +79 -0
- package/MMKV/Core/MMKVMetaInfo.hpp +81 -0
- package/MMKV/Core/MMKVPredef.h +245 -0
- package/MMKV/Core/MMKV_Android.cpp +259 -0
- package/MMKV/Core/MMKV_IO.cpp +1119 -0
- package/MMKV/Core/MMKV_IO.h +57 -0
- package/MMKV/Core/MMKV_OSX.cpp +347 -0
- package/MMKV/Core/MMKV_OSX.h +51 -0
- package/MMKV/Core/MemoryFile.cpp +537 -0
- package/MMKV/Core/MemoryFile.h +182 -0
- package/MMKV/Core/MemoryFile_Android.cpp +211 -0
- package/MMKV/Core/MemoryFile_Linux.cpp +120 -0
- package/MMKV/Core/MemoryFile_OSX.cpp +142 -0
- package/MMKV/Core/MemoryFile_Win32.cpp +536 -0
- package/MMKV/Core/MiniPBCoder.cpp +366 -0
- package/MMKV/Core/MiniPBCoder.h +129 -0
- package/MMKV/Core/MiniPBCoder_OSX.cpp +228 -0
- package/MMKV/Core/PBEncodeItem.hpp +86 -0
- package/MMKV/Core/PBUtility.cpp +61 -0
- package/MMKV/Core/PBUtility.h +153 -0
- package/MMKV/Core/ScopedLock.hpp +69 -0
- package/MMKV/Core/ThreadLock.cpp +68 -0
- package/MMKV/Core/ThreadLock.h +78 -0
- package/MMKV/Core/ThreadLock_Win32.cpp +89 -0
- package/MMKV/Core/aes/AESCrypt.cpp +256 -0
- package/MMKV/Core/aes/AESCrypt.h +107 -0
- package/MMKV/Core/aes/openssl/openssl_aes-armv4.S +1231 -0
- package/MMKV/Core/aes/openssl/openssl_aes.h +118 -0
- package/MMKV/Core/aes/openssl/openssl_aes_core.cpp +1044 -0
- package/MMKV/Core/aes/openssl/openssl_aes_locl.h +38 -0
- package/MMKV/Core/aes/openssl/openssl_aesv8-armx.S +308 -0
- package/MMKV/Core/aes/openssl/openssl_arm_arch.h +84 -0
- package/MMKV/Core/aes/openssl/openssl_cfb128.cpp +97 -0
- package/MMKV/Core/aes/openssl/openssl_md32_common.h +254 -0
- package/MMKV/Core/aes/openssl/openssl_md5.h +49 -0
- package/MMKV/Core/aes/openssl/openssl_md5_dgst.cpp +166 -0
- package/MMKV/Core/aes/openssl/openssl_md5_locl.h +75 -0
- package/MMKV/Core/aes/openssl/openssl_md5_one.cpp +30 -0
- package/MMKV/Core/aes/openssl/openssl_opensslconf.h +271 -0
- package/MMKV/Core/core.vcxproj +186 -0
- package/MMKV/Core/core.vcxproj.filters +150 -0
- package/MMKV/Core/crc32/Checksum.h +67 -0
- package/MMKV/Core/crc32/crc32_armv8.cpp +132 -0
- package/MMKV/Core/crc32/zlib/crc32.cpp +55 -0
- package/MMKV/Core/crc32/zlib/crc32.h +48 -0
- package/MMKV/Core/crc32/zlib/zconf.h +380 -0
- package/MMKV/Core/crc32/zlib/zutil.h +25 -0
- package/MMKV/LICENSE.TXT +193 -0
- package/MMKV/README.md +288 -0
- package/README.md +221 -0
- package/android/CMakeLists.txt +71 -0
- package/android/build.gradle +371 -0
- package/android/gradle.properties +4 -0
- package/android/src/main/AndroidManifest.xml +4 -0
- package/android/src/main/cpp/MmkvHostObject.cpp +302 -0
- package/android/src/main/cpp/MmkvHostObject.h +26 -0
- package/android/src/main/cpp/cpp-adapter.cpp +65 -0
- package/android/src/main/java/com/reactnativemmkv/MmkvModule.java +49 -0
- package/android/src/main/java/com/reactnativemmkv/MmkvPackage.java +26 -0
- package/cpp/TypedArray.cpp +341 -0
- package/cpp/TypedArray.h +175 -0
- package/ios/JSIUtils.h +50 -0
- package/ios/JSIUtils.mm +194 -0
- package/ios/Mmkv.xcodeproj/project.pbxproj +291 -0
- package/ios/MmkvHostObject.h +27 -0
- package/ios/MmkvHostObject.mm +299 -0
- package/ios/MmkvModule.h +5 -0
- package/ios/MmkvModule.mm +73 -0
- package/lib/commonjs/MMKV.js +146 -0
- package/lib/commonjs/MMKV.js.map +1 -0
- package/lib/commonjs/PlatformChecker.js +16 -0
- package/lib/commonjs/PlatformChecker.js.map +1 -0
- package/lib/commonjs/createMMKV.js +66 -0
- package/lib/commonjs/createMMKV.js.map +1 -0
- package/lib/commonjs/createMMKV.mock.js +40 -0
- package/lib/commonjs/createMMKV.mock.js.map +1 -0
- package/lib/commonjs/createMMKV.web.js +77 -0
- package/lib/commonjs/createMMKV.web.js.map +1 -0
- package/lib/commonjs/createTextEncoder.js +24 -0
- package/lib/commonjs/createTextEncoder.js.map +1 -0
- package/lib/commonjs/hooks.js +200 -0
- package/lib/commonjs/hooks.js.map +1 -0
- package/lib/commonjs/index.js +32 -0
- package/lib/commonjs/index.js.map +1 -0
- package/lib/module/MMKV.js +134 -0
- package/lib/module/MMKV.js.map +1 -0
- package/lib/module/PlatformChecker.js +9 -0
- package/lib/module/PlatformChecker.js.map +1 -0
- package/lib/module/createMMKV.js +55 -0
- package/lib/module/createMMKV.js.map +1 -0
- package/lib/module/createMMKV.mock.js +31 -0
- package/lib/module/createMMKV.mock.js.map +1 -0
- package/lib/module/createMMKV.web.js +67 -0
- package/lib/module/createMMKV.web.js.map +1 -0
- package/lib/module/createTextEncoder.js +17 -0
- package/lib/module/createTextEncoder.js.map +1 -0
- package/lib/module/hooks.js +181 -0
- package/lib/module/hooks.js.map +1 -0
- package/lib/module/index.js +3 -0
- package/lib/module/index.js.map +1 -0
- package/lib/typescript/MMKV.d.ts +137 -0
- package/lib/typescript/PlatformChecker.d.ts +1 -0
- package/lib/typescript/createMMKV.d.ts +6 -0
- package/lib/typescript/createMMKV.mock.d.ts +2 -0
- package/lib/typescript/createMMKV.web.d.ts +2 -0
- package/lib/typescript/createTextEncoder.d.ts +1 -0
- package/lib/typescript/hooks.d.ts +81 -0
- package/lib/typescript/index.d.ts +2 -0
- package/package.json +168 -0
- package/react-native-mmkv.podspec +32 -0
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.createMockMMKV = void 0;
|
|
7
|
+
|
|
8
|
+
/* Mock MMKV instance for use in tests */
|
|
9
|
+
const createMockMMKV = () => {
|
|
10
|
+
const storage = new Map();
|
|
11
|
+
return {
|
|
12
|
+
clearAll: () => storage.clear(),
|
|
13
|
+
delete: key => storage.delete(key),
|
|
14
|
+
set: (key, value) => storage.set(key, value),
|
|
15
|
+
getString: key => {
|
|
16
|
+
const result = storage.get(key);
|
|
17
|
+
return typeof result === 'string' ? result : undefined;
|
|
18
|
+
},
|
|
19
|
+
getNumber: key => {
|
|
20
|
+
const result = storage.get(key);
|
|
21
|
+
return typeof result === 'number' ? result : undefined;
|
|
22
|
+
},
|
|
23
|
+
getBoolean: key => {
|
|
24
|
+
const result = storage.get(key);
|
|
25
|
+
return typeof result === 'boolean' ? result : undefined;
|
|
26
|
+
},
|
|
27
|
+
getBuffer: key => {
|
|
28
|
+
const result = storage.get(key);
|
|
29
|
+
return result instanceof Uint8Array ? result : undefined;
|
|
30
|
+
},
|
|
31
|
+
getAllKeys: () => Array.from(storage.keys()),
|
|
32
|
+
contains: key => storage.has(key),
|
|
33
|
+
recrypt: () => {
|
|
34
|
+
console.warn('Encryption is not supported in mocked MMKV instances!');
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
exports.createMockMMKV = createMockMMKV;
|
|
40
|
+
//# sourceMappingURL=createMMKV.mock.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["createMMKV.mock.ts"],"names":["createMockMMKV","storage","Map","clearAll","clear","delete","key","set","value","getString","result","get","undefined","getNumber","getBoolean","getBuffer","Uint8Array","getAllKeys","Array","from","keys","contains","has","recrypt","console","warn"],"mappings":";;;;;;;AAEA;AACO,MAAMA,cAAc,GAAG,MAAkB;AAC9C,QAAMC,OAAO,GAAG,IAAIC,GAAJ,EAAhB;AAEA,SAAO;AACLC,IAAAA,QAAQ,EAAE,MAAMF,OAAO,CAACG,KAAR,EADX;AAELC,IAAAA,MAAM,EAAGC,GAAD,IAASL,OAAO,CAACI,MAAR,CAAeC,GAAf,CAFZ;AAGLC,IAAAA,GAAG,EAAE,CAACD,GAAD,EAAME,KAAN,KAAgBP,OAAO,CAACM,GAAR,CAAYD,GAAZ,EAAiBE,KAAjB,CAHhB;AAILC,IAAAA,SAAS,EAAGH,GAAD,IAAS;AAClB,YAAMI,MAAM,GAAGT,OAAO,CAACU,GAAR,CAAYL,GAAZ,CAAf;AACA,aAAO,OAAOI,MAAP,KAAkB,QAAlB,GAA6BA,MAA7B,GAAsCE,SAA7C;AACD,KAPI;AAQLC,IAAAA,SAAS,EAAGP,GAAD,IAAS;AAClB,YAAMI,MAAM,GAAGT,OAAO,CAACU,GAAR,CAAYL,GAAZ,CAAf;AACA,aAAO,OAAOI,MAAP,KAAkB,QAAlB,GAA6BA,MAA7B,GAAsCE,SAA7C;AACD,KAXI;AAYLE,IAAAA,UAAU,EAAGR,GAAD,IAAS;AACnB,YAAMI,MAAM,GAAGT,OAAO,CAACU,GAAR,CAAYL,GAAZ,CAAf;AACA,aAAO,OAAOI,MAAP,KAAkB,SAAlB,GAA8BA,MAA9B,GAAuCE,SAA9C;AACD,KAfI;AAgBLG,IAAAA,SAAS,EAAGT,GAAD,IAAS;AAClB,YAAMI,MAAM,GAAGT,OAAO,CAACU,GAAR,CAAYL,GAAZ,CAAf;AACA,aAAOI,MAAM,YAAYM,UAAlB,GAA+BN,MAA/B,GAAwCE,SAA/C;AACD,KAnBI;AAoBLK,IAAAA,UAAU,EAAE,MAAMC,KAAK,CAACC,IAAN,CAAWlB,OAAO,CAACmB,IAAR,EAAX,CApBb;AAqBLC,IAAAA,QAAQ,EAAGf,GAAD,IAASL,OAAO,CAACqB,GAAR,CAAYhB,GAAZ,CArBd;AAsBLiB,IAAAA,OAAO,EAAE,MAAM;AACbC,MAAAA,OAAO,CAACC,IAAR,CAAa,uDAAb;AACD;AAxBI,GAAP;AA0BD,CA7BM","sourcesContent":["import type { NativeMMKV } from 'react-native-mmkv';\n\n/* Mock MMKV instance for use in tests */\nexport const createMockMMKV = (): NativeMMKV => {\n const storage = new Map<string, string | boolean | number | Uint8Array>();\n\n return {\n clearAll: () => storage.clear(),\n delete: (key) => storage.delete(key),\n set: (key, value) => storage.set(key, value),\n getString: (key) => {\n const result = storage.get(key);\n return typeof result === 'string' ? result : undefined;\n },\n getNumber: (key) => {\n const result = storage.get(key);\n return typeof result === 'number' ? result : undefined;\n },\n getBoolean: (key) => {\n const result = storage.get(key);\n return typeof result === 'boolean' ? result : undefined;\n },\n getBuffer: (key) => {\n const result = storage.get(key);\n return result instanceof Uint8Array ? result : undefined;\n },\n getAllKeys: () => Array.from(storage.keys()),\n contains: (key) => storage.has(key),\n recrypt: () => {\n console.warn('Encryption is not supported in mocked MMKV instances!');\n },\n };\n};\n"]}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.createMMKV = void 0;
|
|
7
|
+
|
|
8
|
+
var _createTextEncoder = require("./createTextEncoder");
|
|
9
|
+
|
|
10
|
+
var _window$document;
|
|
11
|
+
|
|
12
|
+
const canUseDOM = typeof window !== 'undefined' && ((_window$document = window.document) === null || _window$document === void 0 ? void 0 : _window$document.createElement) != null;
|
|
13
|
+
|
|
14
|
+
const createMMKV = config => {
|
|
15
|
+
if (config.id !== 'mmkv.default') {
|
|
16
|
+
throw new Error("MMKV: 'id' is not supported on Web!");
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
if (config.encryptionKey != null) {
|
|
20
|
+
throw new Error("MMKV: 'encryptionKey' is not supported on Web!");
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
if (config.path != null) {
|
|
24
|
+
throw new Error("MMKV: 'path' is not supported on Web!");
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const storage = () => {
|
|
28
|
+
var _ref, _global$localStorage, _global, _window;
|
|
29
|
+
|
|
30
|
+
if (!canUseDOM) {
|
|
31
|
+
throw new Error('Tried to access storage on the server. Did you forget to call this in useEffect?');
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const domStorage = (_ref = (_global$localStorage = (_global = global) === null || _global === void 0 ? void 0 : _global.localStorage) !== null && _global$localStorage !== void 0 ? _global$localStorage : (_window = window) === null || _window === void 0 ? void 0 : _window.localStorage) !== null && _ref !== void 0 ? _ref : localStorage;
|
|
35
|
+
|
|
36
|
+
if (domStorage == null) {
|
|
37
|
+
throw new Error(`Could not find 'localStorage' instance!`);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
return domStorage;
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
const textEncoder = (0, _createTextEncoder.createTextEncoder)();
|
|
44
|
+
return {
|
|
45
|
+
clearAll: () => storage().clear(),
|
|
46
|
+
delete: key => storage().removeItem(key),
|
|
47
|
+
set: (key, value) => storage().setItem(key, value.toString()),
|
|
48
|
+
getString: key => {
|
|
49
|
+
var _storage$getItem;
|
|
50
|
+
|
|
51
|
+
return (_storage$getItem = storage().getItem(key)) !== null && _storage$getItem !== void 0 ? _storage$getItem : undefined;
|
|
52
|
+
},
|
|
53
|
+
getNumber: key => {
|
|
54
|
+
const value = storage().getItem(key);
|
|
55
|
+
if (value == null) return undefined;
|
|
56
|
+
return Number(value);
|
|
57
|
+
},
|
|
58
|
+
getBoolean: key => {
|
|
59
|
+
const value = storage().getItem(key);
|
|
60
|
+
if (value == null) return undefined;
|
|
61
|
+
return value === 'true';
|
|
62
|
+
},
|
|
63
|
+
getBuffer: key => {
|
|
64
|
+
const value = storage().getItem(key);
|
|
65
|
+
if (value == null) return undefined;
|
|
66
|
+
return textEncoder.encode(value);
|
|
67
|
+
},
|
|
68
|
+
getAllKeys: () => Object.keys(storage()),
|
|
69
|
+
contains: key => storage().getItem(key) != null,
|
|
70
|
+
recrypt: () => {
|
|
71
|
+
throw new Error('`recrypt(..)` is not supported on Web!');
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
exports.createMMKV = createMMKV;
|
|
77
|
+
//# sourceMappingURL=createMMKV.web.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["createMMKV.web.ts"],"names":["canUseDOM","window","document","createElement","createMMKV","config","id","Error","encryptionKey","path","storage","domStorage","global","localStorage","textEncoder","clearAll","clear","delete","key","removeItem","set","value","setItem","toString","getString","getItem","undefined","getNumber","Number","getBoolean","getBuffer","encode","getAllKeys","Object","keys","contains","recrypt"],"mappings":";;;;;;;AAEA;;;;AAEA,MAAMA,SAAS,GACb,OAAOC,MAAP,KAAkB,WAAlB,IAAiC,qBAAAA,MAAM,CAACC,QAAP,sEAAiBC,aAAjB,KAAkC,IADrE;;AAGO,MAAMC,UAAU,GAAIC,MAAD,IAA2C;AACnE,MAAIA,MAAM,CAACC,EAAP,KAAc,cAAlB,EAAkC;AAChC,UAAM,IAAIC,KAAJ,CAAU,qCAAV,CAAN;AACD;;AACD,MAAIF,MAAM,CAACG,aAAP,IAAwB,IAA5B,EAAkC;AAChC,UAAM,IAAID,KAAJ,CAAU,gDAAV,CAAN;AACD;;AACD,MAAIF,MAAM,CAACI,IAAP,IAAe,IAAnB,EAAyB;AACvB,UAAM,IAAIF,KAAJ,CAAU,uCAAV,CAAN;AACD;;AAED,QAAMG,OAAO,GAAG,MAAM;AAAA;;AACpB,QAAI,CAACV,SAAL,EAAgB;AACd,YAAM,IAAIO,KAAJ,CACJ,kFADI,CAAN;AAGD;;AACD,UAAMI,UAAU,8CACdC,MADc,4CACd,QAAQC,YADM,kFACUZ,MADV,4CACU,QAAQY,YADlB,uCACkCA,YADlD;;AAEA,QAAIF,UAAU,IAAI,IAAlB,EAAwB;AACtB,YAAM,IAAIJ,KAAJ,CAAW,yCAAX,CAAN;AACD;;AACD,WAAOI,UAAP;AACD,GAZD;;AAcA,QAAMG,WAAW,GAAG,2CAApB;AAEA,SAAO;AACLC,IAAAA,QAAQ,EAAE,MAAML,OAAO,GAAGM,KAAV,EADX;AAELC,IAAAA,MAAM,EAAGC,GAAD,IAASR,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* global localStorage */\nimport type { MMKVConfiguration, NativeMMKV } from 'react-native-mmkv';\nimport { createTextEncoder } from './createTextEncoder';\n\nconst canUseDOM =\n typeof window !== 'undefined' && window.document?.createElement != null;\n\nexport const createMMKV = (config: MMKVConfiguration): NativeMMKV => {\n if (config.id !== 'mmkv.default') {\n throw new Error(\"MMKV: 'id' is not supported on Web!\");\n }\n if (config.encryptionKey != null) {\n throw new Error(\"MMKV: 'encryptionKey' is not supported on Web!\");\n }\n if (config.path != null) {\n throw new Error(\"MMKV: 'path' is not supported on Web!\");\n }\n\n const storage = () => {\n if (!canUseDOM) {\n throw new Error(\n 'Tried to access storage on the server. Did you forget to call this in useEffect?'\n );\n }\n const domStorage =\n global?.localStorage ?? window?.localStorage ?? localStorage;\n if (domStorage == null) {\n throw new Error(`Could not find 'localStorage' instance!`);\n }\n return domStorage;\n };\n\n const textEncoder = createTextEncoder();\n\n return {\n clearAll: () => storage().clear(),\n delete: (key) => storage().removeItem(key),\n set: (key, value) => storage().setItem(key, value.toString()),\n getString: (key) => storage().getItem(key) ?? undefined,\n getNumber: (key) => {\n const value = storage().getItem(key);\n if (value == null) return undefined;\n return Number(value);\n },\n getBoolean: (key) => {\n const value = storage().getItem(key);\n if (value == null) return undefined;\n return value === 'true';\n },\n getBuffer: (key) => {\n const value = storage().getItem(key);\n if (value == null) return undefined;\n return textEncoder.encode(value);\n },\n getAllKeys: () => Object.keys(storage()),\n contains: (key) => storage().getItem(key) != null,\n recrypt: () => {\n throw new Error('`recrypt(..)` is not supported on Web!');\n },\n };\n};\n"]}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.createTextEncoder = createTextEncoder;
|
|
7
|
+
|
|
8
|
+
/* global TextEncoder */
|
|
9
|
+
function createTextEncoder() {
|
|
10
|
+
if (global.TextEncoder != null) {
|
|
11
|
+
return new global.TextEncoder();
|
|
12
|
+
} else {
|
|
13
|
+
return {
|
|
14
|
+
encode: () => {
|
|
15
|
+
throw new Error('TextEncoder is not supported in this environment!');
|
|
16
|
+
},
|
|
17
|
+
encodeInto: () => {
|
|
18
|
+
throw new Error('TextEncoder is not supported in this environment!');
|
|
19
|
+
},
|
|
20
|
+
encoding: 'utf-8'
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
//# sourceMappingURL=createTextEncoder.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["createTextEncoder.ts"],"names":["createTextEncoder","global","TextEncoder","encode","Error","encodeInto","encoding"],"mappings":";;;;;;;AAAA;AACO,SAASA,iBAAT,GAA0C;AAC/C,MAAIC,MAAM,CAACC,WAAP,IAAsB,IAA1B,EAAgC;AAC9B,WAAO,IAAID,MAAM,CAACC,WAAX,EAAP;AACD,GAFD,MAEO;AACL,WAAO;AACLC,MAAAA,MAAM,EAAE,MAAM;AACZ,cAAM,IAAIC,KAAJ,CAAU,mDAAV,CAAN;AACD,OAHI;AAILC,MAAAA,UAAU,EAAE,MAAM;AAChB,cAAM,IAAID,KAAJ,CAAU,mDAAV,CAAN;AACD,OANI;AAOLE,MAAAA,QAAQ,EAAE;AAPL,KAAP;AASD;AACF","sourcesContent":["/* global TextEncoder */\nexport function createTextEncoder(): TextEncoder {\n if (global.TextEncoder != null) {\n return new global.TextEncoder();\n } else {\n return {\n encode: () => {\n throw new Error('TextEncoder is not supported in this environment!');\n },\n encodeInto: () => {\n throw new Error('TextEncoder is not supported in this environment!');\n },\n encoding: 'utf-8',\n };\n }\n}\n"]}
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.useMMKV = useMMKV;
|
|
7
|
+
exports.useMMKVBuffer = exports.useMMKVBoolean = void 0;
|
|
8
|
+
exports.useMMKVListener = useMMKVListener;
|
|
9
|
+
exports.useMMKVNumber = void 0;
|
|
10
|
+
exports.useMMKVObject = useMMKVObject;
|
|
11
|
+
exports.useMMKVString = void 0;
|
|
12
|
+
|
|
13
|
+
var _react = require("react");
|
|
14
|
+
|
|
15
|
+
var _MMKV = require("./MMKV");
|
|
16
|
+
|
|
17
|
+
function isConfigurationEqual(left, right) {
|
|
18
|
+
if (left == null || right == null) return left == null && right == null;
|
|
19
|
+
return left.encryptionKey === right.encryptionKey && left.id === right.id && left.path === right.path;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
let defaultInstance = null;
|
|
23
|
+
|
|
24
|
+
function getDefaultInstance() {
|
|
25
|
+
if (defaultInstance == null) {
|
|
26
|
+
defaultInstance = new _MMKV.MMKV();
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
return defaultInstance;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Use the default, shared MMKV instance.
|
|
33
|
+
*/
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
function useMMKV(configuration) {
|
|
37
|
+
const instance = (0, _react.useRef)();
|
|
38
|
+
const lastConfiguration = (0, _react.useRef)();
|
|
39
|
+
if (configuration == null) return getDefaultInstance();
|
|
40
|
+
|
|
41
|
+
if (instance.current == null || !isConfigurationEqual(lastConfiguration.current, configuration)) {
|
|
42
|
+
lastConfiguration.current = configuration;
|
|
43
|
+
instance.current = new _MMKV.MMKV(configuration);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
return instance.current;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function createMMKVHook(getter) {
|
|
50
|
+
return (key, instance) => {
|
|
51
|
+
const mmkv = instance !== null && instance !== void 0 ? instance : getDefaultInstance();
|
|
52
|
+
const [value, setValue] = (0, _react.useState)(() => getter(mmkv, key));
|
|
53
|
+
const valueRef = (0, _react.useRef)(value);
|
|
54
|
+
valueRef.current = value; // update value by user set
|
|
55
|
+
|
|
56
|
+
const set = (0, _react.useCallback)(v => {
|
|
57
|
+
const newValue = typeof v === 'function' ? v(valueRef.current) : v;
|
|
58
|
+
|
|
59
|
+
switch (typeof newValue) {
|
|
60
|
+
case 'number':
|
|
61
|
+
case 'string':
|
|
62
|
+
case 'boolean':
|
|
63
|
+
mmkv.set(key, newValue);
|
|
64
|
+
break;
|
|
65
|
+
|
|
66
|
+
case 'undefined':
|
|
67
|
+
mmkv.delete(key);
|
|
68
|
+
break;
|
|
69
|
+
|
|
70
|
+
default:
|
|
71
|
+
throw new Error(`MMKV: Type ${typeof newValue} is not supported!`);
|
|
72
|
+
}
|
|
73
|
+
}, [key, mmkv]); // update value if key or instance changes
|
|
74
|
+
|
|
75
|
+
(0, _react.useEffect)(() => {
|
|
76
|
+
setValue(getter(mmkv, key));
|
|
77
|
+
}, [key, mmkv]); // update value if it changes somewhere else (second hook, same key)
|
|
78
|
+
|
|
79
|
+
(0, _react.useEffect)(() => {
|
|
80
|
+
const listener = mmkv.addOnValueChangedListener(changedKey => {
|
|
81
|
+
if (changedKey === key) {
|
|
82
|
+
setValue(getter(mmkv, key));
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
return () => listener.remove();
|
|
86
|
+
}, [key, mmkv]);
|
|
87
|
+
return [value, set];
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Use the string value of the given `key` from the given MMKV storage instance.
|
|
92
|
+
*
|
|
93
|
+
* If no instance is provided, a shared default instance will be used.
|
|
94
|
+
*
|
|
95
|
+
* @example
|
|
96
|
+
* ```ts
|
|
97
|
+
* const [username, setUsername] = useMMKVString("user.name")
|
|
98
|
+
* ```
|
|
99
|
+
*/
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
const useMMKVString = createMMKVHook((instance, key) => instance.getString(key));
|
|
103
|
+
/**
|
|
104
|
+
* Use the number value of the given `key` from the given MMKV storage instance.
|
|
105
|
+
*
|
|
106
|
+
* If no instance is provided, a shared default instance will be used.
|
|
107
|
+
*
|
|
108
|
+
* @example
|
|
109
|
+
* ```ts
|
|
110
|
+
* const [age, setAge] = useMMKVNumber("user.age")
|
|
111
|
+
* ```
|
|
112
|
+
*/
|
|
113
|
+
|
|
114
|
+
exports.useMMKVString = useMMKVString;
|
|
115
|
+
const useMMKVNumber = createMMKVHook((instance, key) => instance.getNumber(key));
|
|
116
|
+
/**
|
|
117
|
+
* Use the boolean value of the given `key` from the given MMKV storage instance.
|
|
118
|
+
*
|
|
119
|
+
* If no instance is provided, a shared default instance will be used.
|
|
120
|
+
*
|
|
121
|
+
* @example
|
|
122
|
+
* ```ts
|
|
123
|
+
* const [isPremiumAccount, setIsPremiumAccount] = useMMKVBoolean("user.isPremium")
|
|
124
|
+
* ```
|
|
125
|
+
*/
|
|
126
|
+
|
|
127
|
+
exports.useMMKVNumber = useMMKVNumber;
|
|
128
|
+
const useMMKVBoolean = createMMKVHook((instance, key) => instance.getBoolean(key));
|
|
129
|
+
/**
|
|
130
|
+
* Use the buffer value (unsigned 8-bit (0-255)) of the given `key` from the given MMKV storage instance.
|
|
131
|
+
*
|
|
132
|
+
* If no instance is provided, a shared default instance will be used.
|
|
133
|
+
*
|
|
134
|
+
* @example
|
|
135
|
+
* ```ts
|
|
136
|
+
* const [privateKey, setPrivateKey] = useMMKVBuffer("user.privateKey")
|
|
137
|
+
* ```
|
|
138
|
+
*/
|
|
139
|
+
|
|
140
|
+
exports.useMMKVBoolean = useMMKVBoolean;
|
|
141
|
+
const useMMKVBuffer = createMMKVHook((instance, key) => instance.getBuffer(key));
|
|
142
|
+
/**
|
|
143
|
+
* Use an object value of the given `key` from the given MMKV storage instance.
|
|
144
|
+
*
|
|
145
|
+
* If no instance is provided, a shared default instance will be used.
|
|
146
|
+
*
|
|
147
|
+
* The object will be serialized using `JSON`.
|
|
148
|
+
*
|
|
149
|
+
* @example
|
|
150
|
+
* ```ts
|
|
151
|
+
* const [user, setUser] = useMMKVObject<User>("user")
|
|
152
|
+
* ```
|
|
153
|
+
*/
|
|
154
|
+
|
|
155
|
+
exports.useMMKVBuffer = useMMKVBuffer;
|
|
156
|
+
|
|
157
|
+
function useMMKVObject(key, instance) {
|
|
158
|
+
const [string, setString] = useMMKVString(key, instance);
|
|
159
|
+
const value = (0, _react.useMemo)(() => {
|
|
160
|
+
if (string == null) return undefined;
|
|
161
|
+
return JSON.parse(string);
|
|
162
|
+
}, [string]);
|
|
163
|
+
const setValue = (0, _react.useCallback)(v => {
|
|
164
|
+
if (v == null) {
|
|
165
|
+
// Clear the Value
|
|
166
|
+
setString(undefined);
|
|
167
|
+
} else {
|
|
168
|
+
// Store the Object as a serialized Value
|
|
169
|
+
setString(JSON.stringify(v));
|
|
170
|
+
}
|
|
171
|
+
}, [setString]);
|
|
172
|
+
return [value, setValue];
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Listen for changes in the given MMKV storage instance.
|
|
176
|
+
* If no instance is passed, the default instance will be used.
|
|
177
|
+
* @param valueChangedListener The function to call whenever a value inside the storage instance changes
|
|
178
|
+
* @param instance The instance to listen to changes to (or the default instance)
|
|
179
|
+
*
|
|
180
|
+
* @example
|
|
181
|
+
* ```ts
|
|
182
|
+
* useMMKVListener((key) => {
|
|
183
|
+
* console.log(`Value for "${key}" changed!`)
|
|
184
|
+
* })
|
|
185
|
+
* ```
|
|
186
|
+
*/
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
function useMMKVListener(valueChangedListener, instance) {
|
|
190
|
+
const ref = (0, _react.useRef)(valueChangedListener);
|
|
191
|
+
ref.current = valueChangedListener;
|
|
192
|
+
const mmkv = instance !== null && instance !== void 0 ? instance : getDefaultInstance();
|
|
193
|
+
(0, _react.useEffect)(() => {
|
|
194
|
+
const listener = mmkv.addOnValueChangedListener(changedKey => {
|
|
195
|
+
ref.current(changedKey);
|
|
196
|
+
});
|
|
197
|
+
return () => listener.remove();
|
|
198
|
+
}, [mmkv]);
|
|
199
|
+
}
|
|
200
|
+
//# sourceMappingURL=hooks.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["hooks.ts"],"names":["isConfigurationEqual","left","right","encryptionKey","id","path","defaultInstance","getDefaultInstance","MMKV","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"],"mappings":";;;;;;;;;;;;AAAA;;AACA;;AAEA,SAASA,oBAAT,CACEC,IADF,EAEEC,KAFF,EAGW;AACT,MAAID,IAAI,IAAI,IAAR,IAAgBC,KAAK,IAAI,IAA7B,EAAmC,OAAOD,IAAI,IAAI,IAAR,IAAgBC,KAAK,IAAI,IAAhC;AAEnC,SACED,IAAI,CAACE,aAAL,KAAuBD,KAAK,CAACC,aAA7B,IACAF,IAAI,CAACG,EAAL,KAAYF,KAAK,CAACE,EADlB,IAEAH,IAAI,CAACI,IAAL,KAAcH,KAAK,CAACG,IAHtB;AAKD;;AAED,IAAIC,eAA4B,GAAG,IAAnC;;AACA,SAASC,kBAAT,GAAoC;AAClC,MAAID,eAAe,IAAI,IAAvB,EAA6B;AAC3BA,IAAAA,eAAe,GAAG,IAAIE,UAAJ,EAAlB;AACD;;AACD,SAAOF,eAAP;AACD;AAED;AACA;AACA;;;AAOO,SAASG,OAAT,CAAiBC,aAAjB,EAA0D;AAC/D,QAAMC,QAAQ,GAAG,oBAAjB;AACA,QAAMC,iBAAiB,GAAG,oBAA1B;AAEA,MAAIF,aAAa,IAAI,IAArB,EAA2B,OAAOH,kBAAkB,EAAzB;;AAE3B,MACEI,QAAQ,CAACE,OAAT,IAAoB,IAApB,IACA,CAACb,oBAAoB,CAACY,iBAAiB,CAACC,OAAnB,EAA4BH,aAA5B,CAFvB,EAGE;AACAE,IAAAA,iBAA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AN,CAAzC;AAEA,QAAMO,KAAK,GAAG,oBAAQ,MAAM;AAC1B,QAAIqB,MAAM,IAAI,IAAd,EAAoB,OAAOE,SAAP;AACpB,WAAOC,IAAI,CAACC,KAAL,CAAWJ,MAAX,CAAP;AACD,GAHa,EAGX,CAACA,MAAD,CAHW,CAAd;AAIA,QAAMpB,QAAQ,GAAG,wBACdG,CAAD,IAAsB;AACpB,QAAIA,CAAC,IAAI,IAAT,EAAe;AACb;AACAkB,MAAAA,SAAS,CAACC,SAAD,CAAT;AACD,KAHD,MAGO;AACL;AACAD,MAAAA,SAAS,CAACE,IAAI,CAACE,SAAL,CAAetB,CAAf,CAAD,CAAT;AACD;AACF,GATc,EAUf,CAACkB,SAAD,CAVe,CAAjB;AAaA,SAAO,CAACtB,KAAD,EAAQC,QAAR,CAAP;AACD;AAED;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;;AACO,SAAS0B,eAAT,CACLC,oBADK,EAELnC,QAFK,EAGC;AACN,QAAMoC,GAAG,GAAG,mBAAOD,oBAAP,CAAZ;AACAC,EAAAA,GAAG,CAAClC,OAAJ,GAAciC,oBAAd;AAEA,QAAM7B,IAAI,GAAGN,QAAH,aAAGA,QAAH,cAAGA,QAAH,GAAeJ,kBAAkB,EAA3C;AAEA,wBAAU,MAAM;AACd,UAAMmB,QAAQ,GAAGT,IAAI,CAACU,yBAAL,CAAgCC,UAAD,IAAgB;AAC9DmB,MAAAA,GAAG,CAAClC,OAAJ,CAAYe,UAAZ;AACD,KAFgB,CAAjB;AAGA,WAAO,MAAMF,QAAQ,CAACG,MAAT,EAAb;AACD,GALD,EAKG,CAACZ,IAAD,CALH;AAMD","sourcesContent":["import { useRef, useState, useMemo, useCallback, useEffect } from 'react';\nimport { MMKV, MMKVConfiguration } from './MMKV';\n\nfunction isConfigurationEqual(\n left?: MMKVConfiguration,\n right?: MMKVConfiguration\n): boolean {\n if (left == null || right == null) return left == null && right == null;\n\n return (\n left.encryptionKey === right.encryptionKey &&\n left.id === right.id &&\n left.path === right.path\n );\n}\n\nlet defaultInstance: MMKV | null = null;\nfunction getDefaultInstance(): MMKV {\n if (defaultInstance == null) {\n defaultInstance = new MMKV();\n }\n return defaultInstance;\n}\n\n/**\n * Use the default, shared MMKV instance.\n */\nexport function useMMKV(): MMKV;\n/**\n * Use a custom MMKV instance with the given configuration.\n * @param configuration The configuration to initialize the MMKV instance with. Does not have to be memoized.\n */\nexport function useMMKV(configuration: MMKVConfiguration): MMKV;\nexport function useMMKV(configuration?: MMKVConfiguration): MMKV {\n const instance = useRef<MMKV>();\n const lastConfiguration = useRef<MMKVConfiguration>();\n\n if (configuration == null) return getDefaultInstance();\n\n if (\n instance.current == null ||\n !isConfigurationEqual(lastConfiguration.current, configuration)\n ) {\n lastConfiguration.current = configuration;\n instance.current = new MMKV(configuration);\n }\n\n return instance.current;\n}\n\nfunction createMMKVHook<\n T extends (boolean | number | string | Uint8Array) | undefined,\n TSet extends T | undefined,\n TSetAction extends TSet | ((current: T) => TSet)\n>(getter: (instance: MMKV, key: string) => T) {\n return (\n key: string,\n instance?: MMKV\n ): [value: T, setValue: (value: TSetAction) => void] => {\n const mmkv = instance ?? getDefaultInstance();\n const [value, setValue] = useState(() => getter(mmkv, key));\n const valueRef = useRef<T>(value);\n valueRef.current = value;\n\n // update value by user set\n const set = useCallback(\n (v: TSetAction) => {\n const newValue = typeof v === 'function' ? v(valueRef.current) : v;\n switch (typeof newValue) {\n case 'number':\n case 'string':\n case 'boolean':\n mmkv.set(key, newValue);\n break;\n case 'undefined':\n mmkv.delete(key);\n break;\n default:\n throw new Error(`MMKV: Type ${typeof newValue} is not supported!`);\n }\n },\n [key, mmkv]\n );\n\n // update value if key or instance changes\n useEffect(() => {\n setValue(getter(mmkv, key));\n }, [key, mmkv]);\n\n // update value if it changes somewhere else (second hook, same key)\n useEffect(() => {\n const listener = mmkv.addOnValueChangedListener((changedKey) => {\n if (changedKey === key) {\n setValue(getter(mmkv, key));\n }\n });\n return () => listener.remove();\n }, [key, mmkv]);\n\n return [value, set];\n };\n}\n\n/**\n * Use the string value of the given `key` from the given MMKV storage instance.\n *\n * If no instance is provided, a shared default instance will be used.\n *\n * @example\n * ```ts\n * const [username, setUsername] = useMMKVString(\"user.name\")\n * ```\n */\nexport const useMMKVString = createMMKVHook((instance, key) =>\n instance.getString(key)\n);\n\n/**\n * Use the number value of the given `key` from the given MMKV storage instance.\n *\n * If no instance is provided, a shared default instance will be used.\n *\n * @example\n * ```ts\n * const [age, setAge] = useMMKVNumber(\"user.age\")\n * ```\n */\nexport const useMMKVNumber = createMMKVHook((instance, key) =>\n instance.getNumber(key)\n);\n/**\n * Use the boolean value of the given `key` from the given MMKV storage instance.\n *\n * If no instance is provided, a shared default instance will be used.\n *\n * @example\n * ```ts\n * const [isPremiumAccount, setIsPremiumAccount] = useMMKVBoolean(\"user.isPremium\")\n * ```\n */\nexport const useMMKVBoolean = createMMKVHook((instance, key) =>\n instance.getBoolean(key)\n);\n/**\n * Use the buffer value (unsigned 8-bit (0-255)) of the given `key` from the given MMKV storage instance.\n *\n * If no instance is provided, a shared default instance will be used.\n *\n * @example\n * ```ts\n * const [privateKey, setPrivateKey] = useMMKVBuffer(\"user.privateKey\")\n * ```\n */\nexport const useMMKVBuffer = createMMKVHook((instance, key) =>\n instance.getBuffer(key)\n);\n/**\n * Use an object value of the given `key` from the given MMKV storage instance.\n *\n * If no instance is provided, a shared default instance will be used.\n *\n * The object will be serialized using `JSON`.\n *\n * @example\n * ```ts\n * const [user, setUser] = useMMKVObject<User>(\"user\")\n * ```\n */\nexport function useMMKVObject<T>(\n key: string,\n instance?: MMKV\n): [value: T | undefined, setValue: (value: T | undefined) => void] {\n const [string, setString] = useMMKVString(key, instance);\n\n const value = useMemo(() => {\n if (string == null) return undefined;\n return JSON.parse(string) as T;\n }, [string]);\n const setValue = useCallback(\n (v: T | undefined) => {\n if (v == null) {\n // Clear the Value\n setString(undefined);\n } else {\n // Store the Object as a serialized Value\n setString(JSON.stringify(v));\n }\n },\n [setString]\n );\n\n return [value, setValue];\n}\n\n/**\n * Listen for changes in the given MMKV storage instance.\n * If no instance is passed, the default instance will be used.\n * @param valueChangedListener The function to call whenever a value inside the storage instance changes\n * @param instance The instance to listen to changes to (or the default instance)\n *\n * @example\n * ```ts\n * useMMKVListener((key) => {\n * console.log(`Value for \"${key}\" changed!`)\n * })\n * ```\n */\nexport function useMMKVListener(\n valueChangedListener: (key: string) => void,\n instance?: MMKV\n): void {\n const ref = useRef(valueChangedListener);\n ref.current = valueChangedListener;\n\n const mmkv = instance ?? getDefaultInstance();\n\n useEffect(() => {\n const listener = mmkv.addOnValueChangedListener((changedKey) => {\n ref.current(changedKey);\n });\n return () => listener.remove();\n }, [mmkv]);\n}\n"]}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
|
|
7
|
+
var _MMKV = require("./MMKV");
|
|
8
|
+
|
|
9
|
+
Object.keys(_MMKV).forEach(function (key) {
|
|
10
|
+
if (key === "default" || key === "__esModule") return;
|
|
11
|
+
if (key in exports && exports[key] === _MMKV[key]) return;
|
|
12
|
+
Object.defineProperty(exports, key, {
|
|
13
|
+
enumerable: true,
|
|
14
|
+
get: function () {
|
|
15
|
+
return _MMKV[key];
|
|
16
|
+
}
|
|
17
|
+
});
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
var _hooks = require("./hooks");
|
|
21
|
+
|
|
22
|
+
Object.keys(_hooks).forEach(function (key) {
|
|
23
|
+
if (key === "default" || key === "__esModule") return;
|
|
24
|
+
if (key in exports && exports[key] === _hooks[key]) return;
|
|
25
|
+
Object.defineProperty(exports, key, {
|
|
26
|
+
enumerable: true,
|
|
27
|
+
get: function () {
|
|
28
|
+
return _hooks[key];
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
});
|
|
32
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["index.ts"],"names":[],"mappings":";;;;;;AAAA;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;AACA;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA","sourcesContent":["export * from './MMKV';\nexport * from './hooks';\n"]}
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
2
|
+
|
|
3
|
+
import { createMMKV } from './createMMKV';
|
|
4
|
+
import { createMockMMKV } from './createMMKV.mock';
|
|
5
|
+
import { isJest } from './PlatformChecker';
|
|
6
|
+
const onValueChangedListeners = new Map();
|
|
7
|
+
/**
|
|
8
|
+
* A single MMKV instance.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
export class MMKV {
|
|
12
|
+
/**
|
|
13
|
+
* Creates a new MMKV instance with the given Configuration.
|
|
14
|
+
* If no custom `id` is supplied, `'mmkv.default'` will be used.
|
|
15
|
+
*/
|
|
16
|
+
constructor(configuration = {
|
|
17
|
+
id: 'mmkv.default'
|
|
18
|
+
}) {
|
|
19
|
+
_defineProperty(this, "nativeInstance", void 0);
|
|
20
|
+
|
|
21
|
+
_defineProperty(this, "functionCache", void 0);
|
|
22
|
+
|
|
23
|
+
_defineProperty(this, "id", void 0);
|
|
24
|
+
|
|
25
|
+
this.id = configuration.id;
|
|
26
|
+
this.nativeInstance = isJest() ? createMockMMKV() : createMMKV(configuration);
|
|
27
|
+
this.functionCache = {};
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
get onValueChangedListeners() {
|
|
31
|
+
if (!onValueChangedListeners.has(this.id)) {
|
|
32
|
+
onValueChangedListeners.set(this.id, []);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
return onValueChangedListeners.get(this.id);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
getFunctionFromCache(functionName) {
|
|
39
|
+
if (this.functionCache[functionName] == null) {
|
|
40
|
+
this.functionCache[functionName] = this.nativeInstance[functionName];
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return this.functionCache[functionName];
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
onValuesChanged(keys) {
|
|
47
|
+
if (this.onValueChangedListeners.length === 0) return;
|
|
48
|
+
|
|
49
|
+
for (const key of keys) {
|
|
50
|
+
for (const listener of this.onValueChangedListeners) {
|
|
51
|
+
listener(key);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
set(key, value) {
|
|
57
|
+
const func = this.getFunctionFromCache('set');
|
|
58
|
+
func(key, value);
|
|
59
|
+
this.onValuesChanged([key]);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
getBoolean(key) {
|
|
63
|
+
const func = this.getFunctionFromCache('getBoolean');
|
|
64
|
+
return func(key);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
getString(key) {
|
|
68
|
+
const func = this.getFunctionFromCache('getString');
|
|
69
|
+
return func(key);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
getNumber(key) {
|
|
73
|
+
const func = this.getFunctionFromCache('getNumber');
|
|
74
|
+
return func(key);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
getBuffer(key) {
|
|
78
|
+
const func = this.getFunctionFromCache('getBuffer');
|
|
79
|
+
return func(key);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
contains(key) {
|
|
83
|
+
const func = this.getFunctionFromCache('contains');
|
|
84
|
+
return func(key);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
delete(key) {
|
|
88
|
+
const func = this.getFunctionFromCache('delete');
|
|
89
|
+
func(key);
|
|
90
|
+
this.onValuesChanged([key]);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
getAllKeys() {
|
|
94
|
+
const func = this.getFunctionFromCache('getAllKeys');
|
|
95
|
+
return func();
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
clearAll() {
|
|
99
|
+
const keys = this.getAllKeys();
|
|
100
|
+
const func = this.getFunctionFromCache('clearAll');
|
|
101
|
+
func();
|
|
102
|
+
this.onValuesChanged(keys);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
recrypt(key) {
|
|
106
|
+
const func = this.getFunctionFromCache('recrypt');
|
|
107
|
+
return func(key);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
toString() {
|
|
111
|
+
return `MMKV (${this.id}): [${this.getAllKeys().join(', ')}]`;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
toJSON() {
|
|
115
|
+
return {
|
|
116
|
+
[this.id]: this.getAllKeys()
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
addOnValueChangedListener(onValueChanged) {
|
|
121
|
+
this.onValueChangedListeners.push(onValueChanged);
|
|
122
|
+
return {
|
|
123
|
+
remove: () => {
|
|
124
|
+
const index = this.onValueChangedListeners.indexOf(onValueChanged);
|
|
125
|
+
|
|
126
|
+
if (index !== -1) {
|
|
127
|
+
this.onValueChangedListeners.splice(index, 1);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
}
|
|
134
|
+
//# sourceMappingURL=MMKV.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["MMKV.ts"],"names":["createMMKV","createMockMMKV","isJest","onValueChangedListeners","Map","MMKV","constructor","configuration","id","nativeInstance","functionCache","has","set","get","getFunctionFromCache","functionName","onValuesChanged","keys","length","key","listener","value","func","getBoolean","getString","getNumber","getBuffer","contains","delete","getAllKeys","clearAll","recrypt","toString","join","toJSON","addOnValueChangedListener","onValueChanged","push","remove","index","indexOf","splice"],"mappings":";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AAOQ,IAAI,EAAX;AACD;;AACDQ,EAAAA,QAAQ,GAAS;AACf,UAAMb,IAAI,GAAG,KAAKY,UAAL,EAAb;AAEA,UAAMP,IAAI,GAAG,KAAKR,oBAAL,CAA0B,UAA1B,CAAb;AACAQ,IAAAA,IAAI;AAEJ,SAAKN,eAAL,CAAqBC,IAArB;AACD;;AACDc,EAAAA,OAAO,CAACZ,GAAD,EAAgC;AACrC,UAAMG,IAAI,GAAG,KAAKR,oBAAL,CAA0B,SAA1B,CAAb;AACA,WAAOQ,IAAI,CAACH,GAAD,CAAX;AACD;;AAEDa,EAAAA,QAAQ,GAAW;AACjB,WAAQ,SAAQ,KAAKxB,EAAG,OAAM,KAAKqB,UAAL,GAAkBI,IAAlB,CAAuB,IAAvB,CAA6B,GAA3D;AACD;;AACDC,EAAAA,MAAM,GAAW;AACf,WAAO;AACL,OAAC,KAAK1B,EAAN,GAAW,KAAKqB,UAAL;AADN,KAAP;AAGD;;AAEDM,EAAAA,yBAAyB,CAACC,cAAD,EAAkD;AACzE,SAAKjC,uBAAL,CAA6BkC,IAA7B,CAAkCD,cAAlC;AAEA,WAAO;AACLE,MAAAA,MAAM,EAAE,MAAM;AACZ,cAAMC,KAAK,GAAG,KAAKpC,uBAAL,CAA6BqC,OAA7B,CAAqCJ,cAArC,CAAd;;AACA,YAAIG,KAAK,KAAK,CAAC,CAAf,EAAkB;AAChB,eAAKpC,uBAAL,CAA6BsC,MAA7B,CAAoCF,KAApC,EAA2C,CAA3C;AACD;AACF;AANI,KAAP;AAQD;;AAhHwC","sourcesContent":["import { createMMKV } from './createMMKV';\nimport { createMockMMKV } from './createMMKV.mock';\nimport { isJest } from './PlatformChecker';\n\ninterface Listener {\n remove: () => void;\n}\n\n/**\n * Used for configuration of a single MMKV instance.\n */\nexport interface MMKVConfiguration {\n /**\n * The MMKV instance's ID. If you want to use multiple instances, make sure to use different IDs!\n *\n * @example\n * ```ts\n * const userStorage = new MMKV({ id: `user-${userId}-storage` })\n * const globalStorage = new MMKV({ id: 'global-app-storage' })\n * ```\n *\n * @default 'mmkv.default'\n */\n id: string;\n /**\n * The MMKV instance's root path. By default, MMKV stores file inside `$(Documents)/mmkv/`. You can customize MMKV's root directory on MMKV initialization:\n *\n * @example\n * ```ts\n * const temporaryStorage = new MMKV({ path: '/tmp/' })\n * ```\n */\n path?: string;\n /**\n * 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.\n *\n * Encryption keys can have a maximum length of 16 bytes.\n *\n * @example\n * ```ts\n * const secureStorage = new MMKV({ encryptionKey: 'my-encryption-key!' })\n * ```\n */\n encryptionKey?: string;\n}\n\n/**\n * Represents a single MMKV instance.\n */\ninterface MMKVInterface {\n /**\n * Set a value for the given `key`.\n */\n set: (key: string, value: boolean | string | number | Uint8Array) => void;\n /**\n * Get the boolean value for the given `key`, or `undefined` if it does not exist.\n *\n * @default undefined\n */\n getBoolean: (key: string) => boolean | undefined;\n /**\n * Get the string value for the given `key`, or `undefined` if it does not exist.\n *\n * @default undefined\n */\n getString: (key: string) => string | undefined;\n /**\n * Get the number value for the given `key`, or `undefined` if it does not exist.\n *\n * @default undefined\n */\n getNumber: (key: string) => number | undefined;\n /**\n * Get a raw buffer of unsigned 8-bit (0-255) data.\n *\n * @default undefined\n */\n getBuffer: (key: string) => Uint8Array | undefined;\n /**\n * Checks whether the given `key` is being stored in this MMKV instance.\n */\n contains: (key: string) => boolean;\n /**\n * Delete the given `key`.\n */\n delete: (key: string) => void;\n /**\n * Get all keys.\n *\n * @default []\n */\n getAllKeys: () => string[];\n /**\n * Delete all keys.\n */\n clearAll: () => void;\n /**\n * Sets (or updates) the encryption-key to encrypt all data in this MMKV instance with.\n *\n * To remove encryption, pass `undefined` as a key.\n *\n * Encryption keys can have a maximum length of 16 bytes.\n */\n recrypt: (key: string | undefined) => void;\n /**\n * Adds a value changed listener. The Listener will be called whenever any value\n * in this storage instance changes (set or delete).\n *\n * To unsubscribe from value changes, call `remove()` on the Listener.\n */\n addOnValueChangedListener: (\n onValueChanged: (key: string) => void\n ) => Listener;\n}\n\nexport type NativeMMKV = Pick<\n MMKVInterface,\n | 'clearAll'\n | 'contains'\n | 'delete'\n | 'getAllKeys'\n | 'getBoolean'\n | 'getNumber'\n | 'getString'\n | 'getBuffer'\n | 'set'\n | 'recrypt'\n>;\n\nconst onValueChangedListeners = new Map<string, ((key: string) => void)[]>();\n\n/**\n * A single MMKV instance.\n */\nexport class MMKV implements MMKVInterface {\n private nativeInstance: NativeMMKV;\n private functionCache: Partial<NativeMMKV>;\n private id: string;\n\n /**\n * Creates a new MMKV instance with the given Configuration.\n * If no custom `id` is supplied, `'mmkv.default'` will be used.\n */\n constructor(configuration: MMKVConfiguration = { id: 'mmkv.default' }) {\n this.id = configuration.id;\n this.nativeInstance = isJest()\n ? createMockMMKV()\n : createMMKV(configuration);\n this.functionCache = {};\n }\n\n private get onValueChangedListeners() {\n if (!onValueChangedListeners.has(this.id)) {\n onValueChangedListeners.set(this.id, []);\n }\n return onValueChangedListeners.get(this.id)!;\n }\n\n private getFunctionFromCache<T extends keyof NativeMMKV>(\n functionName: T\n ): NativeMMKV[T] {\n if (this.functionCache[functionName] == null) {\n this.functionCache[functionName] = this.nativeInstance[functionName];\n }\n return this.functionCache[functionName] as NativeMMKV[T];\n }\n\n private onValuesChanged(keys: string[]) {\n if (this.onValueChangedListeners.length === 0) return;\n\n for (const key of keys) {\n for (const listener of this.onValueChangedListeners) {\n listener(key);\n }\n }\n }\n\n set(key: string, value: boolean | string | number | Uint8Array): void {\n const func = this.getFunctionFromCache('set');\n func(key, value);\n\n this.onValuesChanged([key]);\n }\n getBoolean(key: string): boolean | undefined {\n const func = this.getFunctionFromCache('getBoolean');\n return func(key);\n }\n getString(key: string): string | undefined {\n const func = this.getFunctionFromCache('getString');\n return func(key);\n }\n getNumber(key: string): number | undefined {\n const func = this.getFunctionFromCache('getNumber');\n return func(key);\n }\n getBuffer(key: string): Uint8Array | undefined {\n const func = this.getFunctionFromCache('getBuffer');\n return func(key);\n }\n contains(key: string): boolean {\n const func = this.getFunctionFromCache('contains');\n return func(key);\n }\n delete(key: string): void {\n const func = this.getFunctionFromCache('delete');\n func(key);\n\n this.onValuesChanged([key]);\n }\n getAllKeys(): string[] {\n const func = this.getFunctionFromCache('getAllKeys');\n return func();\n }\n clearAll(): void {\n const keys = this.getAllKeys();\n\n const func = this.getFunctionFromCache('clearAll');\n func();\n\n this.onValuesChanged(keys);\n }\n recrypt(key: string | undefined): void {\n const func = this.getFunctionFromCache('recrypt');\n return func(key);\n }\n\n toString(): string {\n return `MMKV (${this.id}): [${this.getAllKeys().join(', ')}]`;\n }\n toJSON(): object {\n return {\n [this.id]: this.getAllKeys(),\n };\n }\n\n addOnValueChangedListener(onValueChanged: (key: string) => void): Listener {\n this.onValueChangedListeners.push(onValueChanged);\n\n return {\n remove: () => {\n const index = this.onValueChangedListeners.indexOf(onValueChanged);\n if (index !== -1) {\n this.onValueChangedListeners.splice(index, 1);\n }\n },\n };\n }\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["PlatformChecker.ts"],"names":["isJest","global","process","env","JEST_WORKER_ID"],"mappings":"AAAA,OAAO,SAASA,MAAT,GAA2B;AAChC,MAAIC,MAAM,CAACC,OAAP,IAAkB,IAAtB,EAA4B;AAC1B;AACA,WAAO,KAAP;AACD;;AACD,SAAOA,OAAO,CAACC,GAAR,CAAYC,cAAZ,IAA8B,IAArC;AACD","sourcesContent":["export function isJest(): boolean {\n if (global.process == null) {\n // In a WebBrowser/Electron the `process` variable does not exist\n return false;\n }\n return process.env.JEST_WORKER_ID != null;\n}\n"]}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { NativeModules, Platform } from 'react-native';
|
|
2
|
+
// Root directory of all MMKV stores
|
|
3
|
+
const ROOT_DIRECTORY = null;
|
|
4
|
+
export const createMMKV = config => {
|
|
5
|
+
// Check if the constructor exists. If not, try installing the JSI bindings.
|
|
6
|
+
if (global.mmkvCreateNewInstance == null) {
|
|
7
|
+
// Get the native MMKV ReactModule
|
|
8
|
+
const MMKVModule = NativeModules.MMKV;
|
|
9
|
+
|
|
10
|
+
if (MMKVModule == null) {
|
|
11
|
+
var _NativeModules$Native, _NativeModules$Native2;
|
|
12
|
+
|
|
13
|
+
let message = 'Failed to create a new MMKV instance: The native MMKV Module could not be found.';
|
|
14
|
+
message += '\n* Make sure react-native-mmkv is correctly autolinked (run `npx react-native config` to verify)';
|
|
15
|
+
|
|
16
|
+
if (Platform.OS === 'ios' || Platform.OS === 'macos') {
|
|
17
|
+
message += '\n* Make sure you ran `pod install` in the ios/ directory.';
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
if (Platform.OS === 'android') {
|
|
21
|
+
message += '\n* Make sure gradle is synced.';
|
|
22
|
+
} // check if Expo
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
const ExpoConstants = (_NativeModules$Native = NativeModules.NativeUnimoduleProxy) === null || _NativeModules$Native === void 0 ? void 0 : (_NativeModules$Native2 = _NativeModules$Native.modulesConstants) === null || _NativeModules$Native2 === void 0 ? void 0 : _NativeModules$Native2.ExponentConstants;
|
|
26
|
+
|
|
27
|
+
if (ExpoConstants != null) {
|
|
28
|
+
if (ExpoConstants.appOwnership === 'expo') {
|
|
29
|
+
// We're running Expo Go
|
|
30
|
+
throw new Error('react-native-mmkv is not supported in Expo Go! Use EAS (`expo prebuild`) or eject to a bare workflow instead.');
|
|
31
|
+
} else {
|
|
32
|
+
// We're running Expo bare / standalone
|
|
33
|
+
message += '\n* Make sure you ran `expo prebuild`.';
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
message += '\n* Make sure you rebuilt the app.';
|
|
38
|
+
throw new Error(message);
|
|
39
|
+
} // Check if we are running on-device (JSI)
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
if (global.nativeCallSyncHook == null || MMKVModule.install == null) {
|
|
43
|
+
throw new Error('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.');
|
|
44
|
+
} // Call the synchronous blocking install() function
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
const result = MMKVModule.install(ROOT_DIRECTORY);
|
|
48
|
+
if (result !== true) throw new Error(`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}`); // Check again if the constructor now exists. If not, throw an error.
|
|
49
|
+
|
|
50
|
+
if (global.mmkvCreateNewInstance == null) throw new Error('Failed to create a new MMKV instance, the native initializer function does not exist. Are you trying to use MMKV from different JS Runtimes?');
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
return global.mmkvCreateNewInstance(config);
|
|
54
|
+
};
|
|
55
|
+
//# sourceMappingURL=createMMKV.js.map
|