react-native-mmkv 1.5.4 → 1.6.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/README.md +5 -0
- package/android/CMakeLists.txt +25 -6
- package/android/build.gradle +8 -2
- package/lib/commonjs/MMKV.js +3 -5
- package/lib/commonjs/MMKV.js.map +1 -1
- package/lib/commonjs/createMMKV.js +18 -0
- package/lib/commonjs/createMMKV.js.map +1 -0
- package/lib/commonjs/createMMKV.web.js +67 -0
- package/lib/commonjs/createMMKV.web.js.map +1 -0
- package/lib/module/MMKV.js +2 -5
- package/lib/module/MMKV.js.map +1 -1
- package/lib/module/createMMKV.js +9 -0
- package/lib/module/createMMKV.js.map +1 -0
- package/lib/module/createMMKV.web.js +57 -0
- package/lib/module/createMMKV.web.js.map +1 -0
- package/lib/typescript/MMKV.d.ts +1 -3
- package/lib/typescript/createMMKV.d.ts +5 -0
- package/lib/typescript/createMMKV.web.d.ts +2 -0
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -32,6 +32,7 @@
|
|
|
32
32
|
* **High performance** because everything is **written in C++** (even the JS functions have C++ bodies!)
|
|
33
33
|
* **~30x faster than AsyncStorage**
|
|
34
34
|
* Uses [**JSI**](https://github.com/react-native-community/discussions-and-proposals/issues/91) instead of the "old" Bridge
|
|
35
|
+
* **iOS**, **Android** and **Web** support
|
|
35
36
|
|
|
36
37
|
## Sponsors
|
|
37
38
|
|
|
@@ -72,6 +73,10 @@ cd ios && pod install
|
|
|
72
73
|
|
|
73
74
|
To correctly initialize MMKV on Android, please follow the [Installation guide](./INSTALL.md).
|
|
74
75
|
|
|
76
|
+
### Expo
|
|
77
|
+
|
|
78
|
+
See this comment for more information: [mrousavy/react-native-mmkv#157 (comment)](https://github.com/mrousavy/react-native-mmkv/issues/157#issuecomment-960647481).
|
|
79
|
+
|
|
75
80
|
## Usage
|
|
76
81
|
|
|
77
82
|
### Create a new instance
|
package/android/CMakeLists.txt
CHANGED
|
@@ -12,10 +12,23 @@ include_directories(
|
|
|
12
12
|
"${NODE_MODULES_DIR}/react-native/ReactCommon/jsi"
|
|
13
13
|
)
|
|
14
14
|
|
|
15
|
+
if(${REACT_NATIVE_VERSION} LESS 66)
|
|
16
|
+
set (
|
|
17
|
+
INCLUDE_JSI_CPP
|
|
18
|
+
"${NODE_MODULES_DIR}/react-native/ReactCommon/jsi/jsi/jsi.cpp"
|
|
19
|
+
)
|
|
20
|
+
set (
|
|
21
|
+
INCLUDE_JSIDYNAMIC_CPP
|
|
22
|
+
"${NODE_MODULES_DIR}/react-native/ReactCommon/jsi/jsi/JSIDynamic.cpp"
|
|
23
|
+
)
|
|
24
|
+
endif()
|
|
25
|
+
|
|
15
26
|
add_library(reactnativemmkv # <-- Library name
|
|
16
27
|
SHARED
|
|
17
28
|
src/main/cpp/cpp-adapter.cpp
|
|
18
29
|
src/main/cpp/MmkvHostObject.cpp
|
|
30
|
+
${INCLUDE_JSI_CPP} # only on older RN versions
|
|
31
|
+
${INCLUDE_JSIDYNAMIC_CPP} # only on older RN versions
|
|
19
32
|
)
|
|
20
33
|
|
|
21
34
|
set_target_properties(
|
|
@@ -31,18 +44,24 @@ find_library(
|
|
|
31
44
|
log-lib
|
|
32
45
|
log
|
|
33
46
|
)
|
|
34
|
-
find_library(
|
|
35
|
-
JSI_LIB
|
|
36
|
-
jsi
|
|
37
|
-
PATHS ${LIBRN_DIR}
|
|
38
|
-
NO_CMAKE_FIND_ROOT_PATH
|
|
39
|
-
)
|
|
40
47
|
find_library(
|
|
41
48
|
REACT_NATIVE_JNI_LIB
|
|
42
49
|
reactnativejni
|
|
43
50
|
PATHS ${LIBRN_DIR}
|
|
44
51
|
NO_CMAKE_FIND_ROOT_PATH
|
|
45
52
|
)
|
|
53
|
+
if(${REACT_NATIVE_VERSION} LESS 66)
|
|
54
|
+
# JSI lib didn't exist on RN 0.65 and before. Simply omit it.
|
|
55
|
+
set (JSI_LIB "")
|
|
56
|
+
else()
|
|
57
|
+
# RN 0.66 distributes libjsi.so, can be used instead of compiling jsi.cpp manually.
|
|
58
|
+
find_library(
|
|
59
|
+
JSI_LIB
|
|
60
|
+
jsi
|
|
61
|
+
PATHS ${LIBRN_DIR}
|
|
62
|
+
NO_CMAKE_FIND_ROOT_PATH
|
|
63
|
+
)
|
|
64
|
+
endif()
|
|
46
65
|
|
|
47
66
|
target_link_libraries(
|
|
48
67
|
reactnativemmkv
|
package/android/build.gradle
CHANGED
|
@@ -72,6 +72,11 @@ if (defaultDir.exists()) {
|
|
|
72
72
|
})
|
|
73
73
|
}
|
|
74
74
|
|
|
75
|
+
|
|
76
|
+
def reactProperties = new Properties()
|
|
77
|
+
file("$projectDir/../node_modules/react-native/ReactAndroid/gradle.properties").withInputStream { reactProperties.load(it) }
|
|
78
|
+
def REACT_NATIVE_VERSION = reactProperties.getProperty("VERSION_NAME").split("\\.")[1].toInteger()
|
|
79
|
+
|
|
75
80
|
android {
|
|
76
81
|
compileSdkVersion getExtOrIntegerDefault('compileSdkVersion')
|
|
77
82
|
buildToolsVersion getExtOrDefault('buildToolsVersion')
|
|
@@ -87,7 +92,8 @@ android {
|
|
|
87
92
|
cppFlags "-fexceptions", "-frtti", "-std=c++1y", "-DONANDROID"
|
|
88
93
|
abiFilters 'x86', 'x86_64', 'armeabi-v7a', 'arm64-v8a'
|
|
89
94
|
arguments '-DANDROID_STL=c++_shared',
|
|
90
|
-
|
|
95
|
+
"-DREACT_NATIVE_VERSION=${REACT_NATIVE_VERSION}",
|
|
96
|
+
"-DNODE_MODULES_DIR=${defaultDir.parentFile.parentFile.toString()}"
|
|
91
97
|
}
|
|
92
98
|
}
|
|
93
99
|
}
|
|
@@ -347,7 +353,7 @@ task extractJNIFiles {
|
|
|
347
353
|
extractJNIFiles.mustRunAfter extractAARHeaders
|
|
348
354
|
|
|
349
355
|
tasks.whenTaskAdded { task ->
|
|
350
|
-
if (task.name.contains('externalNative') || task.name.contains('CMake')) {
|
|
356
|
+
if (!task.name.contains('Clean') && (task.name.contains('externalNative') || task.name.contains('CMake'))) {
|
|
351
357
|
task.dependsOn(extractAARHeaders)
|
|
352
358
|
task.dependsOn(extractJNIFiles)
|
|
353
359
|
task.dependsOn(prepareThirdPartyNdkHeaders)
|
package/lib/commonjs/MMKV.js
CHANGED
|
@@ -7,6 +7,8 @@ exports.MMKV = void 0;
|
|
|
7
7
|
|
|
8
8
|
var _reactNative = require("react-native");
|
|
9
9
|
|
|
10
|
+
var _createMMKV = require("./createMMKV");
|
|
11
|
+
|
|
10
12
|
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; }
|
|
11
13
|
|
|
12
14
|
const onValueChangedListeners = new Map();
|
|
@@ -28,12 +30,8 @@ class MMKV {
|
|
|
28
30
|
|
|
29
31
|
_defineProperty(this, "id", void 0);
|
|
30
32
|
|
|
31
|
-
if (global.mmkvCreateNewInstance == null) {
|
|
32
|
-
throw new Error('Failed to create a new MMKV instance, the native initializer function does not exist. Is the native MMKV library correctly installed? Make sure to disable any remote debugger (e.g. Chrome) to use JSI!');
|
|
33
|
-
}
|
|
34
|
-
|
|
35
33
|
this.id = configuration.id;
|
|
36
|
-
this.nativeInstance =
|
|
34
|
+
this.nativeInstance = (0, _createMMKV.createMMKV)(configuration);
|
|
37
35
|
this.functionCache = {};
|
|
38
36
|
}
|
|
39
37
|
|
package/lib/commonjs/MMKV.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["MMKV.ts"],"names":["onValueChangedListeners","Map","MMKV","constructor","configuration","id","
|
|
1
|
+
{"version":3,"sources":["MMKV.ts"],"names":["onValueChangedListeners","Map","MMKV","constructor","configuration","id","nativeInstance","functionCache","has","set","get","getFunctionFromCache","functionName","onValuesAboutToChange","keys","length","setImmediate","key","listener","value","func","getBoolean","getString","getNumber","contains","delete","getAllKeys","clearAll","addOnValueChangedListener","onValueChanged","push","remove","index","indexOf","splice"],"mappings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A1B,CAAb;AACA,WAAOS,IAAI,EAAX;AACD;;AAEDQ,EAAAA,yBAAyB,CAACC,cAAD,EAAkD;AACzE,SAAK7B,uBAAL,CAA6B8B,IAA7B,CAAkCD,cAAlC;AAEA,WAAO;AACLE,MAAAA,MAAM,EAAE,MAAM;AACZ,cAAMC,KAAK,GAAG,KAAKhC,uBAAL,CAA6BiC,OAA7B,CAAqCJ,cAArC,CAAd;;AACA,YAAIG,KAAK,KAAK,CAAC,CAAf,EAAkB;AAChB,eAAKhC,uBAAL,CAA6BkC,MAA7B,CAAoCF,KAApC,EAA2C,CAA3C;AACD;AACF;AANI,KAAP;AAQD;;AAhGwC","sourcesContent":["import { unstable_batchedUpdates } from 'react-native';\nimport { createMMKV } from './createMMKV';\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 * @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) => void;\n /**\n * Get a boolean value for the given `key`.\n *\n * @default false\n */\n getBoolean: (key: string) => boolean;\n /**\n * Get a string value for the given `key`.\n *\n * @default undefined\n */\n getString: (key: string) => string | undefined;\n /**\n * Get a number value for the given `key`.\n *\n * @default 0\n */\n getNumber: (key: string) => number;\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 * Adds a value changed 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 | 'set'\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, `'default'` will be used.\n */\n constructor(configuration: MMKVConfiguration = { id: 'mmkv.default' }) {\n this.id = configuration.id;\n this.nativeInstance = 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 onValuesAboutToChange(keys: string[]) {\n if (this.onValueChangedListeners.length === 0) return;\n\n setImmediate(() => {\n unstable_batchedUpdates(() => {\n for (const key of keys) {\n for (const listener of this.onValueChangedListeners) {\n listener(key);\n }\n }\n });\n });\n }\n\n set(key: string, value: boolean | string | number): void {\n this.onValuesAboutToChange([key]);\n\n const func = this.getFunctionFromCache('set');\n return func(key, value);\n }\n getBoolean(key: string): boolean {\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 {\n const func = this.getFunctionFromCache('getNumber');\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 this.onValuesAboutToChange([key]);\n\n const func = this.getFunctionFromCache('delete');\n return func(key);\n }\n getAllKeys(): string[] {\n const func = this.getFunctionFromCache('getAllKeys');\n return func();\n }\n clearAll(): void {\n const keys = this.getAllKeys();\n this.onValuesAboutToChange(keys);\n\n const func = this.getFunctionFromCache('clearAll');\n return func();\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,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.createMMKV = void 0;
|
|
7
|
+
|
|
8
|
+
// global func declaration for JSI functions
|
|
9
|
+
const createMMKV = config => {
|
|
10
|
+
if (global.mmkvCreateNewInstance == null) {
|
|
11
|
+
throw new Error('Failed to create a new MMKV instance, the native initializer function does not exist. Is the native MMKV library correctly installed? Make sure to disable any remote debugger (e.g. Chrome) to use JSI!');
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
return global.mmkvCreateNewInstance(config);
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
exports.createMMKV = createMMKV;
|
|
18
|
+
//# sourceMappingURL=createMMKV.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["createMMKV.ts"],"names":["createMMKV","config","global","mmkvCreateNewInstance","Error"],"mappings":";;;;;;;AAEA;AAKO,MAAMA,UAAU,GAAIC,MAAD,IAA2C;AACnE,MAAIC,MAAM,CAACC,qBAAP,IAAgC,IAApC,EAA0C;AACxC,UAAM,IAAIC,KAAJ,CACJ,0MADI,CAAN;AAGD;;AAED,SAAOF,MAAM,CAACC,qBAAP,CAA6BF,MAA7B,CAAP;AACD,CARM","sourcesContent":["import type { MMKVConfiguration, NativeMMKV } from 'react-native-mmkv';\n\n// global func declaration for JSI functions\ndeclare global {\n function mmkvCreateNewInstance(configuration: MMKVConfiguration): NativeMMKV;\n}\n\nexport const createMMKV = (config: MMKVConfiguration): NativeMMKV => {\n if (global.mmkvCreateNewInstance == null) {\n throw new Error(\n 'Failed to create a new MMKV instance, the native initializer function does not exist. Is the native MMKV library correctly installed? Make sure to disable any remote debugger (e.g. Chrome) to use JSI!'\n );\n }\n\n return global.mmkvCreateNewInstance(config);\n};\n"]}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.createMMKV = void 0;
|
|
7
|
+
|
|
8
|
+
var _window$document;
|
|
9
|
+
|
|
10
|
+
/* global localStorage */
|
|
11
|
+
const canUseDOM = typeof window !== 'undefined' && ((_window$document = window.document) === null || _window$document === void 0 ? void 0 : _window$document.createElement) != null;
|
|
12
|
+
|
|
13
|
+
const createMMKV = config => {
|
|
14
|
+
if (config.id !== 'mmkv.default') {
|
|
15
|
+
throw new Error("MMKV: 'id' is not supported on Web!");
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
if (config.encryptionKey != null) {
|
|
19
|
+
throw new Error("MMKV: 'encryptionKey' is not supported on Web!");
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
if (config.path != null) {
|
|
23
|
+
throw new Error("MMKV: 'path' is not supported on Web!");
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const storage = () => {
|
|
27
|
+
var _ref, _global$localStorage, _global, _window;
|
|
28
|
+
|
|
29
|
+
if (!canUseDOM) {
|
|
30
|
+
throw new Error('Tried to access storage on the server. Did you forget to call this in useEffect?');
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
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;
|
|
34
|
+
|
|
35
|
+
if (domStorage == null) {
|
|
36
|
+
throw new Error(`Could not find 'localStorage' instance!`);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
return domStorage;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
return {
|
|
43
|
+
clearAll: () => storage().clear(),
|
|
44
|
+
delete: key => storage().removeItem(key),
|
|
45
|
+
set: (key, value) => storage().setItem(key, value.toString()),
|
|
46
|
+
getString: key => {
|
|
47
|
+
var _storage$getItem;
|
|
48
|
+
|
|
49
|
+
return (_storage$getItem = storage().getItem(key)) !== null && _storage$getItem !== void 0 ? _storage$getItem : undefined;
|
|
50
|
+
},
|
|
51
|
+
getNumber: key => {
|
|
52
|
+
var _storage$getItem2;
|
|
53
|
+
|
|
54
|
+
return Number((_storage$getItem2 = storage().getItem(key)) !== null && _storage$getItem2 !== void 0 ? _storage$getItem2 : 0);
|
|
55
|
+
},
|
|
56
|
+
getBoolean: key => {
|
|
57
|
+
var _storage$getItem3;
|
|
58
|
+
|
|
59
|
+
return Boolean((_storage$getItem3 = storage().getItem(key)) !== null && _storage$getItem3 !== void 0 ? _storage$getItem3 : false);
|
|
60
|
+
},
|
|
61
|
+
getAllKeys: () => Object.keys(storage()),
|
|
62
|
+
contains: key => storage().getItem(key) != null
|
|
63
|
+
};
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
exports.createMMKV = createMMKV;
|
|
67
|
+
//# 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","clearAll","clear","delete","key","removeItem","set","value","setItem","toString","getString","getItem","undefined","getNumber","Number","getBoolean","Boolean","getAllKeys","Object","keys","contains"],"mappings":";;;;;;;;;AAAA;AAGA,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,SAAO;AACLG,IAAAA,QAAQ,EAAE,MAAMJ,OAAO,GAAGK,KAAV,EADX;AAELC,IAAAA,MAAM,EAAGC,GAAD,IAASP,OAAO,GAAGQ,UAAV,CAAqBD,GAArB,CAFZ;AAGLE,IAAAA,GAAG,EAAE,CAACF,GAAD,EAAMG,KAAN,KAAgBV,OAAO,GAAGW,OAAV,CAAkBJ,GAAlB,EAAuBG,KAAK,CAACE,QAAN,EAAvB,CAHhB;AAILC,IAAAA,SAAS,EAAGN,GAAD;AAAA;;AAAA,iCAASP,OAAO,GAAGc,OAAV,CAAkBP,GAAlB,CAAT,+DAAmCQ,SAAnC;AAAA,KAJN;AAKLC,IAAAA,SAAS,EAAGT,GAAD;AAAA;;AAAA,aAASU,MAAM,sBAACjB,OAAO,GAAGc,OAAV,CAAkBP,GAAlB,CAAD,iEAA2B,CAA3B,CAAf;AAAA,KALN;AAMLW,IAAAA,UAAU,EAAGX,GAAD;AAAA;;AAAA,aAASY,OAAO,sBAACnB,OAAO,GAAGc,OAAV,CAAkBP,GAAlB,CAAD,iEAA2B,KAA3B,CAAhB;AAAA,KANP;AAOLa,IAAAA,UAAU,EAAE,MAAMC,MAAM,CAACC,IAAP,CAAYtB,OAAO,EAAnB,CAPb;AAQLuB,IAAAA,QAAQ,EAAGhB,GAAD,IAASP,OAAO,GAAGc,OAAV,CAAkBP,GAAlB,KAA0B;AARxC,GAAP;AAUD,CAnCM","sourcesContent":["/* global localStorage */\nimport type { MMKVConfiguration, NativeMMKV } from 'react-native-mmkv';\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 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) => Number(storage().getItem(key) ?? 0),\n getBoolean: (key) => Boolean(storage().getItem(key) ?? false),\n getAllKeys: () => Object.keys(storage()),\n contains: (key) => storage().getItem(key) != null,\n };\n};\n"]}
|
package/lib/module/MMKV.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
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
2
|
|
|
3
3
|
import { unstable_batchedUpdates } from 'react-native';
|
|
4
|
+
import { createMMKV } from './createMMKV';
|
|
4
5
|
const onValueChangedListeners = new Map();
|
|
5
6
|
/**
|
|
6
7
|
* A single MMKV instance.
|
|
@@ -20,12 +21,8 @@ export class MMKV {
|
|
|
20
21
|
|
|
21
22
|
_defineProperty(this, "id", void 0);
|
|
22
23
|
|
|
23
|
-
if (global.mmkvCreateNewInstance == null) {
|
|
24
|
-
throw new Error('Failed to create a new MMKV instance, the native initializer function does not exist. Is the native MMKV library correctly installed? Make sure to disable any remote debugger (e.g. Chrome) to use JSI!');
|
|
25
|
-
}
|
|
26
|
-
|
|
27
24
|
this.id = configuration.id;
|
|
28
|
-
this.nativeInstance =
|
|
25
|
+
this.nativeInstance = createMMKV(configuration);
|
|
29
26
|
this.functionCache = {};
|
|
30
27
|
}
|
|
31
28
|
|
package/lib/module/MMKV.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["MMKV.ts"],"names":["unstable_batchedUpdates","onValueChangedListeners","Map","MMKV","constructor","configuration","id","
|
|
1
|
+
{"version":3,"sources":["MMKV.ts"],"names":["unstable_batchedUpdates","createMMKV","onValueChangedListeners","Map","MMKV","constructor","configuration","id","nativeInstance","functionCache","has","set","get","getFunctionFromCache","functionName","onValuesAboutToChange","keys","length","setImmediate","key","listener","value","func","getBoolean","getString","getNumber","contains","delete","getAllKeys","clearAll","addOnValueChangedListener","onValueChanged","push","remove","index","indexOf","splice"],"mappings":";;AAAA,SAASA,uBAAT,QAAwC,cAAxC;AACA,SAASC,UAAT,QAA2B,cAA3B;AA0GA,MAAMC,uBAAuB,GAAG,IAAIC,GAAJ,EAAhC;AAEA;AACA;AACA;;AACA,OAAO,MAAMC,IAAN,CAAoC;AAKzC;AACF;AACA;AACA;AACEC,EAAAA,WAAW,CAACC,aAAgC,GAAG;AAAEC,IAAAA,EAAE,EAAE;AAAN,GAApC,EAA4D;AAAA;;AAAA;;AAAA;;AACrE,SAAKA,EAAL,GAAUD,aAAa,CAACC,EAAxB;AACA,SAAKC,cAAL,GAAsBP,UAAU,CAACK,aAAD,CAAhC;AACA,SAAKG,aAAL,GAAqB,EAArB;AACD;;AAEkC,MAAvBP,uBAAuB,GAAG;AACpC,QAAI,CAACA,uBAAuB,CAACQ,GAAxB,CAA4B,KAAKH,EAAjC,CAAL,EAA2C;AACzCL,MAAAA,uBAAuB,CAACS,GAAxB,CAA4B,KAAKJ,EAAjC,EAAqC,EAArC;AACD;;AACD,WAAOL,uBAAuB,CAACU,GAAxB,CAA4B,KAAKL,EAAjC,CAAP;AACD;;AAEOM,EAAAA,oBAAoB,CAC1BC,YAD0B,EAEX;AACf,QAAI,KAAKL,aAAL,CAAmBK,YAAnB,KAAoC,IAAxC,EAA8C;AAC5C,WAAKL,aAAL,CAAmBK,YAAnB,IAAmC,KAAKN,cAAL,CAAoBM,YAApB,CAAnC;AACD;;AACD,WAAO,KAAKL,aAAL,CAAmBK,YAAnB,CAAP;AACD;;AAEOC,EAAAA,qBAAqB,CAACC,IAAD,EAAiB;AAC5C,QAAI,KAAKd,uBAAL,CAA6Be,MAA7B,KAAwC,CAA5C,EAA+C;AAE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X;AACD;;AACDS,EAAAA,UAAU,GAAa;AACrB,UAAMN,IAAI,GAAG,KAAKT,oBAAL,CAA0B,YAA1B,CAAb;AACA,WAAOS,IAAI,EAAX;AACD;;AACDO,EAAAA,QAAQ,GAAS;AACf,UAAMb,IAAI,GAAG,KAAKY,UAAL,EAAb;AACA,SAAKb,qBAAL,CAA2BC,IAA3B;AAEA,UAAMM,IAAI,GAAG,KAAKT,oBAAL,CAA0B,UAA1B,CAAb;AACA,WAAOS,IAAI,EAAX;AACD;;AAEDQ,EAAAA,yBAAyB,CAACC,cAAD,EAAkD;AACzE,SAAK7B,uBAAL,CAA6B8B,IAA7B,CAAkCD,cAAlC;AAEA,WAAO;AACLE,MAAAA,MAAM,EAAE,MAAM;AACZ,cAAMC,KAAK,GAAG,KAAKhC,uBAAL,CAA6BiC,OAA7B,CAAqCJ,cAArC,CAAd;;AACA,YAAIG,KAAK,KAAK,CAAC,CAAf,EAAkB;AAChB,eAAKhC,uBAAL,CAA6BkC,MAA7B,CAAoCF,KAApC,EAA2C,CAA3C;AACD;AACF;AANI,KAAP;AAQD;;AAhGwC","sourcesContent":["import { unstable_batchedUpdates } from 'react-native';\nimport { createMMKV } from './createMMKV';\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 * @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) => void;\n /**\n * Get a boolean value for the given `key`.\n *\n * @default false\n */\n getBoolean: (key: string) => boolean;\n /**\n * Get a string value for the given `key`.\n *\n * @default undefined\n */\n getString: (key: string) => string | undefined;\n /**\n * Get a number value for the given `key`.\n *\n * @default 0\n */\n getNumber: (key: string) => number;\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 * Adds a value changed 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 | 'set'\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, `'default'` will be used.\n */\n constructor(configuration: MMKVConfiguration = { id: 'mmkv.default' }) {\n this.id = configuration.id;\n this.nativeInstance = 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 onValuesAboutToChange(keys: string[]) {\n if (this.onValueChangedListeners.length === 0) return;\n\n setImmediate(() => {\n unstable_batchedUpdates(() => {\n for (const key of keys) {\n for (const listener of this.onValueChangedListeners) {\n listener(key);\n }\n }\n });\n });\n }\n\n set(key: string, value: boolean | string | number): void {\n this.onValuesAboutToChange([key]);\n\n const func = this.getFunctionFromCache('set');\n return func(key, value);\n }\n getBoolean(key: string): boolean {\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 {\n const func = this.getFunctionFromCache('getNumber');\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 this.onValuesAboutToChange([key]);\n\n const func = this.getFunctionFromCache('delete');\n return func(key);\n }\n getAllKeys(): string[] {\n const func = this.getFunctionFromCache('getAllKeys');\n return func();\n }\n clearAll(): void {\n const keys = this.getAllKeys();\n this.onValuesAboutToChange(keys);\n\n const func = this.getFunctionFromCache('clearAll');\n return func();\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,9 @@
|
|
|
1
|
+
// global func declaration for JSI functions
|
|
2
|
+
export const createMMKV = config => {
|
|
3
|
+
if (global.mmkvCreateNewInstance == null) {
|
|
4
|
+
throw new Error('Failed to create a new MMKV instance, the native initializer function does not exist. Is the native MMKV library correctly installed? Make sure to disable any remote debugger (e.g. Chrome) to use JSI!');
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
return global.mmkvCreateNewInstance(config);
|
|
8
|
+
};
|
|
9
|
+
//# sourceMappingURL=createMMKV.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["createMMKV.ts"],"names":["createMMKV","config","global","mmkvCreateNewInstance","Error"],"mappings":"AAEA;AAKA,OAAO,MAAMA,UAAU,GAAIC,MAAD,IAA2C;AACnE,MAAIC,MAAM,CAACC,qBAAP,IAAgC,IAApC,EAA0C;AACxC,UAAM,IAAIC,KAAJ,CACJ,0MADI,CAAN;AAGD;;AAED,SAAOF,MAAM,CAACC,qBAAP,CAA6BF,MAA7B,CAAP;AACD,CARM","sourcesContent":["import type { MMKVConfiguration, NativeMMKV } from 'react-native-mmkv';\n\n// global func declaration for JSI functions\ndeclare global {\n function mmkvCreateNewInstance(configuration: MMKVConfiguration): NativeMMKV;\n}\n\nexport const createMMKV = (config: MMKVConfiguration): NativeMMKV => {\n if (global.mmkvCreateNewInstance == null) {\n throw new Error(\n 'Failed to create a new MMKV instance, the native initializer function does not exist. Is the native MMKV library correctly installed? Make sure to disable any remote debugger (e.g. Chrome) to use JSI!'\n );\n }\n\n return global.mmkvCreateNewInstance(config);\n};\n"]}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
var _window$document;
|
|
2
|
+
|
|
3
|
+
/* global localStorage */
|
|
4
|
+
const canUseDOM = typeof window !== 'undefined' && ((_window$document = window.document) === null || _window$document === void 0 ? void 0 : _window$document.createElement) != null;
|
|
5
|
+
export const createMMKV = config => {
|
|
6
|
+
if (config.id !== 'mmkv.default') {
|
|
7
|
+
throw new Error("MMKV: 'id' is not supported on Web!");
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
if (config.encryptionKey != null) {
|
|
11
|
+
throw new Error("MMKV: 'encryptionKey' is not supported on Web!");
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
if (config.path != null) {
|
|
15
|
+
throw new Error("MMKV: 'path' is not supported on Web!");
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const storage = () => {
|
|
19
|
+
var _ref, _global$localStorage, _global, _window;
|
|
20
|
+
|
|
21
|
+
if (!canUseDOM) {
|
|
22
|
+
throw new Error('Tried to access storage on the server. Did you forget to call this in useEffect?');
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
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;
|
|
26
|
+
|
|
27
|
+
if (domStorage == null) {
|
|
28
|
+
throw new Error(`Could not find 'localStorage' instance!`);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
return domStorage;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
return {
|
|
35
|
+
clearAll: () => storage().clear(),
|
|
36
|
+
delete: key => storage().removeItem(key),
|
|
37
|
+
set: (key, value) => storage().setItem(key, value.toString()),
|
|
38
|
+
getString: key => {
|
|
39
|
+
var _storage$getItem;
|
|
40
|
+
|
|
41
|
+
return (_storage$getItem = storage().getItem(key)) !== null && _storage$getItem !== void 0 ? _storage$getItem : undefined;
|
|
42
|
+
},
|
|
43
|
+
getNumber: key => {
|
|
44
|
+
var _storage$getItem2;
|
|
45
|
+
|
|
46
|
+
return Number((_storage$getItem2 = storage().getItem(key)) !== null && _storage$getItem2 !== void 0 ? _storage$getItem2 : 0);
|
|
47
|
+
},
|
|
48
|
+
getBoolean: key => {
|
|
49
|
+
var _storage$getItem3;
|
|
50
|
+
|
|
51
|
+
return Boolean((_storage$getItem3 = storage().getItem(key)) !== null && _storage$getItem3 !== void 0 ? _storage$getItem3 : false);
|
|
52
|
+
},
|
|
53
|
+
getAllKeys: () => Object.keys(storage()),
|
|
54
|
+
contains: key => storage().getItem(key) != null
|
|
55
|
+
};
|
|
56
|
+
};
|
|
57
|
+
//# 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","clearAll","clear","delete","key","removeItem","set","value","setItem","toString","getString","getItem","undefined","getNumber","Number","getBoolean","Boolean","getAllKeys","Object","keys","contains"],"mappings":";;AAAA;AAGA,MAAMA,SAAS,GACb,OAAOC,MAAP,KAAkB,WAAlB,IAAiC,qBAAAA,MAAM,CAACC,QAAP,sEAAiBC,aAAjB,KAAkC,IADrE;AAGA,OAAO,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,SAAO;AACLG,IAAAA,QAAQ,EAAE,MAAMJ,OAAO,GAAGK,KAAV,EADX;AAELC,IAAAA,MAAM,EAAGC,GAAD,IAASP,OAAO,GAAGQ,UAAV,CAAqBD,GAArB,CAFZ;AAGLE,IAAAA,GAAG,EAAE,CAACF,GAAD,EAAMG,KAAN,KAAgBV,OAAO,GAAGW,OAAV,CAAkBJ,GAAlB,EAAuBG,KAAK,CAACE,QAAN,EAAvB,CAHhB;AAILC,IAAAA,SAAS,EAAGN,GAAD;AAAA;;AAAA,iCAASP,OAAO,GAAGc,OAAV,CAAkBP,GAAlB,CAAT,+DAAmCQ,SAAnC;AAAA,KAJN;AAKLC,IAAAA,SAAS,EAAGT,GAAD;AAAA;;AAAA,aAASU,MAAM,sBAACjB,OAAO,GAAGc,OAAV,CAAkBP,GAAlB,CAAD,iEAA2B,CAA3B,CAAf;AAAA,KALN;AAMLW,IAAAA,UAAU,EAAGX,GAAD;AAAA;;AAAA,aAASY,OAAO,sBAACnB,OAAO,GAAGc,OAAV,CAAkBP,GAAlB,CAAD,iEAA2B,KAA3B,CAAhB;AAAA,KANP;AAOLa,IAAAA,UAAU,EAAE,MAAMC,MAAM,CAACC,IAAP,CAAYtB,OAAO,EAAnB,CAPb;AAQLuB,IAAAA,QAAQ,EAAGhB,GAAD,IAASP,OAAO,GAAGc,OAAV,CAAkBP,GAAlB,KAA0B;AARxC,GAAP;AAUD,CAnCM","sourcesContent":["/* global localStorage */\nimport type { MMKVConfiguration, NativeMMKV } from 'react-native-mmkv';\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 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) => Number(storage().getItem(key) ?? 0),\n getBoolean: (key) => Boolean(storage().getItem(key) ?? false),\n getAllKeys: () => Object.keys(storage()),\n contains: (key) => storage().getItem(key) != null,\n };\n};\n"]}
|
package/lib/typescript/MMKV.d.ts
CHANGED
|
@@ -85,9 +85,7 @@ interface MMKVInterface {
|
|
|
85
85
|
*/
|
|
86
86
|
addOnValueChangedListener: (onValueChanged: (key: string) => void) => Listener;
|
|
87
87
|
}
|
|
88
|
-
declare
|
|
89
|
-
function mmkvCreateNewInstance(configuration: MMKVConfiguration): MMKVInterface;
|
|
90
|
-
}
|
|
88
|
+
export declare type NativeMMKV = Pick<MMKVInterface, 'clearAll' | 'contains' | 'delete' | 'getAllKeys' | 'getBoolean' | 'getNumber' | 'getString' | 'set'>;
|
|
91
89
|
/**
|
|
92
90
|
* A single MMKV instance.
|
|
93
91
|
*/
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-native-mmkv",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.6.0",
|
|
4
4
|
"description": "An efficient, small mobile key-value storage framework developed by WeChat. Works on Android and iOS.",
|
|
5
5
|
"main": "lib/commonjs/index",
|
|
6
6
|
"module": "lib/module/index",
|
|
@@ -28,7 +28,7 @@
|
|
|
28
28
|
"typescript": "tsc --noEmit",
|
|
29
29
|
"lint": "eslint \"**/*.{js,ts,tsx}\"",
|
|
30
30
|
"lint-ci": "yarn lint -f ./node_modules/@firmnav/eslint-github-actions-formatter/dist/formatter.js",
|
|
31
|
-
"
|
|
31
|
+
"build": "git submodule update --init --recursive && bob build",
|
|
32
32
|
"update-submodule": "git submodule update --remote --merge",
|
|
33
33
|
"release": "release-it",
|
|
34
34
|
"example": "yarn --cwd example",
|