2015-03-23 20:28:42 +00:00
|
|
|
/**
|
2018-09-11 22:27:47 +00:00
|
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
2015-03-23 20:28:42 +00:00
|
|
|
*
|
2018-02-17 02:24:55 +00:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2015-03-23 20:28:42 +00:00
|
|
|
*/
|
2015-03-11 19:06:03 +00:00
|
|
|
|
|
|
|
#import "RCTAsyncLocalStorage.h"
|
|
|
|
|
|
|
|
#import <Foundation/Foundation.h>
|
|
|
|
|
|
|
|
#import <CommonCrypto/CommonCryptor.h>
|
|
|
|
#import <CommonCrypto/CommonDigest.h>
|
|
|
|
|
2015-11-03 22:45:46 +00:00
|
|
|
#import "RCTConvert.h"
|
2015-03-11 19:06:03 +00:00
|
|
|
#import "RCTLog.h"
|
|
|
|
#import "RCTUtils.h"
|
|
|
|
|
2015-06-23 21:17:31 +00:00
|
|
|
static NSString *const RCTStorageDirectory = @"RCTAsyncLocalStorage_V1";
|
|
|
|
static NSString *const RCTManifestFileName = @"manifest.json";
|
2016-01-05 18:00:37 +00:00
|
|
|
static const NSUInteger RCTInlineValueThreshold = 1024;
|
2015-03-11 19:06:03 +00:00
|
|
|
|
|
|
|
#pragma mark - Static helper functions
|
|
|
|
|
2015-11-03 22:45:46 +00:00
|
|
|
static NSDictionary *RCTErrorForKey(NSString *key)
|
2015-03-11 19:06:03 +00:00
|
|
|
{
|
|
|
|
if (![key isKindOfClass:[NSString class]]) {
|
|
|
|
return RCTMakeAndLogError(@"Invalid key - must be a string. Key: ", key, @{@"key": key});
|
|
|
|
} else if (key.length < 1) {
|
|
|
|
return RCTMakeAndLogError(@"Invalid key - must be at least one character. Key: ", key, @{@"key": key});
|
|
|
|
} else {
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-03 22:45:46 +00:00
|
|
|
static void RCTAppendError(NSDictionary *error, NSMutableArray<NSDictionary *> **errors)
|
2015-03-11 19:06:03 +00:00
|
|
|
{
|
|
|
|
if (error && errors) {
|
|
|
|
if (!*errors) {
|
|
|
|
*errors = [NSMutableArray new];
|
|
|
|
}
|
|
|
|
[*errors addObject:error];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-03 22:45:46 +00:00
|
|
|
static NSString *RCTReadFile(NSString *filePath, NSString *key, NSDictionary **errorOut)
|
2015-03-11 19:06:03 +00:00
|
|
|
{
|
|
|
|
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
|
|
|
|
NSError *error;
|
|
|
|
NSStringEncoding encoding;
|
|
|
|
NSString *entryString = [NSString stringWithContentsOfFile:filePath usedEncoding:&encoding error:&error];
|
2017-09-28 16:22:16 +00:00
|
|
|
NSDictionary *extraData = @{@"key": RCTNullIfNil(key)};
|
|
|
|
|
2015-03-11 19:06:03 +00:00
|
|
|
if (error) {
|
2017-09-28 16:22:16 +00:00
|
|
|
if (errorOut) *errorOut = RCTMakeError(@"Failed to read storage file.", error, extraData);
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (encoding != NSUTF8StringEncoding) {
|
|
|
|
if (errorOut) *errorOut = RCTMakeError(@"Incorrect encoding of storage file: ", @(encoding), extraData);
|
|
|
|
return nil;
|
2015-03-11 19:06:03 +00:00
|
|
|
}
|
2017-09-28 16:22:16 +00:00
|
|
|
return entryString;
|
2015-03-11 19:06:03 +00:00
|
|
|
}
|
2017-09-28 16:22:16 +00:00
|
|
|
|
2015-03-11 19:06:03 +00:00
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
2015-06-23 21:17:31 +00:00
|
|
|
static NSString *RCTGetStorageDirectory()
|
2015-06-18 16:29:31 +00:00
|
|
|
{
|
2015-06-23 21:17:31 +00:00
|
|
|
static NSString *storageDirectory = nil;
|
|
|
|
static dispatch_once_t onceToken;
|
|
|
|
dispatch_once(&onceToken, ^{
|
2016-09-27 13:19:45 +00:00
|
|
|
#if TARGET_OS_TV
|
|
|
|
storageDirectory = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).firstObject;
|
|
|
|
#else
|
2015-08-24 10:14:33 +00:00
|
|
|
storageDirectory = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;
|
2016-09-27 13:19:45 +00:00
|
|
|
#endif
|
2015-06-23 21:17:31 +00:00
|
|
|
storageDirectory = [storageDirectory stringByAppendingPathComponent:RCTStorageDirectory];
|
|
|
|
});
|
|
|
|
return storageDirectory;
|
|
|
|
}
|
|
|
|
|
|
|
|
static NSString *RCTGetManifestFilePath()
|
|
|
|
{
|
|
|
|
static NSString *manifestFilePath = nil;
|
|
|
|
static dispatch_once_t onceToken;
|
|
|
|
dispatch_once(&onceToken, ^{
|
|
|
|
manifestFilePath = [RCTGetStorageDirectory() stringByAppendingPathComponent:RCTManifestFileName];
|
|
|
|
});
|
|
|
|
return manifestFilePath;
|
2015-06-18 16:29:31 +00:00
|
|
|
}
|
|
|
|
|
2015-06-03 23:57:08 +00:00
|
|
|
// Only merges objects - all other types are just clobbered (including arrays)
|
2015-11-11 16:58:07 +00:00
|
|
|
static BOOL RCTMergeRecursive(NSMutableDictionary *destination, NSDictionary *source)
|
2015-06-03 23:57:08 +00:00
|
|
|
{
|
2015-11-11 16:58:07 +00:00
|
|
|
BOOL modified = NO;
|
2015-06-03 23:57:08 +00:00
|
|
|
for (NSString *key in source) {
|
|
|
|
id sourceValue = source[key];
|
2015-11-11 16:58:07 +00:00
|
|
|
id destinationValue = destination[key];
|
2015-06-03 23:57:08 +00:00
|
|
|
if ([sourceValue isKindOfClass:[NSDictionary class]]) {
|
2015-11-11 16:58:07 +00:00
|
|
|
if ([destinationValue isKindOfClass:[NSDictionary class]]) {
|
|
|
|
if ([destinationValue classForCoder] != [NSMutableDictionary class]) {
|
|
|
|
destinationValue = [destinationValue mutableCopy];
|
2015-10-28 18:43:47 +00:00
|
|
|
}
|
2015-11-11 16:58:07 +00:00
|
|
|
if (RCTMergeRecursive(destinationValue, sourceValue)) {
|
|
|
|
destination[key] = destinationValue;
|
|
|
|
modified = YES;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
destination[key] = [sourceValue copy];
|
|
|
|
modified = YES;
|
2015-06-03 23:57:08 +00:00
|
|
|
}
|
2015-11-11 16:58:07 +00:00
|
|
|
} else if (![source isEqual:destinationValue]) {
|
|
|
|
destination[key] = [sourceValue copy];
|
|
|
|
modified = YES;
|
2015-06-03 23:57:08 +00:00
|
|
|
}
|
|
|
|
}
|
2015-11-11 16:58:07 +00:00
|
|
|
return modified;
|
2015-06-03 23:57:08 +00:00
|
|
|
}
|
|
|
|
|
2015-06-22 21:37:11 +00:00
|
|
|
static dispatch_queue_t RCTGetMethodQueue()
|
|
|
|
{
|
|
|
|
// We want all instances to share the same queue since they will be reading/writing the same files.
|
|
|
|
static dispatch_queue_t queue;
|
|
|
|
static dispatch_once_t onceToken;
|
|
|
|
dispatch_once(&onceToken, ^{
|
2016-04-01 14:01:51 +00:00
|
|
|
queue = dispatch_queue_create("com.facebook.react.AsyncLocalStorageQueue", DISPATCH_QUEUE_SERIAL);
|
2015-06-22 21:37:11 +00:00
|
|
|
});
|
|
|
|
return queue;
|
|
|
|
}
|
|
|
|
|
2015-11-14 17:17:40 +00:00
|
|
|
static NSCache *RCTGetCache()
|
|
|
|
{
|
|
|
|
// We want all instances to share the same cache since they will be reading/writing the same files.
|
|
|
|
static NSCache *cache;
|
|
|
|
static dispatch_once_t onceToken;
|
|
|
|
dispatch_once(&onceToken, ^{
|
|
|
|
cache = [NSCache new];
|
|
|
|
cache.totalCostLimit = 2 * 1024 * 1024; // 2MB
|
|
|
|
|
|
|
|
// Clear cache in the event of a memory warning
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationDidReceiveMemoryWarningNotification object:nil queue:nil usingBlock:^(__unused NSNotification *note) {
|
|
|
|
[cache removeAllObjects];
|
|
|
|
}];
|
|
|
|
});
|
|
|
|
return cache;
|
|
|
|
}
|
|
|
|
|
2015-06-22 21:37:11 +00:00
|
|
|
static BOOL RCTHasCreatedStorageDirectory = NO;
|
2015-11-05 04:04:29 +00:00
|
|
|
static NSDictionary *RCTDeleteStorageDirectory()
|
2015-06-22 21:37:11 +00:00
|
|
|
{
|
|
|
|
NSError *error;
|
2015-06-23 21:17:31 +00:00
|
|
|
[[NSFileManager defaultManager] removeItemAtPath:RCTGetStorageDirectory() error:&error];
|
2015-06-22 21:37:11 +00:00
|
|
|
RCTHasCreatedStorageDirectory = NO;
|
2015-11-05 04:04:29 +00:00
|
|
|
return error ? RCTMakeError(@"Failed to delete storage directory.", error, nil) : nil;
|
2015-06-22 21:37:11 +00:00
|
|
|
}
|
|
|
|
|
2015-03-11 19:06:03 +00:00
|
|
|
#pragma mark - RCTAsyncLocalStorage
|
|
|
|
|
|
|
|
@implementation RCTAsyncLocalStorage
|
|
|
|
{
|
|
|
|
BOOL _haveSetup;
|
|
|
|
// The manifest is a dictionary of all keys with small values inlined. Null values indicate values that are stored
|
|
|
|
// in separate files (as opposed to nil values which don't exist). The manifest is read off disk at startup, and
|
|
|
|
// written to disk after all mutations.
|
2015-11-14 17:17:40 +00:00
|
|
|
NSMutableDictionary<NSString *, NSString *> *_manifest;
|
2015-03-11 19:06:03 +00:00
|
|
|
}
|
|
|
|
|
2015-04-08 12:42:43 +00:00
|
|
|
RCT_EXPORT_MODULE()
|
|
|
|
|
2015-04-18 17:43:20 +00:00
|
|
|
- (dispatch_queue_t)methodQueue
|
|
|
|
{
|
2015-06-22 21:37:11 +00:00
|
|
|
return RCTGetMethodQueue();
|
2015-06-18 16:29:31 +00:00
|
|
|
}
|
|
|
|
|
2015-11-03 22:45:46 +00:00
|
|
|
- (void)clearAllData
|
|
|
|
{
|
|
|
|
dispatch_async(RCTGetMethodQueue(), ^{
|
2016-07-07 19:36:56 +00:00
|
|
|
[self->_manifest removeAllObjects];
|
2015-11-14 17:17:40 +00:00
|
|
|
[RCTGetCache() removeAllObjects];
|
2015-11-03 22:45:46 +00:00
|
|
|
RCTDeleteStorageDirectory();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-06-22 21:37:11 +00:00
|
|
|
+ (void)clearAllData
|
2015-06-18 16:29:31 +00:00
|
|
|
{
|
2015-06-22 21:37:11 +00:00
|
|
|
dispatch_async(RCTGetMethodQueue(), ^{
|
2015-11-14 17:17:40 +00:00
|
|
|
[RCTGetCache() removeAllObjects];
|
2015-06-22 21:37:11 +00:00
|
|
|
RCTDeleteStorageDirectory();
|
|
|
|
});
|
2015-06-18 16:29:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)invalidate
|
|
|
|
{
|
|
|
|
if (_clearOnInvalidate) {
|
2015-11-14 17:17:40 +00:00
|
|
|
[RCTGetCache() removeAllObjects];
|
2015-06-22 21:37:11 +00:00
|
|
|
RCTDeleteStorageDirectory();
|
2015-06-18 16:29:31 +00:00
|
|
|
}
|
|
|
|
_clearOnInvalidate = NO;
|
2015-11-14 17:17:40 +00:00
|
|
|
[_manifest removeAllObjects];
|
2015-06-18 16:29:31 +00:00
|
|
|
_haveSetup = NO;
|
|
|
|
}
|
2015-08-14 08:59:42 +00:00
|
|
|
|
2015-06-18 16:29:31 +00:00
|
|
|
- (BOOL)isValid
|
|
|
|
{
|
|
|
|
return _haveSetup;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)dealloc
|
|
|
|
{
|
|
|
|
[self invalidate];
|
2015-04-18 17:43:20 +00:00
|
|
|
}
|
|
|
|
|
2015-03-11 19:06:03 +00:00
|
|
|
- (NSString *)_filePathForKey:(NSString *)key
|
|
|
|
{
|
|
|
|
NSString *safeFileName = RCTMD5Hash(key);
|
2015-06-23 21:17:31 +00:00
|
|
|
return [RCTGetStorageDirectory() stringByAppendingPathComponent:safeFileName];
|
2015-03-11 19:06:03 +00:00
|
|
|
}
|
|
|
|
|
2015-11-03 22:45:46 +00:00
|
|
|
- (NSDictionary *)_ensureSetup
|
2015-03-11 19:06:03 +00:00
|
|
|
{
|
2015-06-22 21:37:11 +00:00
|
|
|
RCTAssertThread(RCTGetMethodQueue(), @"Must be executed on storage thread");
|
|
|
|
|
2016-09-27 13:19:45 +00:00
|
|
|
#if TARGET_OS_TV
|
2016-11-16 15:28:01 +00:00
|
|
|
RCTLogWarn(@"Persistent storage is not supported on tvOS, your data may be removed at any point.");
|
2016-09-27 13:19:45 +00:00
|
|
|
#endif
|
|
|
|
|
2015-06-22 21:37:11 +00:00
|
|
|
NSError *error = nil;
|
|
|
|
if (!RCTHasCreatedStorageDirectory) {
|
2015-06-23 21:17:31 +00:00
|
|
|
[[NSFileManager defaultManager] createDirectoryAtPath:RCTGetStorageDirectory()
|
2015-06-22 21:37:11 +00:00
|
|
|
withIntermediateDirectories:YES
|
|
|
|
attributes:nil
|
|
|
|
error:&error];
|
|
|
|
if (error) {
|
|
|
|
return RCTMakeError(@"Failed to create storage directory.", error, nil);
|
|
|
|
}
|
|
|
|
RCTHasCreatedStorageDirectory = YES;
|
2015-03-11 19:06:03 +00:00
|
|
|
}
|
2015-06-22 21:37:11 +00:00
|
|
|
if (!_haveSetup) {
|
|
|
|
NSDictionary *errorOut;
|
2017-09-28 16:22:16 +00:00
|
|
|
NSString *serialized = RCTReadFile(RCTGetManifestFilePath(), RCTManifestFileName, &errorOut);
|
2015-11-03 22:45:46 +00:00
|
|
|
_manifest = serialized ? RCTJSONParseMutable(serialized, &error) : [NSMutableDictionary new];
|
2015-06-22 21:37:11 +00:00
|
|
|
if (error) {
|
|
|
|
RCTLogWarn(@"Failed to parse manifest - creating new one.\n\n%@", error);
|
2015-08-17 14:35:34 +00:00
|
|
|
_manifest = [NSMutableDictionary new];
|
2015-06-22 21:37:11 +00:00
|
|
|
}
|
|
|
|
_haveSetup = YES;
|
2015-03-11 19:06:03 +00:00
|
|
|
}
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
2015-11-03 22:45:46 +00:00
|
|
|
- (NSDictionary *)_writeManifest:(NSMutableArray<NSDictionary *> **)errors
|
2015-03-11 19:06:03 +00:00
|
|
|
{
|
|
|
|
NSError *error;
|
|
|
|
NSString *serialized = RCTJSONStringify(_manifest, &error);
|
2015-06-23 21:17:31 +00:00
|
|
|
[serialized writeToFile:RCTGetManifestFilePath() atomically:YES encoding:NSUTF8StringEncoding error:&error];
|
2015-11-03 22:45:46 +00:00
|
|
|
NSDictionary *errorOut;
|
2015-03-11 19:06:03 +00:00
|
|
|
if (error) {
|
|
|
|
errorOut = RCTMakeError(@"Failed to write manifest file.", error, nil);
|
|
|
|
RCTAppendError(errorOut, errors);
|
|
|
|
}
|
|
|
|
return errorOut;
|
|
|
|
}
|
|
|
|
|
2015-11-03 22:45:46 +00:00
|
|
|
- (NSDictionary *)_appendItemForKey:(NSString *)key
|
|
|
|
toArray:(NSMutableArray<NSArray<NSString *> *> *)result
|
2015-10-28 18:43:47 +00:00
|
|
|
{
|
2015-11-03 22:45:46 +00:00
|
|
|
NSDictionary *errorOut = RCTErrorForKey(key);
|
2015-10-28 18:43:47 +00:00
|
|
|
if (errorOut) {
|
|
|
|
return errorOut;
|
|
|
|
}
|
2015-11-03 22:45:46 +00:00
|
|
|
NSString *value = [self _getValueForKey:key errorOut:&errorOut];
|
2015-10-28 18:43:47 +00:00
|
|
|
[result addObject:@[key, RCTNullIfNil(value)]]; // Insert null if missing or failure.
|
|
|
|
return errorOut;
|
|
|
|
}
|
|
|
|
|
2015-06-03 23:57:08 +00:00
|
|
|
- (NSString *)_getValueForKey:(NSString *)key errorOut:(NSDictionary **)errorOut
|
|
|
|
{
|
2015-11-14 17:17:40 +00:00
|
|
|
NSString *value = _manifest[key]; // nil means missing, null means there may be a data file, else: NSString
|
2015-06-12 18:05:01 +00:00
|
|
|
if (value == (id)kCFNull) {
|
2015-11-14 17:17:40 +00:00
|
|
|
value = [RCTGetCache() objectForKey:key];
|
|
|
|
if (!value) {
|
|
|
|
NSString *filePath = [self _filePathForKey:key];
|
|
|
|
value = RCTReadFile(filePath, key, errorOut);
|
|
|
|
if (value) {
|
|
|
|
[RCTGetCache() setObject:value forKey:key cost:value.length];
|
|
|
|
} else {
|
|
|
|
// file does not exist after all, so remove from manifest (no need to save
|
|
|
|
// manifest immediately though, as cost of checking again next time is negligible)
|
|
|
|
[_manifest removeObjectForKey:key];
|
|
|
|
}
|
|
|
|
}
|
2015-03-11 19:06:03 +00:00
|
|
|
}
|
2015-06-03 23:57:08 +00:00
|
|
|
return value;
|
2015-03-11 19:06:03 +00:00
|
|
|
}
|
|
|
|
|
2015-11-14 17:17:40 +00:00
|
|
|
- (NSDictionary *)_writeEntry:(NSArray<NSString *> *)entry changedManifest:(BOOL *)changedManifest
|
2015-03-11 19:06:03 +00:00
|
|
|
{
|
2015-11-03 22:45:46 +00:00
|
|
|
if (entry.count != 2) {
|
2015-03-11 19:06:03 +00:00
|
|
|
return RCTMakeAndLogError(@"Entries must be arrays of the form [key: string, value: string], got: ", entry, nil);
|
|
|
|
}
|
|
|
|
NSString *key = entry[0];
|
2015-11-03 22:45:46 +00:00
|
|
|
NSDictionary *errorOut = RCTErrorForKey(key);
|
2015-03-11 19:06:03 +00:00
|
|
|
if (errorOut) {
|
|
|
|
return errorOut;
|
|
|
|
}
|
|
|
|
NSString *value = entry[1];
|
|
|
|
NSString *filePath = [self _filePathForKey:key];
|
|
|
|
NSError *error;
|
2015-06-23 21:17:31 +00:00
|
|
|
if (value.length <= RCTInlineValueThreshold) {
|
2015-11-14 17:17:40 +00:00
|
|
|
if (_manifest[key] == (id)kCFNull) {
|
2015-03-11 19:06:03 +00:00
|
|
|
// If the value already existed but wasn't inlined, remove the old file.
|
|
|
|
[[NSFileManager defaultManager] removeItemAtPath:filePath error:nil];
|
2015-11-14 17:17:40 +00:00
|
|
|
[RCTGetCache() removeObjectForKey:key];
|
2015-03-11 19:06:03 +00:00
|
|
|
}
|
2015-11-14 17:17:40 +00:00
|
|
|
*changedManifest = YES;
|
2015-03-11 19:06:03 +00:00
|
|
|
_manifest[key] = value;
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
[value writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:&error];
|
2015-11-14 17:17:40 +00:00
|
|
|
[RCTGetCache() setObject:value forKey:key cost:value.length];
|
2015-03-11 19:06:03 +00:00
|
|
|
if (error) {
|
|
|
|
errorOut = RCTMakeError(@"Failed to write value.", error, @{@"key": key});
|
2015-11-14 17:17:40 +00:00
|
|
|
} else if (_manifest[key] != (id)kCFNull) {
|
|
|
|
*changedManifest = YES;
|
|
|
|
_manifest[key] = (id)kCFNull;
|
2015-03-11 19:06:03 +00:00
|
|
|
}
|
|
|
|
return errorOut;
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - Exported JS Functions
|
|
|
|
|
2015-12-10 18:09:04 +00:00
|
|
|
RCT_EXPORT_METHOD(multiGet:(NSArray<NSString *> *)keys
|
2015-04-08 15:52:48 +00:00
|
|
|
callback:(RCTResponseSenderBlock)callback)
|
2015-03-11 19:06:03 +00:00
|
|
|
{
|
2015-11-03 22:45:46 +00:00
|
|
|
NSDictionary *errorOut = [self _ensureSetup];
|
2015-04-18 17:43:20 +00:00
|
|
|
if (errorOut) {
|
2015-06-12 18:05:01 +00:00
|
|
|
callback(@[@[errorOut], (id)kCFNull]);
|
2015-04-18 17:43:20 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-11-03 22:45:46 +00:00
|
|
|
NSMutableArray<NSDictionary *> *errors;
|
|
|
|
NSMutableArray<NSArray<NSString *> *> *result = [[NSMutableArray alloc] initWithCapacity:keys.count];
|
2015-04-18 17:43:20 +00:00
|
|
|
for (NSString *key in keys) {
|
2015-11-14 17:17:40 +00:00
|
|
|
id keyError;
|
|
|
|
id value = [self _getValueForKey:key errorOut:&keyError];
|
|
|
|
[result addObject:@[key, RCTNullIfNil(value)]];
|
2015-04-18 17:43:20 +00:00
|
|
|
RCTAppendError(keyError, &errors);
|
|
|
|
}
|
2015-06-12 18:05:01 +00:00
|
|
|
callback(@[RCTNullIfNil(errors), result]);
|
2015-03-11 19:06:03 +00:00
|
|
|
}
|
|
|
|
|
2015-12-10 18:09:04 +00:00
|
|
|
RCT_EXPORT_METHOD(multiSet:(NSArray<NSArray<NSString *> *> *)kvPairs
|
2015-04-08 15:52:48 +00:00
|
|
|
callback:(RCTResponseSenderBlock)callback)
|
2015-03-11 19:06:03 +00:00
|
|
|
{
|
2015-11-03 22:45:46 +00:00
|
|
|
NSDictionary *errorOut = [self _ensureSetup];
|
2015-04-18 17:43:20 +00:00
|
|
|
if (errorOut) {
|
|
|
|
callback(@[@[errorOut]]);
|
|
|
|
return;
|
|
|
|
}
|
2015-11-14 17:17:40 +00:00
|
|
|
BOOL changedManifest = NO;
|
2015-11-03 22:45:46 +00:00
|
|
|
NSMutableArray<NSDictionary *> *errors;
|
|
|
|
for (NSArray<NSString *> *entry in kvPairs) {
|
2015-11-14 17:17:40 +00:00
|
|
|
NSDictionary *keyError = [self _writeEntry:entry changedManifest:&changedManifest];
|
2015-04-18 17:43:20 +00:00
|
|
|
RCTAppendError(keyError, &errors);
|
|
|
|
}
|
2015-11-14 17:17:40 +00:00
|
|
|
if (changedManifest) {
|
|
|
|
[self _writeManifest:&errors];
|
|
|
|
}
|
2015-11-03 22:45:46 +00:00
|
|
|
callback(@[RCTNullIfNil(errors)]);
|
2015-03-11 19:06:03 +00:00
|
|
|
}
|
|
|
|
|
2015-12-10 18:09:04 +00:00
|
|
|
RCT_EXPORT_METHOD(multiMerge:(NSArray<NSArray<NSString *> *> *)kvPairs
|
2015-11-03 22:45:46 +00:00
|
|
|
callback:(RCTResponseSenderBlock)callback)
|
2015-06-03 23:57:08 +00:00
|
|
|
{
|
2015-11-03 22:45:46 +00:00
|
|
|
NSDictionary *errorOut = [self _ensureSetup];
|
2015-06-03 23:57:08 +00:00
|
|
|
if (errorOut) {
|
|
|
|
callback(@[@[errorOut]]);
|
|
|
|
return;
|
|
|
|
}
|
2015-11-14 17:17:40 +00:00
|
|
|
BOOL changedManifest = NO;
|
2015-11-03 22:45:46 +00:00
|
|
|
NSMutableArray<NSDictionary *> *errors;
|
|
|
|
for (__strong NSArray<NSString *> *entry in kvPairs) {
|
|
|
|
NSDictionary *keyError;
|
2015-06-03 23:57:08 +00:00
|
|
|
NSString *value = [self _getValueForKey:entry[0] errorOut:&keyError];
|
2015-11-14 17:17:40 +00:00
|
|
|
if (!keyError) {
|
2015-06-03 23:57:08 +00:00
|
|
|
if (value) {
|
2015-11-03 22:45:46 +00:00
|
|
|
NSError *jsonError;
|
|
|
|
NSMutableDictionary *mergedVal = RCTJSONParseMutable(value, &jsonError);
|
2015-11-11 16:58:07 +00:00
|
|
|
if (RCTMergeRecursive(mergedVal, RCTJSONParse(entry[1], &jsonError))) {
|
|
|
|
entry = @[entry[0], RCTNullIfNil(RCTJSONStringify(mergedVal, NULL))];
|
|
|
|
}
|
2015-11-03 22:45:46 +00:00
|
|
|
if (jsonError) {
|
|
|
|
keyError = RCTJSErrorFromNSError(jsonError);
|
|
|
|
}
|
2015-10-28 18:43:47 +00:00
|
|
|
}
|
|
|
|
if (!keyError) {
|
2015-11-14 17:17:40 +00:00
|
|
|
keyError = [self _writeEntry:entry changedManifest:&changedManifest];
|
2015-06-03 23:57:08 +00:00
|
|
|
}
|
|
|
|
}
|
2015-11-14 17:17:40 +00:00
|
|
|
RCTAppendError(keyError, &errors);
|
|
|
|
}
|
|
|
|
if (changedManifest) {
|
|
|
|
[self _writeManifest:&errors];
|
2015-06-03 23:57:08 +00:00
|
|
|
}
|
2015-11-03 22:45:46 +00:00
|
|
|
callback(@[RCTNullIfNil(errors)]);
|
2015-06-03 23:57:08 +00:00
|
|
|
}
|
|
|
|
|
2015-12-10 18:09:04 +00:00
|
|
|
RCT_EXPORT_METHOD(multiRemove:(NSArray<NSString *> *)keys
|
2015-04-08 15:52:48 +00:00
|
|
|
callback:(RCTResponseSenderBlock)callback)
|
2015-03-11 19:06:03 +00:00
|
|
|
{
|
2015-11-03 22:45:46 +00:00
|
|
|
NSDictionary *errorOut = [self _ensureSetup];
|
2015-04-18 17:43:20 +00:00
|
|
|
if (errorOut) {
|
|
|
|
callback(@[@[errorOut]]);
|
|
|
|
return;
|
|
|
|
}
|
2015-11-03 22:45:46 +00:00
|
|
|
NSMutableArray<NSDictionary *> *errors;
|
2015-11-14 17:17:40 +00:00
|
|
|
BOOL changedManifest = NO;
|
2015-04-18 17:43:20 +00:00
|
|
|
for (NSString *key in keys) {
|
2015-11-03 22:45:46 +00:00
|
|
|
NSDictionary *keyError = RCTErrorForKey(key);
|
2015-04-18 17:43:20 +00:00
|
|
|
if (!keyError) {
|
2015-11-14 17:17:40 +00:00
|
|
|
if (_manifest[key] == (id)kCFNull) {
|
|
|
|
NSString *filePath = [self _filePathForKey:key];
|
|
|
|
[[NSFileManager defaultManager] removeItemAtPath:filePath error:nil];
|
|
|
|
[RCTGetCache() removeObjectForKey:key];
|
|
|
|
// remove the key from manifest, but no need to mark as changed just for
|
|
|
|
// this, as the cost of checking again next time is negligible.
|
|
|
|
[_manifest removeObjectForKey:key];
|
|
|
|
} else if (_manifest[key]) {
|
|
|
|
changedManifest = YES;
|
|
|
|
[_manifest removeObjectForKey:key];
|
|
|
|
}
|
2015-03-11 19:06:03 +00:00
|
|
|
}
|
2015-04-18 17:43:20 +00:00
|
|
|
RCTAppendError(keyError, &errors);
|
|
|
|
}
|
2015-11-14 17:17:40 +00:00
|
|
|
if (changedManifest) {
|
|
|
|
[self _writeManifest:&errors];
|
|
|
|
}
|
2015-11-03 22:45:46 +00:00
|
|
|
callback(@[RCTNullIfNil(errors)]);
|
2015-03-11 19:06:03 +00:00
|
|
|
}
|
|
|
|
|
2015-04-08 15:52:48 +00:00
|
|
|
RCT_EXPORT_METHOD(clear:(RCTResponseSenderBlock)callback)
|
2015-03-11 19:06:03 +00:00
|
|
|
{
|
2015-11-14 17:17:40 +00:00
|
|
|
[_manifest removeAllObjects];
|
|
|
|
[RCTGetCache() removeAllObjects];
|
2015-11-05 04:04:29 +00:00
|
|
|
NSDictionary *error = RCTDeleteStorageDirectory();
|
2015-11-03 22:45:46 +00:00
|
|
|
callback(@[RCTNullIfNil(error)]);
|
2015-03-11 19:06:03 +00:00
|
|
|
}
|
|
|
|
|
2015-04-08 15:52:48 +00:00
|
|
|
RCT_EXPORT_METHOD(getAllKeys:(RCTResponseSenderBlock)callback)
|
2015-03-11 19:06:03 +00:00
|
|
|
{
|
2015-11-03 22:45:46 +00:00
|
|
|
NSDictionary *errorOut = [self _ensureSetup];
|
2015-04-18 17:43:20 +00:00
|
|
|
if (errorOut) {
|
2015-06-12 18:05:01 +00:00
|
|
|
callback(@[errorOut, (id)kCFNull]);
|
2015-04-18 17:43:20 +00:00
|
|
|
} else {
|
2015-08-24 10:14:33 +00:00
|
|
|
callback(@[(id)kCFNull, _manifest.allKeys]);
|
2015-04-18 17:43:20 +00:00
|
|
|
}
|
2015-03-11 19:06:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|