Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,13 @@ See [Configuring App Groups](https://developer.apple.com/documentation/xcode/con
#### Customize

```js
import { MMKV } from 'react-native-mmkv'
import { MMKV, MMKVMode } from 'react-native-mmkv'

export const storage = new MMKV({
id: `user-${userId}-storage`,
path: `${USER_DIRECTORY}/storage`,
encryptionKey: 'hunter2'
encryptionKey: 'hunter2',
mode: MMKVMode.SingleProcess
})
```

Expand All @@ -124,6 +125,8 @@ The following values can be configured:
* `id`: The MMKV instance's ID. If you want to use multiple instances, use different IDs. For example, you can separate the global app's storage and a logged-in user's storage. (required if `path` or `encryptionKey` fields are specified, otherwise defaults to: `'mmkv.default'`)
* `path`: The MMKV instance's root path. By default, MMKV stores file inside `$(Documents)/mmkv/`. You can customize MMKV's root directory on MMKV initialization (documentation: [iOS](https://github.com/Tencent/MMKV/wiki/iOS_advance#customize-location) / [Android](https://github.com/Tencent/MMKV/wiki/android_advance#customize-location))
* `encryptionKey`: The MMKV instance's encryption/decryption key. By default, MMKV stores all key-values in plain text on file, relying on iOS's/Android's sandbox to make sure the file is encrypted. Should you worry about information leaking, you can choose to encrypt MMKV. (documentation: [iOS](https://github.com/Tencent/MMKV/wiki/iOS_advance#encryption) / [Android](https://github.com/Tencent/MMKV/wiki/android_advance#encryption))
* `mode`: *Android Only*: The MMKV mode. It is set to SingleProcess by default. You can set its value to MultiProcess to support simultaneous read-write access between processus at the cost of performance. This is useful when you want to share data between your react-native app and native extensions such as widgets. _Notice_: On iOS, this will automatically be set to MMKVMode.MultiProcess if you set an AppGroup in the plist configuration (more information on AppGroups [here](https://github.com/mrousavy/react-native-mmkv/tree/master#app-groups))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to also use this prop for iOS? I hate having "android only" or "ios only" props... Maybe we can just reconfigure the instance on iOS when this prop is passed?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately no, I wanted to do this first but on iOS we only have the following constructors:

Screenshot 2023-11-30 at 9 34 38 AM

As you can see we cannot set both "rootPath" and "mode" at the same time, meaning the only way to set MMKVMode to "multi-process" is to set an AppGroup when initializing MMKV.

Copy link
Author

@maxencehenneron maxencehenneron Nov 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could however add support for single-process mode with AppGroups but I'm not sure why would anyone want to do that.



### Set

Expand Down
4 changes: 2 additions & 2 deletions android/src/main/cpp/MmkvHostObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
#include <vector>

MmkvHostObject::MmkvHostObject(const std::string& instanceId, std::string path,
std::string cryptKey) {
std::string cryptKey, MMKVMode mmkvMode) {
bool hasEncryptionKey = cryptKey.size() > 0;
__android_log_print(ANDROID_LOG_INFO, "RNMMKV",
"Creating MMKV instance \"%s\"... (Path: %s, Encrypted: %b)",
instanceId.c_str(), path.c_str(), hasEncryptionKey);
std::string* pathPtr = path.size() > 0 ? &path : nullptr;
std::string* cryptKeyPtr = cryptKey.size() > 0 ? &cryptKey : nullptr;
instance = MMKV::mmkvWithID(instanceId, mmkv::DEFAULT_MMAP_SIZE, MMKV_SINGLE_PROCESS, cryptKeyPtr,
instance = MMKV::mmkvWithID(instanceId, mmkv::DEFAULT_MMAP_SIZE, mmkvMode, cryptKeyPtr,
pathPtr);

if (instance == nullptr) {
Expand Down
2 changes: 1 addition & 1 deletion android/src/main/cpp/MmkvHostObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ using namespace facebook;

class JSI_EXPORT MmkvHostObject : public jsi::HostObject {
public:
MmkvHostObject(const std::string& instanceId, std::string path, std::string cryptKey);
MmkvHostObject(const std::string& instanceId, std::string path, std::string cryptKey, MMKVMode mmkvMode);

public:
jsi::Value get(jsi::Runtime&, const jsi::PropNameID& name) override;
Expand Down
19 changes: 18 additions & 1 deletion android/src/main/cpp/cpp-adapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,22 @@ std::string getPropertyAsStringOrEmptyFromObject(jsi::Object& object,
return value.isString() ? value.asString(runtime).utf8(runtime) : "";
}

MMKVMode getPropertyAsMMKVModeFromObject(jsi::Object& object,
const std::string& propertyName,
jsi::Runtime& runtime) {
jsi::Value value = object.getProperty(runtime, propertyName.c_str());

// Checks that the value is a number without any fractional part
double integralPart;
if (!value.isNumber() || modf(value.asNumber(), &integralPart) != 0) {
// The value is not an integer which does not make sense. Return a default value
return MMKV_SINGLE_PROCESS;
}

// Cast to uint32_t value
return static_cast<MMKVMode>(integralPart);
}

void install(jsi::Runtime& jsiRuntime) {
// MMKV.createNewInstance()
auto mmkvCreateNewInstance = jsi::Function::createFromHostFunction(
Expand All @@ -28,8 +44,9 @@ void install(jsi::Runtime& jsiRuntime) {
std::string path = getPropertyAsStringOrEmptyFromObject(config, "path", runtime);
std::string encryptionKey =
getPropertyAsStringOrEmptyFromObject(config, "encryptionKey", runtime);
MMKVMode mode = getPropertyAsMMKVModeFromObject(config, "mode", runtime);

auto instance = std::make_shared<MmkvHostObject>(instanceId, path, encryptionKey);
auto instance = std::make_shared<MmkvHostObject>(instanceId, path, encryptionKey, mode);
return jsi::Object::createFromHostObject(runtime, instance);
});
jsiRuntime.global().setProperty(jsiRuntime, "mmkvCreateNewInstance",
Expand Down
22 changes: 22 additions & 0 deletions src/MMKV.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ interface Listener {
remove: () => void;
}

/**
* All the possible MMKV modes. The values are extracted from [MMKV.h](https://github.com/Tencent/MMKV/blob/a8d3cdd2a43a8e5c5ab5043dfad111c96a4963bf/Core/MMKV.h#L41)
*/
export enum MMKVMode {
SingleProcess = 1 << 0,
MultiProcess = 1 << 1,
}

/**
* Used for configuration of a single MMKV instance.
*/
Expand Down Expand Up @@ -46,6 +54,20 @@ export interface MMKVConfiguration {
* ```
*/
encryptionKey?: string;
/**
* *Android Only*: The MMKV mode. It is set to SingleProcess by default. You can set its value to MultiProcess to support simultaneous read-write access between processus at the cost of performance.
*
* This is useful when you want to share data between your react-native app and native extensions such as widgets.
*
* @example
* ```ts
* const extensionStorage = new MMKV({ mode: MMKVMode.MultiProcess })
* ```
*
* _Notice_: On iOS, this will automatically be set to MMKVMode.MultiProcess if you set an AppGroup in the plist configuration.
* More information on AppGroups [here](https://github.com/mrousavy/react-native-mmkv/tree/master#app-groups)
*/
mode?: MMKVMode;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please use TypeScript unions instead of enums here?

So instead of mode?: MMKVMode it would be mode?: 'single-process' | 'multi-process'

}

/**
Expand Down