react-native-mmkv 2.1.0 → 2.1.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.
@@ -1,32 +0,0 @@
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
@@ -1 +0,0 @@
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"]}
@@ -1,131 +0,0 @@
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 { unstable_batchedUpdates } from 'react-native';
4
- import { createMMKV } from './createMMKV';
5
- const onValueChangedListeners = new Map();
6
- /**
7
- * A single MMKV instance.
8
- */
9
-
10
- export class MMKV {
11
- /**
12
- * Creates a new MMKV instance with the given Configuration.
13
- * If no custom `id` is supplied, `'mmkv.default'` will be used.
14
- */
15
- constructor(configuration = {
16
- id: 'mmkv.default'
17
- }) {
18
- _defineProperty(this, "nativeInstance", void 0);
19
-
20
- _defineProperty(this, "functionCache", void 0);
21
-
22
- _defineProperty(this, "id", void 0);
23
-
24
- this.id = configuration.id;
25
- this.nativeInstance = createMMKV(configuration);
26
- this.functionCache = {};
27
- }
28
-
29
- get onValueChangedListeners() {
30
- if (!onValueChangedListeners.has(this.id)) {
31
- onValueChangedListeners.set(this.id, []);
32
- }
33
-
34
- return onValueChangedListeners.get(this.id);
35
- }
36
-
37
- getFunctionFromCache(functionName) {
38
- if (this.functionCache[functionName] == null) {
39
- this.functionCache[functionName] = this.nativeInstance[functionName];
40
- }
41
-
42
- return this.functionCache[functionName];
43
- }
44
-
45
- onValuesAboutToChange(keys) {
46
- if (this.onValueChangedListeners.length === 0) return;
47
- setImmediate(() => {
48
- unstable_batchedUpdates(() => {
49
- for (const key of keys) {
50
- for (const listener of this.onValueChangedListeners) {
51
- listener(key);
52
- }
53
- }
54
- });
55
- });
56
- }
57
-
58
- set(key, value) {
59
- this.onValuesAboutToChange([key]);
60
- const func = this.getFunctionFromCache('set');
61
- return func(key, value);
62
- }
63
-
64
- getBoolean(key) {
65
- const func = this.getFunctionFromCache('getBoolean');
66
- return func(key);
67
- }
68
-
69
- getString(key) {
70
- const func = this.getFunctionFromCache('getString');
71
- return func(key);
72
- }
73
-
74
- getNumber(key) {
75
- const func = this.getFunctionFromCache('getNumber');
76
- return func(key);
77
- }
78
-
79
- contains(key) {
80
- const func = this.getFunctionFromCache('contains');
81
- return func(key);
82
- }
83
-
84
- delete(key) {
85
- this.onValuesAboutToChange([key]);
86
- const func = this.getFunctionFromCache('delete');
87
- return func(key);
88
- }
89
-
90
- getAllKeys() {
91
- const func = this.getFunctionFromCache('getAllKeys');
92
- return func();
93
- }
94
-
95
- clearAll() {
96
- const keys = this.getAllKeys();
97
- this.onValuesAboutToChange(keys);
98
- const func = this.getFunctionFromCache('clearAll');
99
- return func();
100
- }
101
-
102
- recrypt(key) {
103
- const func = this.getFunctionFromCache('recrypt');
104
- return func(key);
105
- }
106
-
107
- toString() {
108
- return `MMKV (${this.id}): [${this.getAllKeys().join(', ')}]`;
109
- }
110
-
111
- toJSON() {
112
- return {
113
- [this.id]: this.getAllKeys()
114
- };
115
- }
116
-
117
- addOnValueChangedListener(onValueChanged) {
118
- this.onValueChangedListeners.push(onValueChanged);
119
- return {
120
- remove: () => {
121
- const index = this.onValueChangedListeners.indexOf(onValueChanged);
122
-
123
- if (index !== -1) {
124
- this.onValueChangedListeners.splice(index, 1);
125
- }
126
- }
127
- };
128
- }
129
-
130
- }
131
- //# sourceMappingURL=MMKV.js.map
@@ -1 +0,0 @@
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","recrypt","toString","join","toJSON","addOnValueChangedListener","onValueChanged","push","remove","index","indexOf","splice"],"mappings":";;AAAA,SAASA,uBAAT,QAAwC,cAAxC;AACA,SAASC,UAAT,QAA2B,cAA3B;AAwHA,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,EAAj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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;;AA7GwC","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 * 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) => 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 * 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 | '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 = 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 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"]}
@@ -1,55 +0,0 @@
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
@@ -1 +0,0 @@
1
- {"version":3,"sources":["createMMKV.ts"],"names":["NativeModules","Platform","ROOT_DIRECTORY","createMMKV","config","global","mmkvCreateNewInstance","MMKVModule","MMKV","message","OS","ExpoConstants","NativeUnimoduleProxy","modulesConstants","ExponentConstants","appOwnership","Error","nativeCallSyncHook","install","result"],"mappings":"AAAA,SAASA,aAAT,EAAwBC,QAAxB,QAAwC,cAAxC;AASA;AACA,MAAMC,cAA6B,GAAG,IAAtC;AAEA,OAAO,MAAMC,UAAU,GAAIC,MAAD,IAA2C;AACnE;AACA,MAAIC,MAAM,CAACC,qBAAP,IAAgC,IAApC,EAA0C;AACxC;AACA,UAAMC,UAAU,GAAGP,aAAa,CAACQ,IAAjC;;AACA,QAAID,UAAU,IAAI,IAAlB,EAAwB;AAAA;;AACtB,UAAIE,OAAO,GACT,kFADF;AAEAA,MAAAA,OAAO,IACL,mGADF;;AAEA,UAAIR,QAAQ,CAACS,EAAT,KAAgB,KAAhB,IAAyBT,QAAQ,CAACS,EAAT,KAAgB,OAA7C,EAAsD;AACpDD,QAAAA,OAAO,IAAI,4DAAX;AACD;;AACD,UAAIR,QAAQ,CAACS,EAAT,KAAgB,SAApB,EAA+B;AAC7BD,QAAAA,OAAO,IAAI,iCAAX;AACD,OAVqB,CAWtB;;;AACA,YAAME,aAAa,4BACjBX,aAAa,CAACY,oBADG,oFACjB,sBAAoCC,gBADnB,2DACjB,uBAAsDC,iBADxD;;AAEA,UAAIH,aAAa,IAAI,IAArB,EAA2B;AACzB,YAAIA,aAAa,CAACI,YAAd,KAA+B,MAAnC,EAA2C;AACzC;AACA,gBAAM,IAAIC,KAAJ,CACJ,+GADI,CAAN;AAGD,SALD,MAKO;AACL;AACAP,UAAAA,OAAO,IAAI,wCAAX;AACD;AACF;;AAEDA,MAAAA,OAAO,IAAI,oCAAX;AACA,YAAM,IAAIO,KAAJ,CAAUP,OAAV,CAAN;AACD,KA/BuC,CAiCxC;;;AACA,QAAIJ,MAAM,CAACY,kBAAP,IAA6B,IAA7B,IAAqCV,UAAU,CAACW,OAAX,IAAsB,IAA/D,EAAqE;AACnE,YAAM,IAAIF,KAAJ,CACJ,sQADI,CAAN;AAGD,KAtCuC,CAwCxC;;;AACA,UAAMG,MAAM,GAAGZ,UAAU,CAACW,OAAX,CAAmBhB,cAAnB,CAAf;AACA,QAAIiB,MAAM,KAAK,IAAf,EACE,MAAM,IAAIH,KAAJ,CACH,sJAAqJG,MAAO,EADzJ,CAAN,CA3CsC,CA+CxC;;AACA,QAAId,MAAM,CAACC,qBAAP,IAAgC,IAApC,EACE,MAAM,IAAIU,KAAJ,CACJ,8IADI,CAAN;AAGH;;AAED,SAAOX,MAAM,CAACC,qBAAP,CAA6BF,MAA7B,CAAP;AACD,CAzDM","sourcesContent":["import { NativeModules, Platform } from 'react-native';\nimport type { MMKVConfiguration, NativeMMKV } from 'react-native-mmkv';\n\n// global func declaration for JSI functions\ndeclare global {\n function mmkvCreateNewInstance(configuration: MMKVConfiguration): NativeMMKV;\n function nativeCallSyncHook(): unknown;\n}\n\n// Root directory of all MMKV stores\nconst ROOT_DIRECTORY: string | null = null;\n\nexport const createMMKV = (config: MMKVConfiguration): NativeMMKV => {\n // Check if the constructor exists. If not, try installing the JSI bindings.\n if (global.mmkvCreateNewInstance == null) {\n // Get the native MMKV ReactModule\n const MMKVModule = NativeModules.MMKV;\n if (MMKVModule == null) {\n let message =\n 'Failed to create a new MMKV instance: The native MMKV Module could not be found.';\n message +=\n '\\n* Make sure react-native-mmkv is correctly autolinked (run `npx react-native config` to verify)';\n if (Platform.OS === 'ios' || Platform.OS === 'macos') {\n message += '\\n* Make sure you ran `pod install` in the ios/ directory.';\n }\n if (Platform.OS === 'android') {\n message += '\\n* Make sure gradle is synced.';\n }\n // check if Expo\n const ExpoConstants =\n NativeModules.NativeUnimoduleProxy?.modulesConstants?.ExponentConstants;\n if (ExpoConstants != null) {\n if (ExpoConstants.appOwnership === 'expo') {\n // We're running Expo Go\n throw new Error(\n 'react-native-mmkv is not supported in Expo Go! Use EAS (`expo prebuild`) or eject to a bare workflow instead.'\n );\n } else {\n // We're running Expo bare / standalone\n message += '\\n* Make sure you ran `expo prebuild`.';\n }\n }\n\n message += '\\n* Make sure you rebuilt the app.';\n throw new Error(message);\n }\n\n // Check if we are running on-device (JSI)\n if (global.nativeCallSyncHook == null || MMKVModule.install == null) {\n throw new Error(\n '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.'\n );\n }\n\n // Call the synchronous blocking install() function\n const result = MMKVModule.install(ROOT_DIRECTORY);\n if (result !== true)\n throw new Error(\n `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}`\n );\n\n // Check again if the constructor now exists. If not, throw an error.\n if (global.mmkvCreateNewInstance == null)\n throw new Error(\n 'Failed to create a new MMKV instance, the native initializer function does not exist. Are you trying to use MMKV from different JS Runtimes?'\n );\n }\n\n return global.mmkvCreateNewInstance(config);\n};\n"]}
@@ -1,60 +0,0 @@
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
- recrypt: () => {
56
- throw new Error('`recrypt(..)` is not supported on Web!');
57
- }
58
- };
59
- };
60
- //# sourceMappingURL=createMMKV.web.js.map
@@ -1 +0,0 @@
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","recrypt"],"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,I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AlB,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,IARxC;AASLiB,IAAAA,OAAO,EAAE,MAAM;AACb,YAAM,IAAI3B,KAAJ,CAAU,wCAAV,CAAN;AACD;AAXI,GAAP;AAaD,CAtCM","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 recrypt: () => {\n throw new Error('`recrypt(..)` is not supported on Web!');\n },\n };\n};\n"]}
@@ -1,158 +0,0 @@
1
- import { useRef, useState, useMemo, useCallback, useEffect } from 'react';
2
- import { MMKV } from './MMKV';
3
-
4
- function isConfigurationEqual(left, right) {
5
- return left.encryptionKey === right.encryptionKey && left.id === right.id && left.path === right.path;
6
- }
7
-
8
- let defaultInstance = null;
9
-
10
- function getDefaultInstance() {
11
- if (defaultInstance == null) {
12
- defaultInstance = new MMKV();
13
- }
14
-
15
- return defaultInstance;
16
- }
17
-
18
- export function useMMKV(configuration) {
19
- const instance = useRef();
20
- const lastConfiguration = useRef();
21
-
22
- if (lastConfiguration.current == null || !isConfigurationEqual(lastConfiguration.current, configuration)) {
23
- lastConfiguration.current = configuration;
24
- instance.current = new MMKV(configuration);
25
- } // @ts-expect-error it's not null, I promise.
26
-
27
-
28
- return instance;
29
- }
30
-
31
- function createMMKVHook(getter) {
32
- return (key, instance) => {
33
- const mmkv = instance !== null && instance !== void 0 ? instance : getDefaultInstance();
34
- const [value, setValue] = useState(() => getter(mmkv, key));
35
- const valueRef = useRef(value);
36
- valueRef.current = value;
37
- const set = useCallback(v => {
38
- const newValue = typeof v === 'function' ? v(valueRef.current) : v;
39
-
40
- switch (typeof newValue) {
41
- case 'number':
42
- case 'string':
43
- case 'boolean':
44
- mmkv.set(key, newValue);
45
- break;
46
-
47
- case 'undefined':
48
- mmkv.delete(key);
49
- break;
50
-
51
- default:
52
- throw new Error(`MMKV: Type ${typeof newValue} is not supported!`);
53
- }
54
- }, [key, mmkv]);
55
- useEffect(() => {
56
- const listener = mmkv.addOnValueChangedListener(changedKey => {
57
- if (changedKey === key) {
58
- setValue(getter(mmkv, key));
59
- }
60
- });
61
- return () => listener.remove();
62
- }, [key, mmkv]);
63
- return [value, set];
64
- };
65
- }
66
- /**
67
- * Use the string value of the given `key` from the given MMKV storage instance.
68
- *
69
- * If no instance is provided, a shared default instance will be used.
70
- *
71
- * @example
72
- * ```ts
73
- * const [username, setUsername] = useMMKVString("user.name")
74
- * ```
75
- */
76
-
77
-
78
- export const useMMKVString = createMMKVHook((instance, key) => instance.getString(key));
79
- /**
80
- * Use the number value of the given `key` from the given MMKV storage instance.
81
- *
82
- * If no instance is provided, a shared default instance will be used.
83
- *
84
- * @example
85
- * ```ts
86
- * const [age, setAge] = useMMKVNumber("user.age")
87
- * ```
88
- */
89
-
90
- export const useMMKVNumber = createMMKVHook((instance, key) => instance.getNumber(key));
91
- /**
92
- * Use the boolean value of the given `key` from the given MMKV storage instance.
93
- *
94
- * If no instance is provided, a shared default instance will be used.
95
- *
96
- * @example
97
- * ```ts
98
- * const [isPremiumAccount, setIsPremiumAccount] = useMMKVBoolean("user.isPremium")
99
- * ```
100
- */
101
-
102
- export const useMMKVBoolean = createMMKVHook((instance, key) => instance.getBoolean(key));
103
- /**
104
- * Use an object 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
- * The object will be serialized using `JSON`.
109
- *
110
- * @example
111
- * ```ts
112
- * const [user, setUser] = useMMKVObject<User>("user")
113
- * ```
114
- */
115
-
116
- export function useMMKVObject(key, instance) {
117
- const [string, setString] = useMMKVString(key, instance);
118
- const value = useMemo(() => {
119
- if (string == null) return undefined;
120
- return JSON.parse(string);
121
- }, [string]);
122
- const setValue = useCallback(v => {
123
- if (v == null) {
124
- // Clear the Value
125
- setString(undefined);
126
- } else {
127
- // Store the Object as a serialized Value
128
- setString(JSON.stringify(v));
129
- }
130
- }, [setString]);
131
- return [value, setValue];
132
- }
133
- /**
134
- * Listen for changes in the given MMKV storage instance.
135
- * If no instance is passed, the default instance will be used.
136
- * @param valueChangedListener The function to call whenever a value inside the storage instance changes
137
- * @param instance The instance to listen to changes to (or the default instance)
138
- *
139
- * @example
140
- * ```ts
141
- * useMMKVListener((key) => {
142
- * console.log(`Value for "${key}" changed!`)
143
- * })
144
- * ```
145
- */
146
-
147
- export function useMMKVListener(valueChangedListener, instance) {
148
- const ref = useRef(valueChangedListener);
149
- ref.current = valueChangedListener;
150
- const mmkv = instance !== null && instance !== void 0 ? instance : getDefaultInstance();
151
- useEffect(() => {
152
- const listener = mmkv.addOnValueChangedListener(changedKey => {
153
- ref.current(changedKey);
154
- });
155
- return () => listener.remove();
156
- }, [mmkv]);
157
- }
158
- //# sourceMappingURL=hooks.js.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["hooks.ts"],"names":["useRef","useState","useMemo","useCallback","useEffect","MMKV","isConfigurationEqual","left","right","encryptionKey","id","path","defaultInstance","getDefaultInstance","useMMKV","configuration","instance","lastConfiguration","current","createMMKVHook","getter","key","mmkv","value","setValue","valueRef","set","v","newValue","delete","Error","listener","addOnValueChangedListener","changedKey","remove","useMMKVString","getString","useMMKVNumber","getNumber","useMMKVBoolean","getBoolean","useMMKVObject","string","setString","undefined","JSON","parse","stringify","useMMKVListener","valueChangedListener","ref"],"mappings":"AAAA,SACEA,MADF,EAEEC,QAFF,EAGEC,OAHF,EAIEC,WAJF,EAKEC,SALF,QAMO,OANP;AAOA,SAASC,IAAT,QAAwC,QAAxC;;AAEA,SAASC,oBAAT,CACEC,IADF,EAEEC,KAFF,EAGW;AACT,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,IAAIP,IAAJ,EAAlB;AACD;;AACD,SAAOO,eAAP;AACD;;AAED,OAAO,SAASE,OAAT,CACLC,aADK,EAEkB;AACvB,QAAMC,QAAQ,GAAGhB,MAAM,EAAvB;AAEA,QAAMiB,iBAAiB,GAAGjB,MAAM,EAAhC;;AACA,MACEiB,iBAAiB,CAACC,OAAlB,IAA6B,IAA7B,IACA,CAACZ,oBAAoB,CAACW,iBAAiB,CAACC,OAAnB,EAA4BH,aAA5B,CAFvB,EAGE;AACAE,IAAAA,iBAAiB,CAACC,OAAlB,GAA4BH,aAA5B;AACAC,IAAAA,Q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AAD,CAAT;AACD,KAHD,MAGO;AACL;AACAD,MAAAA,SAAS,CAACE,IAAI,CAACE,SAAL,CAAepB,CAAf,CAAD,CAAT;AACD;AACF,GATyB,EAU1B,CAACgB,SAAD,CAV0B,CAA5B;AAaA,SAAO,CAACpB,KAAD,EAAQC,QAAR,CAAP;AACD;AAED;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,OAAO,SAASwB,eAAT,CACLC,oBADK,EAELjC,QAFK,EAGC;AACN,QAAMkC,GAAG,GAAGlD,MAAM,CAACiD,oBAAD,CAAlB;AACAC,EAAAA,GAAG,CAAChC,OAAJ,GAAc+B,oBAAd;AAEA,QAAM3B,IAAI,GAAGN,QAAH,aAAGA,QAAH,cAAGA,QAAH,GAAeH,kBAAkB,EAA3C;AAEAT,EAAAA,SAAS,CAAC,MAAM;AACd,UAAM2B,QAAQ,GAAGT,IAAI,CAACU,yBAAL,CAAgCC,UAAD,IAAgB;AAC9DiB,MAAAA,GAAG,CAAChC,OAAJ,CAAYe,UAAZ;AACD,KAFgB,CAAjB;AAGA,WAAO,MAAMF,QAAQ,CAACG,MAAT,EAAb;AACD,GALQ,EAKN,CAACZ,IAAD,CALM,CAAT;AAMD","sourcesContent":["import React, {\n useRef,\n useState,\n useMemo,\n useCallback,\n useEffect,\n} from 'react';\nimport { MMKV, MMKVConfiguration } from './MMKV';\n\nfunction isConfigurationEqual(\n left: MMKVConfiguration,\n right: MMKVConfiguration\n): boolean {\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\nexport function useMMKV(\n configuration: MMKVConfiguration\n): React.RefObject<MMKV> {\n const instance = useRef<MMKV>();\n\n const lastConfiguration = useRef<MMKVConfiguration>();\n if (\n lastConfiguration.current == null ||\n !isConfigurationEqual(lastConfiguration.current, configuration)\n ) {\n lastConfiguration.current = configuration;\n instance.current = new MMKV(configuration);\n }\n\n // @ts-expect-error it's not null, I promise.\n return instance;\n}\n\nfunction createMMKVHook<\n T extends boolean | number | (string | 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 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 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 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"]}
@@ -1,3 +0,0 @@
1
- export * from './MMKV';
2
- export * from './hooks';
3
- //# sourceMappingURL=index.js.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["index.ts"],"names":[],"mappings":"AAAA,cAAc,QAAd;AACA,cAAc,SAAd","sourcesContent":["export * from './MMKV';\nexport * from './hooks';\n"]}
@@ -1,130 +0,0 @@
1
- interface Listener {
2
- remove: () => void;
3
- }
4
- /**
5
- * Used for configuration of a single MMKV instance.
6
- */
7
- export interface MMKVConfiguration {
8
- /**
9
- * The MMKV instance's ID. If you want to use multiple instances, make sure to use different IDs!
10
- *
11
- * @example
12
- * ```ts
13
- * const userStorage = new MMKV({ id: `user-${userId}-storage` })
14
- * const globalStorage = new MMKV({ id: 'global-app-storage' })
15
- * ```
16
- *
17
- * @default 'mmkv.default'
18
- */
19
- id: string;
20
- /**
21
- * The MMKV instance's root path. By default, MMKV stores file inside `$(Documents)/mmkv/`. You can customize MMKV's root directory on MMKV initialization:
22
- *
23
- * @example
24
- * ```ts
25
- * const temporaryStorage = new MMKV({ path: '/tmp/' })
26
- * ```
27
- */
28
- path?: string;
29
- /**
30
- * 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.
31
- *
32
- * Encryption keys can have a maximum length of 16 bytes.
33
- *
34
- * @example
35
- * ```ts
36
- * const secureStorage = new MMKV({ encryptionKey: 'my-encryption-key!' })
37
- * ```
38
- */
39
- encryptionKey?: string;
40
- }
41
- /**
42
- * Represents a single MMKV instance.
43
- */
44
- interface MMKVInterface {
45
- /**
46
- * Set a value for the given `key`.
47
- */
48
- set: (key: string, value: boolean | string | number) => void;
49
- /**
50
- * Get a boolean value for the given `key`.
51
- *
52
- * @default false
53
- */
54
- getBoolean: (key: string) => boolean;
55
- /**
56
- * Get a string value for the given `key`.
57
- *
58
- * @default undefined
59
- */
60
- getString: (key: string) => string | undefined;
61
- /**
62
- * Get a number value for the given `key`.
63
- *
64
- * @default 0
65
- */
66
- getNumber: (key: string) => number;
67
- /**
68
- * Checks whether the given `key` is being stored in this MMKV instance.
69
- */
70
- contains: (key: string) => boolean;
71
- /**
72
- * Delete the given `key`.
73
- */
74
- delete: (key: string) => void;
75
- /**
76
- * Get all keys.
77
- *
78
- * @default []
79
- */
80
- getAllKeys: () => string[];
81
- /**
82
- * Delete all keys.
83
- */
84
- clearAll: () => void;
85
- /**
86
- * Sets (or updates) the encryption-key to encrypt all data in this MMKV instance with.
87
- *
88
- * To remove encryption, pass `undefined` as a key.
89
- *
90
- * Encryption keys can have a maximum length of 16 bytes.
91
- */
92
- recrypt: (key: string | undefined) => void;
93
- /**
94
- * Adds a value changed listener. The Listener will be called whenever any value
95
- * in this storage instance changes (set or delete).
96
- *
97
- * To unsubscribe from value changes, call `remove()` on the Listener.
98
- */
99
- addOnValueChangedListener: (onValueChanged: (key: string) => void) => Listener;
100
- }
101
- export declare type NativeMMKV = Pick<MMKVInterface, 'clearAll' | 'contains' | 'delete' | 'getAllKeys' | 'getBoolean' | 'getNumber' | 'getString' | 'set' | 'recrypt'>;
102
- /**
103
- * A single MMKV instance.
104
- */
105
- export declare class MMKV implements MMKVInterface {
106
- private nativeInstance;
107
- private functionCache;
108
- private id;
109
- /**
110
- * Creates a new MMKV instance with the given Configuration.
111
- * If no custom `id` is supplied, `'mmkv.default'` will be used.
112
- */
113
- constructor(configuration?: MMKVConfiguration);
114
- private get onValueChangedListeners();
115
- private getFunctionFromCache;
116
- private onValuesAboutToChange;
117
- set(key: string, value: boolean | string | number): void;
118
- getBoolean(key: string): boolean;
119
- getString(key: string): string | undefined;
120
- getNumber(key: string): number;
121
- contains(key: string): boolean;
122
- delete(key: string): void;
123
- getAllKeys(): string[];
124
- clearAll(): void;
125
- recrypt(key: string | undefined): void;
126
- toString(): string;
127
- toJSON(): object;
128
- addOnValueChangedListener(onValueChanged: (key: string) => void): Listener;
129
- }
130
- export {};
@@ -1,6 +0,0 @@
1
- import type { MMKVConfiguration, NativeMMKV } from 'react-native-mmkv';
2
- declare global {
3
- function mmkvCreateNewInstance(configuration: MMKVConfiguration): NativeMMKV;
4
- function nativeCallSyncHook(): unknown;
5
- }
6
- export declare const createMMKV: (config: MMKVConfiguration) => NativeMMKV;
@@ -1,2 +0,0 @@
1
- import type { MMKVConfiguration, NativeMMKV } from 'react-native-mmkv';
2
- export declare const createMMKV: (config: MMKVConfiguration) => NativeMMKV;