Improved AsyncStorage merge function

Reviewed By: tadeuzagallo

Differential Revision: D2641817

fb-gh-sync-id: 0ba526ce21039ccdb979ac75c44d41c522c910ca
This commit is contained in:
Nick Lockwood 2015-11-11 08:58:07 -08:00 committed by facebook-github-bot-9
parent 12488da9cf
commit 5931089677
1 changed files with 20 additions and 19 deletions

View File

@ -84,31 +84,31 @@ static NSString *RCTGetManifestFilePath()
}
// Only merges objects - all other types are just clobbered (including arrays)
static void RCTMergeRecursive(NSMutableDictionary *destination, NSDictionary *source)
static BOOL RCTMergeRecursive(NSMutableDictionary *destination, NSDictionary *source)
{
BOOL modified = NO;
for (NSString *key in source) {
id sourceValue = source[key];
id destinationValue = destination[key];
if ([sourceValue isKindOfClass:[NSDictionary class]]) {
id destinationValue = destination[key];
NSMutableDictionary *nestedDestination;
if ([destinationValue classForCoder] == [NSMutableDictionary class]) {
nestedDestination = destinationValue;
} else {
if ([destinationValue isKindOfClass:[NSDictionary class]]) {
// Ideally we wouldn't eagerly copy here...
nestedDestination = [destinationValue mutableCopy];
} else {
destination[key] = [sourceValue copy];
if ([destinationValue isKindOfClass:[NSDictionary class]]) {
if ([destinationValue classForCoder] != [NSMutableDictionary class]) {
destinationValue = [destinationValue mutableCopy];
}
if (RCTMergeRecursive(destinationValue, sourceValue)) {
destination[key] = destinationValue;
modified = YES;
}
} else {
destination[key] = [sourceValue copy];
modified = YES;
}
if (nestedDestination) {
RCTMergeRecursive(nestedDestination, sourceValue);
destination[key] = nestedDestination;
}
} else {
destination[key] = sourceValue;
} else if (![source isEqual:destinationValue]) {
destination[key] = [sourceValue copy];
modified = YES;
}
}
return modified;
}
static dispatch_queue_t RCTGetMethodQueue()
@ -337,8 +337,9 @@ RCT_EXPORT_METHOD(multiMerge:(NSStringArrayArray *)kvPairs
if (value) {
NSError *jsonError;
NSMutableDictionary *mergedVal = RCTJSONParseMutable(value, &jsonError);
RCTMergeRecursive(mergedVal, RCTJSONParse(entry[1], &jsonError));
entry = @[entry[0], RCTNullIfNil(RCTJSONStringify(mergedVal, NULL))];
if (RCTMergeRecursive(mergedVal, RCTJSONParse(entry[1], &jsonError))) {
entry = @[entry[0], RCTNullIfNil(RCTJSONStringify(mergedVal, NULL))];
}
if (jsonError) {
keyError = RCTJSErrorFromNSError(jsonError);
}