-
Notifications
You must be signed in to change notification settings - Fork 0
Replace MMKV with C file writing #57
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
stevenconner
wants to merge
1
commit into
main
Choose a base branch
from
38-replace-mmkv-with-c-file-writing
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// | ||
// IRFileStorage.mm | ||
// Reactotron-macOS | ||
// | ||
|
||
#import <Foundation/Foundation.h> | ||
#import <AppSpec/AppSpec.h> | ||
|
||
@interface IRFileStorage : NativeIRFileStorageSpecBase <NativeIRFileStorageSpec> | ||
@end | ||
|
||
@implementation IRFileStorage | ||
|
||
RCT_EXPORT_MODULE() | ||
|
||
static BOOL ensureDirectoryExists(NSString *path) { | ||
if (path.length == 0) return NO; | ||
BOOL isDir = NO; | ||
NSFileManager *fm = [NSFileManager defaultManager]; | ||
if ([fm fileExistsAtPath:path isDirectory:&isDir]) { | ||
return isDir; | ||
} | ||
NSError *error = nil; | ||
BOOL ok = [fm createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:&error]; | ||
return ok && error == nil; | ||
} | ||
|
||
// Spec methods (sync) | ||
- (NSString *)read:(NSString *)path { | ||
if (path == nil || path.length == 0) return @""; | ||
NSData *data = [NSData dataWithContentsOfFile:path]; | ||
if (data == nil) return @""; | ||
NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; | ||
return str ?: @""; | ||
} | ||
|
||
- (void)write:(NSString *)path data:(NSString *)data { | ||
if (path == nil || data == nil) return; | ||
NSString *dir = [path stringByDeletingLastPathComponent]; | ||
ensureDirectoryExists(dir); | ||
NSError *err = nil; | ||
[data writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:&err]; | ||
} | ||
|
||
- (void)remove:(NSString *)path { | ||
if (path == nil) return; | ||
NSFileManager *fm = [NSFileManager defaultManager]; | ||
if (![fm fileExistsAtPath:path]) return; | ||
NSError *err = nil; | ||
[fm removeItemAtPath:path error:&err]; | ||
} | ||
|
||
- (void)ensureDir:(NSString *)path { | ||
if (path == nil) return; | ||
ensureDirectoryExists(path); | ||
} | ||
|
||
- (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:(const facebook::react::ObjCTurboModule::InitParams &)params { | ||
return std::make_shared<facebook::react::NativeIRFileStorageSpecJSI>(params); | ||
} | ||
|
||
@end | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
#include "pch.h" | ||
#include "IRFileStorage.windows.h" | ||
#include <filesystem> | ||
#include <fstream> | ||
|
||
namespace fs = std::filesystem; | ||
|
||
namespace winrt::reactotron::implementation { | ||
|
||
static void ensure_dir_exists(const std::string &path) { | ||
if (path.empty()) return; | ||
std::error_code ec; | ||
fs::create_directories(fs::path(path), ec); | ||
} | ||
|
||
std::string IRFileStorage::read(std::string path) noexcept { | ||
try { | ||
std::ifstream file(path, std::ios::in | std::ios::binary); | ||
if (!file.is_open()) return std::string(); | ||
std::string contents; | ||
file.seekg(0, std::ios::end); | ||
contents.resize(static_cast<size_t>(file.tellg())); | ||
file.seekg(0, std::ios::beg); | ||
file.read(contents.data(), static_cast<std::streamsize>(contents.size())); | ||
return contents; | ||
} catch (...) { | ||
return std::string(); | ||
} | ||
} | ||
|
||
void IRFileStorage::write(std::string path, std::string data) noexcept { | ||
try { | ||
ensure_dir_exists(fs::path(path).parent_path().string()); | ||
std::ofstream file(path, std::ios::out | std::ios::binary | std::ios::trunc); | ||
if (!file.is_open()) return; | ||
file.write(data.data(), static_cast<std::streamsize>(data.size())); | ||
} catch (...) { | ||
} | ||
} | ||
|
||
void IRFileStorage::remove(std::string path) noexcept { | ||
try { | ||
std::error_code ec; | ||
fs::remove(fs::path(path), ec); | ||
} catch (...) { | ||
} | ||
} | ||
|
||
void IRFileStorage::ensureDir(std::string path) noexcept { | ||
ensure_dir_exists(path); | ||
} | ||
|
||
} | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#pragma once | ||
#include "NativeModules.h" | ||
|
||
namespace winrt::reactotron::implementation | ||
{ | ||
REACT_MODULE(IRFileStorage) | ||
struct IRFileStorage | ||
{ | ||
IRFileStorage() noexcept = default; | ||
|
||
REACT_SYNC_METHOD(read) | ||
std::string read(std::string path) noexcept; | ||
|
||
REACT_METHOD(write) | ||
void write(std::string path, std::string data) noexcept; | ||
|
||
REACT_METHOD(remove) | ||
void remove(std::string path) noexcept; | ||
|
||
REACT_METHOD(ensureDir) | ||
void ensureDir(std::string path) noexcept; | ||
}; | ||
} | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import type { TurboModule } from "react-native" | ||
import { TurboModuleRegistry } from "react-native" | ||
|
||
export interface Spec extends TurboModule { | ||
read(path: string): string | ||
write(path: string, data: string): void | ||
remove(path: string): void | ||
ensureDir(path: string): void | ||
} | ||
|
||
export default TurboModuleRegistry.getEnforcing<Spec>("IRFileStorage") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: on windows, this would be better put temp in
C:\Users\Username\AppData\Local\Temp
. But for now this is okay.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should be able to utilize
std::filesystem::path temp = std::filesystem::temp_directory_path();
That'll get the path Josh is talking about (and also should be good on macOS also)https://en.cppreference.com/w/cpp/filesystem/temp_directory_path.html
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like this evaluates to
/var/folders/09/c8x8x89n75n02qyqvvybl54h0000gp/T/
on mac, are we alright with that? Seems fine to me, because i don't think people are really going to care about it, but figured i'd bring that up before i commit changesThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also @joshuayoes i believe the client information that you're seeing persisted is coming from the
standalone-server