Skip to content

Commit

Permalink
feat: Create PlatformContext TurboModule
Browse files Browse the repository at this point in the history
  • Loading branch information
mrousavy committed Mar 27, 2024
1 parent 0f33f1c commit 87dedcd
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 51 deletions.
1 change: 0 additions & 1 deletion android/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ add_library(
react-native-mmkv
SHARED
src/main/cpp/AndroidLogger.cpp
src/main/cpp/AndroidMmkvPlatformContext.cpp
src/main/cpp/cpp-adapter.cpp
../cpp/MmkvHostObject.cpp
../cpp/NativeMmkvModule.cpp
Expand Down
12 changes: 0 additions & 12 deletions android/src/main/cpp/AndroidMmkvPlatformContext.cpp

This file was deleted.

26 changes: 22 additions & 4 deletions android/src/main/java/com/mrousavy/mmkv/MmkvPackage.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.mrousavy.mmkv;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.facebook.react.bridge.NativeModule;
Expand All @@ -12,15 +13,32 @@
import java.util.Map;

public class MmkvPackage extends TurboReactPackage {

@Nullable
@Override
public NativeModule getModule(String name, ReactApplicationContext reactContext) {
return null;
public NativeModule getModule(String name, @NonNull ReactApplicationContext reactContext) {
if (name.equals(MmkvPlatformContextModule.NAME)) {
return new MmkvPlatformContextModule(reactContext);
} else {
return null;
}
}

@Override
public ReactModuleInfoProvider getReactModuleInfoProvider() {
return HashMap::new;
return () -> {
final Map<String, ReactModuleInfo> moduleInfos = new HashMap<>();
moduleInfos.put(
MmkvPlatformContextModule.NAME,
new ReactModuleInfo(
MmkvPlatformContextModule.NAME,
MmkvPlatformContextModule.NAME,
false, // canOverrideExistingModule
false, // needsEagerInit
true, // hasConstants
false, // isCxxModule
true // isTurboModule
));
return moduleInfos;
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.mrousavy.mmkv;

import com.facebook.react.bridge.ReactApplicationContext;

public class MmkvPlatformContextModule extends NativeMmkvPlatformContextSpec {
private final ReactApplicationContext context;

public MmkvPlatformContextModule(ReactApplicationContext reactContext) {
super(reactContext);
context = reactContext;
}

@Override
public String getBaseDirectory() {
return context.getFilesDir().getAbsolutePath() + "/mmkv";
}
}
21 changes: 0 additions & 21 deletions cpp/MmkvPlatformContext.h

This file was deleted.

10 changes: 1 addition & 9 deletions cpp/NativeMmkvModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#include "NativeMmkvModule.h"
#include "Logger.h"
#include "MmkvHostObject.h"
#include "MmkvPlatformContext.h"

#if __has_include("MMKV.h")
#include "MMKV.h"
Expand All @@ -22,14 +21,7 @@ NativeMmkvModule::NativeMmkvModule(std::shared_ptr<CallInvoker> jsInvoker)
: NativeMmkvCxxSpec(jsInvoker) {}

bool NativeMmkvModule::initialize(jsi::Runtime& runtime,
std::optional<std::string> customBasePath) {
std::string basePath = MmkvPlatformContext::getDefaultBasePath();
if (customBasePath.has_value()) {
Logger::log("RNMMKV", "Default path is %s, but user specified a custom path: %s",
basePath.c_str(), customBasePath.value().c_str());
basePath = customBasePath.value();
}

std::string basePath) {
if (basePath.size() < 1) {
throw jsi::JSError(runtime, "Path cannot be empty!");
}
Expand Down
2 changes: 1 addition & 1 deletion cpp/NativeMmkvModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class NativeMmkvModule : public NativeMmkvCxxSpec<NativeMmkvModule> {
NativeMmkvModule(std::shared_ptr<CallInvoker> jsInvoker);
~NativeMmkvModule();

bool initialize(jsi::Runtime& runtime, std::optional<std::string> basePath);
bool initialize(jsi::Runtime& runtime, std::string basePath);
jsi::Object createMMKV(jsi::Runtime& runtime, MMKVConfig config);
};

Expand Down
12 changes: 9 additions & 3 deletions src/NativeMmkv.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { NativeModules, Platform, TurboModule } from 'react-native';
import { TurboModuleRegistry } from 'react-native';
import { UnsafeObject } from 'react-native/Libraries/Types/CodegenTypes';
import { PlatformContext } from './NativeMmkvPlatformContext';

/**
* Configures the mode of the MMKV instance.
Expand Down Expand Up @@ -71,11 +72,12 @@ export interface Configuration {
}

export interface Spec extends TurboModule {
initialize(basePath: string | undefined): boolean;
initialize(basePath: string): boolean;
createMMKV(configuration: Configuration): UnsafeObject;
}

let module: Spec | null = null;
let basePath: string | null = null;

/**
* Get the MMKV TurboModule, and initialize it if this is the first time calling.
Expand Down Expand Up @@ -117,9 +119,13 @@ export function getMMKVTurboModule(): Spec {
throw new Error(message);
}

if (basePath == null) {
// Get base path from platform specific context
basePath = PlatformContext.getBaseDirectory();
}

// Initialize MMKV
const BASE_PATH: string | undefined = undefined;
module.initialize(BASE_PATH);
module.initialize(basePath);
}

return module;
Expand Down
12 changes: 12 additions & 0 deletions src/NativeMmkvPlatformContext.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { TurboModule, TurboModuleRegistry } from 'react-native';

export interface Spec extends TurboModule {
/**
* Gets the base directory of the documents storage
*/
getBaseDirectory(): string;
}

export const PlatformContext = TurboModuleRegistry.getEnforcing<Spec>(
'MmkvPlatformContext'
);

0 comments on commit 87dedcd

Please sign in to comment.