react-native-mmkv 4.0.0-beta.1 → 4.0.0-beta.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/{react-native-mmkv.podspec → NitroMmkv.podspec} +17 -13
- package/README.md +1 -3
- package/android/CMakeLists.txt +31 -30
- package/android/build.gradle +64 -19
- package/android/fix-prefab.gradle +51 -0
- package/android/gradle.properties +5 -5
- package/android/src/main/cpp/cpp-adapter.cpp +6 -0
- package/android/src/main/java/com/margelo/nitro/mmkv/HybridMMKVPlatformContext.kt +19 -0
- package/android/src/main/java/com/margelo/nitro/mmkv/NitroMmkvPackage.java +33 -0
- package/ios/HybridMMKVPlatformContext.swift +32 -0
- package/lib/__tests__/hooks.test.d.ts +1 -0
- package/lib/__tests__/hooks.test.js +66 -0
- package/lib/addMemoryWarningListener/addMemoryWarningListener.d.ts +2 -0
- package/lib/addMemoryWarningListener/addMemoryWarningListener.js +25 -0
- package/lib/addMemoryWarningListener/addMemoryWarningListener.mock.d.ts +2 -0
- package/lib/addMemoryWarningListener/addMemoryWarningListener.mock.js +3 -0
- package/lib/addMemoryWarningListener/addMemoryWarningListener.web.d.ts +2 -0
- package/lib/addMemoryWarningListener/addMemoryWarningListener.web.js +3 -0
- package/lib/createMMKV/createMMKV.d.ts +3 -0
- package/lib/createMMKV/createMMKV.js +40 -0
- package/lib/createMMKV/createMMKV.mock.d.ts +5 -0
- package/lib/createMMKV/createMMKV.mock.js +53 -0
- package/lib/createMMKV/createMMKV.web.d.ts +3 -0
- package/lib/createMMKV/createMMKV.web.js +117 -0
- package/lib/createMMKV/getDefaultMMKVInstance.d.ts +2 -0
- package/lib/createMMKV/getDefaultMMKVInstance.js +8 -0
- package/lib/hooks/createMMKVHook.d.ts +2 -0
- package/lib/hooks/createMMKVHook.js +49 -0
- package/lib/hooks/useMMKV.d.ts +11 -0
- package/lib/hooks/useMMKV.js +23 -0
- package/lib/hooks/useMMKVBoolean.d.ts +11 -0
- package/lib/hooks/useMMKVBoolean.js +12 -0
- package/lib/hooks/useMMKVBuffer.d.ts +11 -0
- package/lib/hooks/useMMKVBuffer.js +12 -0
- package/lib/hooks/useMMKVKeys.d.ts +12 -0
- package/lib/hooks/useMMKVKeys.js +33 -0
- package/lib/hooks/useMMKVListener.d.ts +15 -0
- package/lib/hooks/useMMKVListener.js +26 -0
- package/lib/hooks/useMMKVNumber.d.ts +11 -0
- package/lib/hooks/useMMKVNumber.js +12 -0
- package/lib/hooks/useMMKVObject.d.ts +17 -0
- package/lib/hooks/useMMKVObject.js +38 -0
- package/lib/hooks/useMMKVString.d.ts +11 -0
- package/lib/hooks/useMMKVString.js +12 -0
- package/lib/index.d.ts +11 -0
- package/lib/index.js +11 -0
- package/lib/isTest.d.ts +1 -0
- package/lib/isTest.js +7 -0
- package/lib/specs/MMKV.nitro.d.ts +93 -0
- package/lib/specs/MMKV.nitro.js +1 -0
- package/lib/{typescript/src/NativeMmkv.d.ts → specs/MMKVFactory.nitro.d.ts} +22 -29
- package/lib/specs/MMKVFactory.nitro.js +1 -0
- package/lib/specs/MMKVPlatformContext.nitro.d.ts +15 -0
- package/lib/specs/MMKVPlatformContext.nitro.js +1 -0
- package/lib/{typescript/src → web}/createTextEncoder.d.ts +0 -1
- package/lib/web/createTextEncoder.js +17 -0
- package/nitro.json +27 -0
- package/nitrogen/generated/.gitattributes +1 -0
- package/nitrogen/generated/android/NitroMmkv+autolinking.cmake +80 -0
- package/nitrogen/generated/android/NitroMmkv+autolinking.gradle +27 -0
- package/nitrogen/generated/android/NitroMmkvOnLoad.cpp +55 -0
- package/nitrogen/generated/android/NitroMmkvOnLoad.hpp +25 -0
- package/nitrogen/generated/android/c++/JHybridMMKVPlatformContextSpec.cpp +51 -0
- package/nitrogen/generated/android/c++/JHybridMMKVPlatformContextSpec.hpp +65 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/mmkv/HybridMMKVPlatformContextSpec.kt +56 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/mmkv/NitroMmkvOnLoad.kt +35 -0
- package/nitrogen/generated/ios/NitroMmkv+autolinking.rb +60 -0
- package/nitrogen/generated/ios/NitroMmkv-Swift-Cxx-Bridge.cpp +32 -0
- package/nitrogen/generated/ios/NitroMmkv-Swift-Cxx-Bridge.hpp +52 -0
- package/nitrogen/generated/ios/NitroMmkv-Swift-Cxx-Umbrella.hpp +44 -0
- package/nitrogen/generated/ios/NitroMmkvAutolinking.mm +43 -0
- package/nitrogen/generated/ios/NitroMmkvAutolinking.swift +25 -0
- package/nitrogen/generated/ios/c++/HybridMMKVPlatformContextSpecSwift.cpp +11 -0
- package/nitrogen/generated/ios/c++/HybridMMKVPlatformContextSpecSwift.hpp +81 -0
- package/nitrogen/generated/ios/swift/HybridMMKVPlatformContextSpec.swift +50 -0
- package/nitrogen/generated/ios/swift/HybridMMKVPlatformContextSpec_cxx.swift +135 -0
- package/nitrogen/generated/shared/c++/Configuration.hpp +86 -0
- package/nitrogen/generated/shared/c++/HybridMMKVFactorySpec.cpp +23 -0
- package/nitrogen/generated/shared/c++/HybridMMKVFactorySpec.hpp +69 -0
- package/nitrogen/generated/shared/c++/HybridMMKVPlatformContextSpec.cpp +22 -0
- package/nitrogen/generated/shared/c++/HybridMMKVPlatformContextSpec.hpp +63 -0
- package/nitrogen/generated/shared/c++/HybridMMKVSpec.cpp +34 -0
- package/nitrogen/generated/shared/c++/HybridMMKVSpec.hpp +83 -0
- package/nitrogen/generated/shared/c++/Listener.hpp +67 -0
- package/nitrogen/generated/shared/c++/Mode.hpp +76 -0
- package/package.json +71 -122
- package/react-native.config.js +2 -15
- package/src/__tests__/hooks.test.tsx +34 -34
- package/src/addMemoryWarningListener/addMemoryWarningListener.mock.ts +5 -0
- package/src/{MemoryWarningListener.ts → addMemoryWarningListener/addMemoryWarningListener.ts} +12 -12
- package/src/addMemoryWarningListener/addMemoryWarningListener.web.ts +5 -0
- package/src/createMMKV/createMMKV.mock.ts +56 -0
- package/src/createMMKV/createMMKV.ts +51 -0
- package/src/{createMMKV.web.ts → createMMKV/createMMKV.web.ts} +56 -46
- package/src/createMMKV/getDefaultMMKVInstance.ts +10 -0
- package/src/hooks/createMMKVHook.ts +66 -0
- package/src/hooks/useMMKV.ts +45 -0
- package/src/hooks/useMMKVBoolean.ts +15 -0
- package/src/hooks/useMMKVBuffer.ts +15 -0
- package/src/hooks/useMMKVKeys.ts +36 -0
- package/src/hooks/useMMKVListener.ts +33 -0
- package/src/hooks/useMMKVNumber.ts +15 -0
- package/src/hooks/useMMKVObject.ts +53 -0
- package/src/hooks/useMMKVString.ts +15 -0
- package/src/index.ts +15 -3
- package/src/{PlatformChecker.ts → isTest.ts} +2 -2
- package/src/specs/MMKV.nitro.ts +92 -0
- package/src/specs/MMKVFactory.nitro.ts +87 -0
- package/src/specs/MMKVPlatformContext.nitro.ts +14 -0
- package/src/{createTextEncoder.ts → web/createTextEncoder.ts} +7 -7
- package/android/src/main/cpp/AndroidLogger.cpp +0 -16
- package/android/src/main/java/com/mrousavy/mmkv/MmkvPackage.java +0 -44
- package/android/src/main/java/com/mrousavy/mmkv/MmkvPlatformContextModule.java +0 -26
- package/cpp/ManagedMMBuffer.h +0 -32
- package/cpp/MmkvHostObject.cpp +0 -360
- package/cpp/MmkvHostObject.h +0 -31
- package/cpp/MmkvLogger.h +0 -35
- package/cpp/MmkvTypes.h +0 -50
- package/cpp/NativeMmkvModule.cpp +0 -43
- package/cpp/NativeMmkvModule.h +0 -31
- package/ios/AppleLogger.mm +0 -16
- package/ios/MmkvOnLoad.mm +0 -25
- package/ios/MmkvPlatformContext.h +0 -19
- package/ios/MmkvPlatformContextModule.mm +0 -55
- package/lib/commonjs/MMKV.js +0 -124
- package/lib/commonjs/MMKV.js.map +0 -1
- package/lib/commonjs/MemoryWarningListener.js +0 -31
- package/lib/commonjs/MemoryWarningListener.js.map +0 -1
- package/lib/commonjs/MemoryWarningListener.web.js +0 -11
- package/lib/commonjs/MemoryWarningListener.web.js.map +0 -1
- package/lib/commonjs/ModuleNotFoundError.js +0 -75
- package/lib/commonjs/ModuleNotFoundError.js.map +0 -1
- package/lib/commonjs/NativeMmkv.js +0 -47
- package/lib/commonjs/NativeMmkv.js.map +0 -1
- package/lib/commonjs/NativeMmkvPlatformContext.js +0 -22
- package/lib/commonjs/NativeMmkvPlatformContext.js.map +0 -1
- package/lib/commonjs/PlatformChecker.js +0 -14
- package/lib/commonjs/PlatformChecker.js.map +0 -1
- package/lib/commonjs/Types.js +0 -26
- package/lib/commonjs/Types.js.map +0 -1
- package/lib/commonjs/createMMKV.js +0 -43
- package/lib/commonjs/createMMKV.js.map +0 -1
- package/lib/commonjs/createMMKV.mock.js +0 -43
- package/lib/commonjs/createMMKV.mock.js.map +0 -1
- package/lib/commonjs/createMMKV.web.js +0 -110
- package/lib/commonjs/createMMKV.web.js.map +0 -1
- package/lib/commonjs/createTextEncoder.js +0 -23
- package/lib/commonjs/createTextEncoder.js.map +0 -1
- package/lib/commonjs/hooks.js +0 -198
- package/lib/commonjs/hooks.js.map +0 -1
- package/lib/commonjs/index.js +0 -40
- package/lib/commonjs/index.js.map +0 -1
- package/lib/commonjs/package.json +0 -1
- package/lib/module/MMKV.js +0 -119
- package/lib/module/MMKV.js.map +0 -1
- package/lib/module/MemoryWarningListener.js +0 -27
- package/lib/module/MemoryWarningListener.js.map +0 -1
- package/lib/module/MemoryWarningListener.web.js +0 -6
- package/lib/module/MemoryWarningListener.web.js.map +0 -1
- package/lib/module/ModuleNotFoundError.js +0 -70
- package/lib/module/ModuleNotFoundError.js.map +0 -1
- package/lib/module/NativeMmkv.js +0 -45
- package/lib/module/NativeMmkv.js.map +0 -1
- package/lib/module/NativeMmkvPlatformContext.js +0 -18
- package/lib/module/NativeMmkvPlatformContext.js.map +0 -1
- package/lib/module/PlatformChecker.js +0 -10
- package/lib/module/PlatformChecker.js.map +0 -1
- package/lib/module/Types.js +0 -25
- package/lib/module/Types.js.map +0 -1
- package/lib/module/createMMKV.js +0 -38
- package/lib/module/createMMKV.js.map +0 -1
- package/lib/module/createMMKV.mock.js +0 -38
- package/lib/module/createMMKV.mock.js.map +0 -1
- package/lib/module/createMMKV.web.js +0 -105
- package/lib/module/createMMKV.web.js.map +0 -1
- package/lib/module/createTextEncoder.js +0 -19
- package/lib/module/createTextEncoder.js.map +0 -1
- package/lib/module/hooks.js +0 -189
- package/lib/module/hooks.js.map +0 -1
- package/lib/module/index.js +0 -6
- package/lib/module/index.js.map +0 -1
- package/lib/module/package.json +0 -1
- package/lib/typescript/src/MMKV.d.ts +0 -34
- package/lib/typescript/src/MMKV.d.ts.map +0 -1
- package/lib/typescript/src/MemoryWarningListener.d.ts +0 -3
- package/lib/typescript/src/MemoryWarningListener.d.ts.map +0 -1
- package/lib/typescript/src/MemoryWarningListener.web.d.ts +0 -3
- package/lib/typescript/src/MemoryWarningListener.web.d.ts.map +0 -1
- package/lib/typescript/src/ModuleNotFoundError.d.ts +0 -7
- package/lib/typescript/src/ModuleNotFoundError.d.ts.map +0 -1
- package/lib/typescript/src/NativeMmkv.d.ts.map +0 -1
- package/lib/typescript/src/NativeMmkvPlatformContext.d.ts +0 -20
- package/lib/typescript/src/NativeMmkvPlatformContext.d.ts.map +0 -1
- package/lib/typescript/src/PlatformChecker.d.ts +0 -2
- package/lib/typescript/src/PlatformChecker.d.ts.map +0 -1
- package/lib/typescript/src/Types.d.ts +0 -172
- package/lib/typescript/src/Types.d.ts.map +0 -1
- package/lib/typescript/src/__tests__/hooks.test.d.ts +0 -2
- package/lib/typescript/src/__tests__/hooks.test.d.ts.map +0 -1
- package/lib/typescript/src/createMMKV.d.ts +0 -3
- package/lib/typescript/src/createMMKV.d.ts.map +0 -1
- package/lib/typescript/src/createMMKV.mock.d.ts +0 -3
- package/lib/typescript/src/createMMKV.mock.d.ts.map +0 -1
- package/lib/typescript/src/createMMKV.web.d.ts +0 -3
- package/lib/typescript/src/createMMKV.web.d.ts.map +0 -1
- package/lib/typescript/src/createTextEncoder.d.ts.map +0 -1
- package/lib/typescript/src/hooks.d.ts +0 -86
- package/lib/typescript/src/hooks.d.ts.map +0 -1
- package/lib/typescript/src/index.d.ts +0 -4
- package/lib/typescript/src/index.d.ts.map +0 -1
- package/src/MMKV.ts +0 -142
- package/src/MemoryWarningListener.web.ts +0 -5
- package/src/ModuleNotFoundError.ts +0 -95
- package/src/NativeMmkv.ts +0 -118
- package/src/NativeMmkvPlatformContext.ts +0 -38
- package/src/Types.ts +0 -178
- package/src/createMMKV.mock.ts +0 -38
- package/src/createMMKV.ts +0 -42
- package/src/hooks.ts +0 -247
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { createTextEncoder } from '../web/createTextEncoder';
|
|
2
|
+
const canUseDOM = typeof window !== 'undefined' && window.document?.createElement != null;
|
|
3
|
+
const hasAccessToLocalStorage = () => {
|
|
4
|
+
try {
|
|
5
|
+
// throws ACCESS_DENIED error
|
|
6
|
+
window.localStorage;
|
|
7
|
+
return true;
|
|
8
|
+
}
|
|
9
|
+
catch {
|
|
10
|
+
return false;
|
|
11
|
+
}
|
|
12
|
+
};
|
|
13
|
+
const KEY_WILDCARD = '\\';
|
|
14
|
+
const inMemoryStorage = new Map();
|
|
15
|
+
export function createMMKV(config) {
|
|
16
|
+
if (config.encryptionKey != null) {
|
|
17
|
+
throw new Error("MMKV: 'encryptionKey' is not supported on Web!");
|
|
18
|
+
}
|
|
19
|
+
if (config.path != null) {
|
|
20
|
+
throw new Error("MMKV: 'path' is not supported on Web!");
|
|
21
|
+
}
|
|
22
|
+
// canUseDOM check prevents spam in Node server environments, such as Next.js server side props.
|
|
23
|
+
if (!hasAccessToLocalStorage() && canUseDOM) {
|
|
24
|
+
console.warn('MMKV: LocalStorage has been disabled. Your experience will be limited to in-memory storage!');
|
|
25
|
+
}
|
|
26
|
+
const storage = () => {
|
|
27
|
+
if (!canUseDOM) {
|
|
28
|
+
throw new Error('Tried to access storage on the server. Did you forget to call this in useEffect?');
|
|
29
|
+
}
|
|
30
|
+
if (!hasAccessToLocalStorage()) {
|
|
31
|
+
return {
|
|
32
|
+
getItem: (key) => inMemoryStorage.get(key) ?? null,
|
|
33
|
+
setItem: (key, value) => inMemoryStorage.set(key, value),
|
|
34
|
+
removeItem: (key) => inMemoryStorage.delete(key),
|
|
35
|
+
clear: () => inMemoryStorage.clear(),
|
|
36
|
+
length: inMemoryStorage.size,
|
|
37
|
+
key: (index) => Object.keys(inMemoryStorage).at(index) ?? null,
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
const domStorage = global?.localStorage ?? window?.localStorage ?? localStorage;
|
|
41
|
+
if (domStorage == null) {
|
|
42
|
+
throw new Error(`Could not find 'localStorage' instance!`);
|
|
43
|
+
}
|
|
44
|
+
return domStorage;
|
|
45
|
+
};
|
|
46
|
+
const textEncoder = createTextEncoder();
|
|
47
|
+
const listeners = new Set();
|
|
48
|
+
if (config.id.includes(KEY_WILDCARD)) {
|
|
49
|
+
throw new Error('MMKV: `id` cannot contain the backslash character (`\\`)!');
|
|
50
|
+
}
|
|
51
|
+
const keyPrefix = `${config.id}${KEY_WILDCARD}`; // mmkv.default\\
|
|
52
|
+
const prefixedKey = (key) => {
|
|
53
|
+
if (key.includes('\\')) {
|
|
54
|
+
throw new Error('MMKV: `key` cannot contain the backslash character (`\\`)!');
|
|
55
|
+
}
|
|
56
|
+
return `${keyPrefix}${key}`;
|
|
57
|
+
};
|
|
58
|
+
return {
|
|
59
|
+
clearAll: () => {
|
|
60
|
+
const keys = Object.keys(storage());
|
|
61
|
+
for (const key of keys) {
|
|
62
|
+
if (key.startsWith(keyPrefix)) {
|
|
63
|
+
storage().removeItem(key);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
},
|
|
67
|
+
remove: (key) => storage().removeItem(prefixedKey(key)),
|
|
68
|
+
set: (key, value) => {
|
|
69
|
+
storage().setItem(prefixedKey(key), value.toString());
|
|
70
|
+
},
|
|
71
|
+
getString: (key) => storage().getItem(prefixedKey(key)) ?? undefined,
|
|
72
|
+
getNumber: (key) => {
|
|
73
|
+
const value = storage().getItem(prefixedKey(key));
|
|
74
|
+
if (value == null)
|
|
75
|
+
return undefined;
|
|
76
|
+
return Number(value);
|
|
77
|
+
},
|
|
78
|
+
getBoolean: (key) => {
|
|
79
|
+
const value = storage().getItem(prefixedKey(key));
|
|
80
|
+
if (value == null)
|
|
81
|
+
return undefined;
|
|
82
|
+
return value === 'true';
|
|
83
|
+
},
|
|
84
|
+
getBuffer: (key) => {
|
|
85
|
+
const value = storage().getItem(prefixedKey(key));
|
|
86
|
+
if (value == null)
|
|
87
|
+
return undefined;
|
|
88
|
+
return textEncoder.encode(value).buffer;
|
|
89
|
+
},
|
|
90
|
+
getAllKeys: () => {
|
|
91
|
+
const keys = Object.keys(storage());
|
|
92
|
+
return keys
|
|
93
|
+
.filter((key) => key.startsWith(keyPrefix))
|
|
94
|
+
.map((key) => key.slice(keyPrefix.length));
|
|
95
|
+
},
|
|
96
|
+
contains: (key) => storage().getItem(prefixedKey(key)) != null,
|
|
97
|
+
recrypt: () => {
|
|
98
|
+
throw new Error('`recrypt(..)` is not supported on Web!');
|
|
99
|
+
},
|
|
100
|
+
size: 0,
|
|
101
|
+
isReadOnly: false,
|
|
102
|
+
trim: () => {
|
|
103
|
+
// no-op
|
|
104
|
+
},
|
|
105
|
+
dispose: () => { },
|
|
106
|
+
equals: () => false,
|
|
107
|
+
name: 'MMKV',
|
|
108
|
+
addOnValueChangedListener: (listener) => {
|
|
109
|
+
listeners.add(listener);
|
|
110
|
+
return {
|
|
111
|
+
remove: () => {
|
|
112
|
+
listeners.delete(listener);
|
|
113
|
+
},
|
|
114
|
+
};
|
|
115
|
+
},
|
|
116
|
+
};
|
|
117
|
+
}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import type { MMKV } from '../specs/MMKV.nitro';
|
|
2
|
+
export declare function createMMKVHook<T extends (boolean | number | string | ArrayBufferLike) | undefined, TSet extends T | undefined, TSetAction extends TSet | ((current: T) => TSet)>(getter: (instance: MMKV, key: string) => T): (key: string, instance?: MMKV) => [value: T, setValue: (value: TSetAction) => void];
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { useCallback, useEffect, useMemo, useState } from 'react';
|
|
2
|
+
import { getDefaultMMKVInstance } from '../createMMKV/getDefaultMMKVInstance';
|
|
3
|
+
export function createMMKVHook(getter) {
|
|
4
|
+
return (key, instance) => {
|
|
5
|
+
const mmkv = instance ?? getDefaultMMKVInstance();
|
|
6
|
+
const [bump, setBump] = useState(0);
|
|
7
|
+
const value = useMemo(() => {
|
|
8
|
+
// bump is here as an additional outside dependency, so this useMemo
|
|
9
|
+
// re-computes the value each time bump changes, effectively acting as a hint
|
|
10
|
+
// that the outside value (storage) has changed. setting bump refreshes this value.
|
|
11
|
+
bump;
|
|
12
|
+
return getter(mmkv, key);
|
|
13
|
+
}, [mmkv, key, bump]);
|
|
14
|
+
// update value by user set
|
|
15
|
+
const set = useCallback((v) => {
|
|
16
|
+
const newValue = typeof v === 'function' ? v(getter(mmkv, key)) : v;
|
|
17
|
+
switch (typeof newValue) {
|
|
18
|
+
case 'number':
|
|
19
|
+
case 'string':
|
|
20
|
+
case 'boolean':
|
|
21
|
+
mmkv.set(key, newValue);
|
|
22
|
+
break;
|
|
23
|
+
case 'undefined':
|
|
24
|
+
mmkv.remove(key);
|
|
25
|
+
break;
|
|
26
|
+
case 'object':
|
|
27
|
+
if (newValue instanceof ArrayBuffer) {
|
|
28
|
+
mmkv.set(key, newValue);
|
|
29
|
+
break;
|
|
30
|
+
}
|
|
31
|
+
else {
|
|
32
|
+
throw new Error(`MMKV: Type object (${newValue}) is not supported!`);
|
|
33
|
+
}
|
|
34
|
+
default:
|
|
35
|
+
throw new Error(`MMKV: Type ${typeof newValue} is not supported!`);
|
|
36
|
+
}
|
|
37
|
+
}, [key, mmkv]);
|
|
38
|
+
// update value if it changes somewhere else (second hook, same key)
|
|
39
|
+
useEffect(() => {
|
|
40
|
+
const listener = mmkv.addOnValueChangedListener((changedKey) => {
|
|
41
|
+
if (changedKey === key) {
|
|
42
|
+
setBump((b) => b + 1);
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
return () => listener.remove();
|
|
46
|
+
}, [key, mmkv]);
|
|
47
|
+
return [value, set];
|
|
48
|
+
};
|
|
49
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { MMKV } from '../specs/MMKV.nitro';
|
|
2
|
+
import type { Configuration } from '../specs/MMKVFactory.nitro';
|
|
3
|
+
/**
|
|
4
|
+
* Use the default, shared MMKV instance.
|
|
5
|
+
*/
|
|
6
|
+
export declare function useMMKV(): MMKV;
|
|
7
|
+
/**
|
|
8
|
+
* Use a custom MMKV instance with the given configuration.
|
|
9
|
+
* @param configuration The configuration to initialize the MMKV instance with. Does not have to be memoized.
|
|
10
|
+
*/
|
|
11
|
+
export declare function useMMKV(configuration: Configuration): MMKV;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { useRef } from 'react';
|
|
2
|
+
import { getDefaultMMKVInstance } from '../createMMKV/getDefaultMMKVInstance';
|
|
3
|
+
import { createMMKV } from '../createMMKV/createMMKV';
|
|
4
|
+
function isConfigurationEqual(left, right) {
|
|
5
|
+
if (left == null || right == null)
|
|
6
|
+
return left == null && right == null;
|
|
7
|
+
return (left.encryptionKey === right.encryptionKey &&
|
|
8
|
+
left.id === right.id &&
|
|
9
|
+
left.path === right.path &&
|
|
10
|
+
left.mode === right.mode);
|
|
11
|
+
}
|
|
12
|
+
export function useMMKV(configuration) {
|
|
13
|
+
const instance = useRef(undefined);
|
|
14
|
+
const lastConfiguration = useRef(undefined);
|
|
15
|
+
if (configuration == null)
|
|
16
|
+
return getDefaultMMKVInstance();
|
|
17
|
+
if (instance.current == null ||
|
|
18
|
+
!isConfigurationEqual(lastConfiguration.current, configuration)) {
|
|
19
|
+
lastConfiguration.current = configuration;
|
|
20
|
+
instance.current = createMMKV(configuration);
|
|
21
|
+
}
|
|
22
|
+
return instance.current;
|
|
23
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Use the boolean value of the given `key` from the given MMKV storage instance.
|
|
3
|
+
*
|
|
4
|
+
* If no instance is provided, a shared default instance will be used.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```ts
|
|
8
|
+
* const [isPremiumAccount, setIsPremiumAccount] = useMMKVBoolean("user.isPremium")
|
|
9
|
+
* ```
|
|
10
|
+
*/
|
|
11
|
+
export declare const useMMKVBoolean: (key: string, instance?: import("..").MMKV) => [value: boolean | undefined, setValue: (value: boolean | ((current: boolean | undefined) => boolean | undefined) | undefined) => void];
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { createMMKVHook } from './createMMKVHook';
|
|
2
|
+
/**
|
|
3
|
+
* Use the boolean value of the given `key` from the given MMKV storage instance.
|
|
4
|
+
*
|
|
5
|
+
* If no instance is provided, a shared default instance will be used.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```ts
|
|
9
|
+
* const [isPremiumAccount, setIsPremiumAccount] = useMMKVBoolean("user.isPremium")
|
|
10
|
+
* ```
|
|
11
|
+
*/
|
|
12
|
+
export const useMMKVBoolean = createMMKVHook((instance, key) => instance.getBoolean(key));
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Use the buffer value (unsigned 8-bit (0-255)) of the given `key` from the given MMKV storage instance.
|
|
3
|
+
*
|
|
4
|
+
* If no instance is provided, a shared default instance will be used.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```ts
|
|
8
|
+
* const [privateKey, setPrivateKey] = useMMKVBuffer("user.privateKey")
|
|
9
|
+
* ```
|
|
10
|
+
*/
|
|
11
|
+
export declare const useMMKVBuffer: (key: string, instance?: import("..").MMKV) => [value: ArrayBuffer | undefined, setValue: (value: ArrayBuffer | ((current: ArrayBuffer | undefined) => ArrayBuffer | undefined) | undefined) => void];
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { createMMKVHook } from './createMMKVHook';
|
|
2
|
+
/**
|
|
3
|
+
* Use the buffer value (unsigned 8-bit (0-255)) of the given `key` from the given MMKV storage instance.
|
|
4
|
+
*
|
|
5
|
+
* If no instance is provided, a shared default instance will be used.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```ts
|
|
9
|
+
* const [privateKey, setPrivateKey] = useMMKVBuffer("user.privateKey")
|
|
10
|
+
* ```
|
|
11
|
+
*/
|
|
12
|
+
export const useMMKVBuffer = createMMKVHook((instance, key) => instance.getBuffer(key));
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { MMKV } from '../specs/MMKV.nitro';
|
|
2
|
+
/**
|
|
3
|
+
* Get a list of all keys that exist in the given MMKV {@linkcode instance}.
|
|
4
|
+
* The keys update when new keys are added or removed.
|
|
5
|
+
* @param instance The instance to listen to changes to (or the default instance)
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```ts
|
|
9
|
+
* useMMKVKeys(instance)
|
|
10
|
+
* ```
|
|
11
|
+
*/
|
|
12
|
+
export declare function useMMKVKeys(instance?: MMKV): string[];
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { useState } from 'react';
|
|
2
|
+
import { getDefaultMMKVInstance } from '../createMMKV/getDefaultMMKVInstance';
|
|
3
|
+
import { useMMKVListener } from './useMMKVListener';
|
|
4
|
+
/**
|
|
5
|
+
* Get a list of all keys that exist in the given MMKV {@linkcode instance}.
|
|
6
|
+
* The keys update when new keys are added or removed.
|
|
7
|
+
* @param instance The instance to listen to changes to (or the default instance)
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```ts
|
|
11
|
+
* useMMKVKeys(instance)
|
|
12
|
+
* ```
|
|
13
|
+
*/
|
|
14
|
+
export function useMMKVKeys(instance) {
|
|
15
|
+
const mmkv = instance ?? getDefaultMMKVInstance();
|
|
16
|
+
const [allKeys, setKeys] = useState(() => mmkv.getAllKeys());
|
|
17
|
+
useMMKVListener((key) => {
|
|
18
|
+
// a key changed
|
|
19
|
+
setKeys((keys) => {
|
|
20
|
+
const currentlyHasKey = keys.includes(key);
|
|
21
|
+
const hasKey = mmkv.contains(key);
|
|
22
|
+
if (hasKey !== currentlyHasKey) {
|
|
23
|
+
// Re-fetch the keys from native
|
|
24
|
+
return mmkv.getAllKeys();
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
// We are up-to-date.
|
|
28
|
+
return keys;
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
}, mmkv);
|
|
32
|
+
return allKeys;
|
|
33
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { MMKV } from '../specs/MMKV.nitro';
|
|
2
|
+
/**
|
|
3
|
+
* Listen for changes in the given MMKV storage instance.
|
|
4
|
+
* If no instance is passed, the default instance will be used.
|
|
5
|
+
* @param valueChangedListener The function to call whenever a value inside the storage instance changes
|
|
6
|
+
* @param instance The instance to listen to changes to (or the default instance)
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```ts
|
|
10
|
+
* useMMKVListener((key) => {
|
|
11
|
+
* console.log(`Value for "${key}" changed!`)
|
|
12
|
+
* })
|
|
13
|
+
* ```
|
|
14
|
+
*/
|
|
15
|
+
export declare function useMMKVListener(valueChangedListener: (key: string) => void, instance?: MMKV): void;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { useEffect, useRef } from 'react';
|
|
2
|
+
import { getDefaultMMKVInstance } from '../createMMKV/getDefaultMMKVInstance';
|
|
3
|
+
/**
|
|
4
|
+
* Listen for changes in the given MMKV storage instance.
|
|
5
|
+
* If no instance is passed, the default instance will be used.
|
|
6
|
+
* @param valueChangedListener The function to call whenever a value inside the storage instance changes
|
|
7
|
+
* @param instance The instance to listen to changes to (or the default instance)
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```ts
|
|
11
|
+
* useMMKVListener((key) => {
|
|
12
|
+
* console.log(`Value for "${key}" changed!`)
|
|
13
|
+
* })
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
16
|
+
export function useMMKVListener(valueChangedListener, instance) {
|
|
17
|
+
const ref = useRef(valueChangedListener);
|
|
18
|
+
ref.current = valueChangedListener;
|
|
19
|
+
const mmkv = instance ?? getDefaultMMKVInstance();
|
|
20
|
+
useEffect(() => {
|
|
21
|
+
const listener = mmkv.addOnValueChangedListener((changedKey) => {
|
|
22
|
+
ref.current(changedKey);
|
|
23
|
+
});
|
|
24
|
+
return () => listener.remove();
|
|
25
|
+
}, [mmkv]);
|
|
26
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Use the number value of the given `key` from the given MMKV storage instance.
|
|
3
|
+
*
|
|
4
|
+
* If no instance is provided, a shared default instance will be used.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```ts
|
|
8
|
+
* const [age, setAge] = useMMKVNumber("user.age")
|
|
9
|
+
* ```
|
|
10
|
+
*/
|
|
11
|
+
export declare const useMMKVNumber: (key: string, instance?: import("..").MMKV) => [value: number | undefined, setValue: (value: number | ((current: number | undefined) => number | undefined) | undefined) => void];
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { createMMKVHook } from './createMMKVHook';
|
|
2
|
+
/**
|
|
3
|
+
* Use the number value of the given `key` from the given MMKV storage instance.
|
|
4
|
+
*
|
|
5
|
+
* If no instance is provided, a shared default instance will be used.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```ts
|
|
9
|
+
* const [age, setAge] = useMMKVNumber("user.age")
|
|
10
|
+
* ```
|
|
11
|
+
*/
|
|
12
|
+
export const useMMKVNumber = createMMKVHook((instance, key) => instance.getNumber(key));
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { MMKV } from '../specs/MMKV.nitro';
|
|
2
|
+
/**
|
|
3
|
+
* Use an object value of the given `key` from the given MMKV storage instance.
|
|
4
|
+
*
|
|
5
|
+
* If no instance is provided, a shared default instance will be used.
|
|
6
|
+
*
|
|
7
|
+
* The object will be serialized using `JSON`.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```ts
|
|
11
|
+
* const [user, setUser] = useMMKVObject<User>("user")
|
|
12
|
+
* ```
|
|
13
|
+
*/
|
|
14
|
+
export declare function useMMKVObject<T>(key: string, instance?: MMKV): [
|
|
15
|
+
value: T | undefined,
|
|
16
|
+
setValue: (value: T | undefined | ((prevValue: T | undefined) => T | undefined)) => void
|
|
17
|
+
];
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { useCallback, useMemo } from 'react';
|
|
2
|
+
import { useMMKVString } from './useMMKVString';
|
|
3
|
+
/**
|
|
4
|
+
* Use an object value of the given `key` from the given MMKV storage instance.
|
|
5
|
+
*
|
|
6
|
+
* If no instance is provided, a shared default instance will be used.
|
|
7
|
+
*
|
|
8
|
+
* The object will be serialized using `JSON`.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```ts
|
|
12
|
+
* const [user, setUser] = useMMKVObject<User>("user")
|
|
13
|
+
* ```
|
|
14
|
+
*/
|
|
15
|
+
export function useMMKVObject(key, instance) {
|
|
16
|
+
const [json, setJson] = useMMKVString(key, instance);
|
|
17
|
+
const value = useMemo(() => {
|
|
18
|
+
if (json == null)
|
|
19
|
+
return undefined;
|
|
20
|
+
return JSON.parse(json);
|
|
21
|
+
}, [json]);
|
|
22
|
+
const setValue = useCallback((v) => {
|
|
23
|
+
if (v instanceof Function) {
|
|
24
|
+
setJson((currentJson) => {
|
|
25
|
+
const currentValue = currentJson != null ? JSON.parse(currentJson) : undefined;
|
|
26
|
+
const newValue = v(currentValue);
|
|
27
|
+
// Store the Object as a serialized Value or clear the value
|
|
28
|
+
return newValue != null ? JSON.stringify(newValue) : undefined;
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
else {
|
|
32
|
+
// Store the Object as a serialized Value or clear the value
|
|
33
|
+
const newValue = v != null ? JSON.stringify(v) : undefined;
|
|
34
|
+
setJson(newValue);
|
|
35
|
+
}
|
|
36
|
+
}, [setJson]);
|
|
37
|
+
return [value, setValue];
|
|
38
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Use the string value of the given `key` from the given MMKV storage instance.
|
|
3
|
+
*
|
|
4
|
+
* If no instance is provided, a shared default instance will be used.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```ts
|
|
8
|
+
* const [username, setUsername] = useMMKVString("user.name")
|
|
9
|
+
* ```
|
|
10
|
+
*/
|
|
11
|
+
export declare const useMMKVString: (key: string, instance?: import("..").MMKV) => [value: string | undefined, setValue: (value: string | ((current: string | undefined) => string | undefined) | undefined) => void];
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { createMMKVHook } from './createMMKVHook';
|
|
2
|
+
/**
|
|
3
|
+
* Use the string value of the given `key` from the given MMKV storage instance.
|
|
4
|
+
*
|
|
5
|
+
* If no instance is provided, a shared default instance will be used.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```ts
|
|
9
|
+
* const [username, setUsername] = useMMKVString("user.name")
|
|
10
|
+
* ```
|
|
11
|
+
*/
|
|
12
|
+
export const useMMKVString = createMMKVHook((instance, key) => instance.getString(key));
|
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export type { MMKV } from './specs/MMKV.nitro';
|
|
2
|
+
export type { Configuration, Mode } from './specs/MMKVFactory.nitro';
|
|
3
|
+
export { createMMKV } from './createMMKV/createMMKV';
|
|
4
|
+
export { useMMKV } from './hooks/useMMKV';
|
|
5
|
+
export { useMMKVBoolean } from './hooks/useMMKVBoolean';
|
|
6
|
+
export { useMMKVBuffer } from './hooks/useMMKVBuffer';
|
|
7
|
+
export { useMMKVNumber } from './hooks/useMMKVNumber';
|
|
8
|
+
export { useMMKVObject } from './hooks/useMMKVObject';
|
|
9
|
+
export { useMMKVString } from './hooks/useMMKVString';
|
|
10
|
+
export { useMMKVListener } from './hooks/useMMKVListener';
|
|
11
|
+
export { useMMKVKeys } from './hooks/useMMKVKeys';
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
// The create function
|
|
2
|
+
export { createMMKV } from './createMMKV/createMMKV';
|
|
3
|
+
// All the hooks
|
|
4
|
+
export { useMMKV } from './hooks/useMMKV';
|
|
5
|
+
export { useMMKVBoolean } from './hooks/useMMKVBoolean';
|
|
6
|
+
export { useMMKVBuffer } from './hooks/useMMKVBuffer';
|
|
7
|
+
export { useMMKVNumber } from './hooks/useMMKVNumber';
|
|
8
|
+
export { useMMKVObject } from './hooks/useMMKVObject';
|
|
9
|
+
export { useMMKVString } from './hooks/useMMKVString';
|
|
10
|
+
export { useMMKVListener } from './hooks/useMMKVListener';
|
|
11
|
+
export { useMMKVKeys } from './hooks/useMMKVKeys';
|
package/lib/isTest.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function isTest(): boolean;
|
package/lib/isTest.js
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import type { HybridObject } from 'react-native-nitro-modules';
|
|
2
|
+
export interface Listener {
|
|
3
|
+
remove: () => void;
|
|
4
|
+
}
|
|
5
|
+
export interface MMKV extends HybridObject<{
|
|
6
|
+
ios: 'c++';
|
|
7
|
+
android: 'c++';
|
|
8
|
+
}> {
|
|
9
|
+
/**
|
|
10
|
+
* Set a value for the given `key`.
|
|
11
|
+
*
|
|
12
|
+
* @throws an Error if the value cannot be set.
|
|
13
|
+
*/
|
|
14
|
+
set(key: string, value: boolean | string | number | ArrayBuffer): void;
|
|
15
|
+
/**
|
|
16
|
+
* Get the boolean value for the given `key`, or `undefined` if it does not exist.
|
|
17
|
+
*
|
|
18
|
+
* @default undefined
|
|
19
|
+
*/
|
|
20
|
+
getBoolean(key: string): boolean | undefined;
|
|
21
|
+
/**
|
|
22
|
+
* Get the string value for the given `key`, or `undefined` if it does not exist.
|
|
23
|
+
*
|
|
24
|
+
* @default undefined
|
|
25
|
+
*/
|
|
26
|
+
getString(key: string): string | undefined;
|
|
27
|
+
/**
|
|
28
|
+
* Get the number value for the given `key`, or `undefined` if it does not exist.
|
|
29
|
+
*
|
|
30
|
+
* @default undefined
|
|
31
|
+
*/
|
|
32
|
+
getNumber(key: string): number | undefined;
|
|
33
|
+
/**
|
|
34
|
+
* Get a raw buffer of unsigned 8-bit (0-255) data.
|
|
35
|
+
*
|
|
36
|
+
* @default undefined
|
|
37
|
+
*/
|
|
38
|
+
getBuffer(key: string): ArrayBuffer | undefined;
|
|
39
|
+
/**
|
|
40
|
+
* Checks whether the given `key` is being stored in this MMKV instance.
|
|
41
|
+
*/
|
|
42
|
+
contains(key: string): boolean;
|
|
43
|
+
/**
|
|
44
|
+
* Removes the given `key`.
|
|
45
|
+
*/
|
|
46
|
+
remove(key: string): void;
|
|
47
|
+
/**
|
|
48
|
+
* Get all keys.
|
|
49
|
+
*
|
|
50
|
+
* @default []
|
|
51
|
+
*/
|
|
52
|
+
getAllKeys(): string[];
|
|
53
|
+
/**
|
|
54
|
+
* Clears all keys/values.
|
|
55
|
+
*/
|
|
56
|
+
clearAll(): void;
|
|
57
|
+
/**
|
|
58
|
+
* Sets (or updates) the encryption-key to encrypt all data in this MMKV instance with.
|
|
59
|
+
*
|
|
60
|
+
* To remove encryption, pass `undefined` as a key.
|
|
61
|
+
*
|
|
62
|
+
* Encryption keys can have a maximum length of 16 bytes.
|
|
63
|
+
*
|
|
64
|
+
* @throws an Error if the instance cannot be recrypted.
|
|
65
|
+
*/
|
|
66
|
+
recrypt(key: string | undefined): void;
|
|
67
|
+
/**
|
|
68
|
+
* Trims the storage space and clears memory cache.
|
|
69
|
+
*
|
|
70
|
+
* Since MMKV does not resize itself after deleting keys, you can call `trim()`
|
|
71
|
+
* after deleting a bunch of keys to manually trim the memory- and
|
|
72
|
+
* disk-file to reduce storage and memory usage.
|
|
73
|
+
*
|
|
74
|
+
* In most applications, this is not needed at all.
|
|
75
|
+
*/
|
|
76
|
+
trim(): void;
|
|
77
|
+
/**
|
|
78
|
+
* Get the current total size of the storage, in bytes.
|
|
79
|
+
*/
|
|
80
|
+
readonly size: number;
|
|
81
|
+
/**
|
|
82
|
+
* Returns whether this instance is in read-only mode or not.
|
|
83
|
+
* If this is `true`, you can only use "get"-functions.
|
|
84
|
+
*/
|
|
85
|
+
readonly isReadOnly: boolean;
|
|
86
|
+
/**
|
|
87
|
+
* Adds a value changed listener. The Listener will be called whenever any value
|
|
88
|
+
* in this storage instance changes (set or delete).
|
|
89
|
+
*
|
|
90
|
+
* To unsubscribe from value changes, call `remove()` on the Listener.
|
|
91
|
+
*/
|
|
92
|
+
addOnValueChangedListener(onValueChanged: (key: string) => void): Listener;
|
|
93
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|