react-native-mmkv 3.0.0-beta.1 → 3.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.
Files changed (60) hide show
  1. package/README.md +28 -3
  2. package/cpp/MMKVManagedBuffer.h +1 -6
  3. package/cpp/MmkvHostObject.h +1 -6
  4. package/cpp/NativeMmkvModule.cpp +7 -1
  5. package/lib/commonjs/LazyTurboModule.js +49 -0
  6. package/lib/commonjs/LazyTurboModule.js.map +1 -0
  7. package/lib/commonjs/MMKV.js +4 -10
  8. package/lib/commonjs/MMKV.js.map +1 -1
  9. package/lib/commonjs/NativeMmkv.js +13 -44
  10. package/lib/commonjs/NativeMmkv.js.map +1 -1
  11. package/lib/commonjs/NativeMmkvPlatformContext.js +5 -1
  12. package/lib/commonjs/NativeMmkvPlatformContext.js.map +1 -1
  13. package/lib/commonjs/Types.js +2 -0
  14. package/lib/commonjs/Types.js.map +1 -0
  15. package/lib/commonjs/createMMKV.js +1 -2
  16. package/lib/commonjs/createMMKV.js.map +1 -1
  17. package/lib/commonjs/createMMKV.web.js.map +1 -1
  18. package/lib/commonjs/index.js.map +1 -1
  19. package/lib/module/LazyTurboModule.js +43 -0
  20. package/lib/module/LazyTurboModule.js.map +1 -0
  21. package/lib/module/MMKV.js +4 -11
  22. package/lib/module/MMKV.js.map +1 -1
  23. package/lib/module/NativeMmkv.js +10 -41
  24. package/lib/module/NativeMmkv.js.map +1 -1
  25. package/lib/module/NativeMmkvPlatformContext.js +5 -1
  26. package/lib/module/NativeMmkvPlatformContext.js.map +1 -1
  27. package/lib/module/Types.js +2 -0
  28. package/lib/module/Types.js.map +1 -0
  29. package/lib/module/createMMKV.js +2 -3
  30. package/lib/module/createMMKV.js.map +1 -1
  31. package/lib/module/createMMKV.web.js.map +1 -1
  32. package/lib/module/index.js.map +1 -1
  33. package/lib/typescript/src/LazyTurboModule.d.ts +6 -0
  34. package/lib/typescript/src/LazyTurboModule.d.ts.map +1 -0
  35. package/lib/typescript/src/MMKV.d.ts +1 -85
  36. package/lib/typescript/src/MMKV.d.ts.map +1 -1
  37. package/lib/typescript/src/NativeMmkv.d.ts +9 -5
  38. package/lib/typescript/src/NativeMmkv.d.ts.map +1 -1
  39. package/lib/typescript/src/NativeMmkvPlatformContext.d.ts.map +1 -1
  40. package/lib/typescript/src/Types.d.ts +86 -0
  41. package/lib/typescript/src/Types.d.ts.map +1 -0
  42. package/lib/typescript/src/createMMKV.d.ts +2 -1
  43. package/lib/typescript/src/createMMKV.d.ts.map +1 -1
  44. package/lib/typescript/src/createMMKV.mock.d.ts +1 -1
  45. package/lib/typescript/src/createMMKV.mock.d.ts.map +1 -1
  46. package/lib/typescript/src/createMMKV.web.d.ts +2 -1
  47. package/lib/typescript/src/createMMKV.web.d.ts.map +1 -1
  48. package/lib/typescript/src/index.d.ts.map +1 -1
  49. package/package.json +17 -14
  50. package/react-native.config.js +20 -0
  51. package/src/LazyTurboModule.ts +62 -0
  52. package/src/MMKV.ts +5 -96
  53. package/src/NativeMmkv.ts +19 -46
  54. package/src/NativeMmkvPlatformContext.ts +5 -3
  55. package/src/Types.ts +89 -0
  56. package/src/__tests__/hooks.test.tsx +1 -0
  57. package/src/createMMKV.mock.ts +1 -1
  58. package/src/createMMKV.ts +4 -5
  59. package/src/createMMKV.web.ts +2 -1
  60. /package/src/{index.tsx → index.ts} +0 -0
package/src/NativeMmkv.ts CHANGED
@@ -1,6 +1,7 @@
1
- import { NativeModules, Platform, TurboModule } from 'react-native';
1
+ import { TurboModule } from 'react-native';
2
2
  import { TurboModuleRegistry } from 'react-native';
3
3
  import { UnsafeObject } from 'react-native/Libraries/Types/CodegenTypes';
4
+ import { getLazyTurboModule } from './LazyTurboModule';
4
5
  import { PlatformContext } from './NativeMmkvPlatformContext';
5
6
 
6
7
  /**
@@ -72,61 +73,33 @@ export interface Configuration {
72
73
  }
73
74
 
74
75
  export interface Spec extends TurboModule {
76
+ /**
77
+ * Initialize MMKV with the given base storage directory.
78
+ * This should be the documents directory by default.
79
+ */
75
80
  initialize(basePath: string): boolean;
81
+ /**
82
+ * Create a new instance of MMKV.
83
+ * The returned {@linkcode UnsafeObject} is a `jsi::HostObject`.
84
+ */
76
85
  createMMKV(configuration: Configuration): UnsafeObject;
77
86
  }
78
87
 
79
- let module: Spec | null = null;
80
88
  let basePath: string | null = null;
81
89
 
82
- /**
83
- * Get the MMKV TurboModule, and initialize it if this is the first time calling.
84
- * This will throw an error if the module cannot be found.
85
- */
86
- export function getMMKVTurboModule(): Spec {
87
- if (module == null) {
88
- // try to find the turbomodule
89
- module = TurboModuleRegistry.get<Spec>('MmkvCxx');
90
-
91
- if (module == null) {
92
- // if it still is null, something went wrong!
93
- let message =
94
- 'Failed to create a new MMKV instance: The native MMKV Module could not be found.';
95
- message +=
96
- '\n* Make sure react-native-mmkv is correctly autolinked (run `npx react-native config` to verify)';
97
- if (Platform.OS === 'ios' || Platform.OS === 'macos') {
98
- message += '\n* Make sure you ran `pod install` in the ios/ directory.';
99
- }
100
- if (Platform.OS === 'android') {
101
- message += '\n* Make sure gradle is synced.';
102
- }
103
- // check if Expo
104
- const ExpoConstants =
105
- NativeModules.NativeUnimoduleProxy?.modulesConstants?.ExponentConstants;
106
- if (ExpoConstants != null) {
107
- if (ExpoConstants.appOwnership === 'expo') {
108
- // We're running Expo Go
109
- throw new Error(
110
- 'react-native-mmkv is not supported in Expo Go! Use EAS (`expo prebuild`) or eject to a bare workflow instead.'
111
- );
112
- } else {
113
- // We're running Expo bare / standalone
114
- message += '\n* Make sure you ran `expo prebuild`.';
115
- }
116
- }
117
-
118
- message += '\n* Make sure you rebuilt the app.';
119
- throw new Error(message);
120
- }
90
+ function getNativeModule(): Spec | null {
91
+ if (basePath == null) {
92
+ // use default base path from the Platform (iOS/Android)
93
+ basePath = PlatformContext.getBaseDirectory();
94
+ }
121
95
 
122
- if (basePath == null) {
123
- // Get base path from platform specific context
124
- basePath = PlatformContext.getBaseDirectory();
125
- }
96
+ const module = TurboModuleRegistry.get<Spec>('MmkvCxx');
126
97
 
127
- // Initialize MMKV
98
+ if (module != null) {
99
+ // initialize MMKV
128
100
  module.initialize(basePath);
129
101
  }
130
102
 
131
103
  return module;
132
104
  }
105
+ export const MMKVTurboModule = getLazyTurboModule(getNativeModule);
@@ -1,4 +1,5 @@
1
1
  import { TurboModule, TurboModuleRegistry } from 'react-native';
2
+ import { getLazyTurboModule } from './LazyTurboModule';
2
3
 
3
4
  export interface Spec extends TurboModule {
4
5
  /**
@@ -7,6 +8,7 @@ export interface Spec extends TurboModule {
7
8
  getBaseDirectory(): string;
8
9
  }
9
10
 
10
- export const PlatformContext = TurboModuleRegistry.getEnforcing<Spec>(
11
- 'MmkvPlatformContext'
12
- );
11
+ function getModule(): Spec | null {
12
+ return TurboModuleRegistry.get<Spec>('MmkvPlatformContext');
13
+ }
14
+ export const PlatformContext = getLazyTurboModule(getModule);
package/src/Types.ts ADDED
@@ -0,0 +1,89 @@
1
+ /**
2
+ * Represents a single MMKV instance.
3
+ */
4
+ export interface NativeMMKV {
5
+ /**
6
+ * Set a value for the given `key`.
7
+ */
8
+ set: (key: string, value: boolean | string | number | ArrayBuffer) => void;
9
+ /**
10
+ * Get the boolean value for the given `key`, or `undefined` if it does not exist.
11
+ *
12
+ * @default undefined
13
+ */
14
+ getBoolean: (key: string) => boolean | undefined;
15
+ /**
16
+ * Get the string value for the given `key`, or `undefined` if it does not exist.
17
+ *
18
+ * @default undefined
19
+ */
20
+ getString: (key: string) => string | undefined;
21
+ /**
22
+ * Get the number value for the given `key`, or `undefined` if it does not exist.
23
+ *
24
+ * @default undefined
25
+ */
26
+ getNumber: (key: string) => number | undefined;
27
+ /**
28
+ * Get a raw buffer of unsigned 8-bit (0-255) data.
29
+ *
30
+ * @default undefined
31
+ */
32
+ getBuffer: (key: string) => ArrayBuffer | undefined;
33
+ /**
34
+ * Checks whether the given `key` is being stored in this MMKV instance.
35
+ */
36
+ contains: (key: string) => boolean;
37
+ /**
38
+ * Delete the given `key`.
39
+ */
40
+ delete: (key: string) => void;
41
+ /**
42
+ * Get all keys.
43
+ *
44
+ * @default []
45
+ */
46
+ getAllKeys: () => string[];
47
+ /**
48
+ * Delete all keys.
49
+ */
50
+ clearAll: () => void;
51
+ /**
52
+ * Sets (or updates) the encryption-key to encrypt all data in this MMKV instance with.
53
+ *
54
+ * To remove encryption, pass `undefined` as a key.
55
+ *
56
+ * Encryption keys can have a maximum length of 16 bytes.
57
+ */
58
+ recrypt: (key: string | undefined) => void;
59
+ /**
60
+ * Trims the storage space and clears memory cache.
61
+ *
62
+ * Since MMKV does not resize itself after deleting keys, you can call `trim()`
63
+ * after deleting a bunch of keys to manually trim the memory- and
64
+ * disk-file to reduce storage and memory usage.
65
+ *
66
+ * In most applications, this is not needed at all.
67
+ */
68
+ trim(): void;
69
+ /**
70
+ * Get the current total size of the storage, in bytes.
71
+ */
72
+ readonly size: number;
73
+ }
74
+
75
+ export interface Listener {
76
+ remove: () => void;
77
+ }
78
+
79
+ export interface MMKVInterface extends NativeMMKV {
80
+ /**
81
+ * Adds a value changed listener. The Listener will be called whenever any value
82
+ * in this storage instance changes (set or delete).
83
+ *
84
+ * To unsubscribe from value changes, call `remove()` on the Listener.
85
+ */
86
+ addOnValueChangedListener: (
87
+ onValueChanged: (key: string) => void
88
+ ) => Listener;
89
+ }
@@ -13,6 +13,7 @@ const mmkv = new MMKV();
13
13
 
14
14
  beforeEach(() => {
15
15
  mmkv.clearAll();
16
+ mmkv.trim();
16
17
  });
17
18
 
18
19
  test('hooks update when the value is changed directly through the instance', () => {
@@ -1,4 +1,4 @@
1
- import type { NativeMMKV } from './MMKV';
1
+ import type { NativeMMKV } from './Types';
2
2
 
3
3
  /* Mock MMKV instance for use in tests */
4
4
  export const createMockMMKV = (): NativeMMKV => {
package/src/createMMKV.ts CHANGED
@@ -1,10 +1,9 @@
1
- import type { Configuration, NativeMMKV } from './MMKV';
2
- import { getMMKVTurboModule } from './NativeMmkv';
3
-
4
- const module = getMMKVTurboModule();
1
+ import type { Configuration } from './MMKV';
2
+ import { MMKVTurboModule } from './NativeMmkv';
3
+ import type { NativeMMKV } from './Types';
5
4
 
6
5
  export const createMMKV = (config: Configuration): NativeMMKV => {
7
- const instance = module.createMMKV(config);
6
+ const instance = MMKVTurboModule.createMMKV(config);
8
7
  if (__DEV__) {
9
8
  if (typeof instance !== 'object' || instance == null) {
10
9
  throw new Error(
@@ -1,5 +1,6 @@
1
1
  /* global localStorage */
2
- import type { Configuration, NativeMMKV } from './MMKV';
2
+ import type { Configuration } from './MMKV';
3
+ import type { NativeMMKV } from './Types';
3
4
  import { createTextEncoder } from './createTextEncoder';
4
5
 
5
6
  const canUseDOM =
File without changes