From 2c6875effa58565d31994eb94137ee755d890e89 Mon Sep 17 00:00:00 2001 From: Dave Miller Date: Wed, 10 Feb 2016 07:24:38 -0800 Subject: [PATCH] Fix promises on iOS to no longer wrap values in Arrays Summary: public In https://github.com/facebook/react-native/commit/9baff8f437eee49f8ab0e6f433bf86466ca16662#diff-8d9841e5b53fd6c9cf3a7f431827e319R331, I incorrectly assumed that iOS was wrapping promises in an extra Array. What was really happening is that all the callers were doing this. I removed the wrapping in the callers and the special case handling MessageQueue. Now one can pass whatever object one wants to resolve and it will show properly in the resolve call on the js side. This fixes issue https://github.com/facebook/react-native/issues/5851 Reviewed By: nicklockwood Differential Revision: D2921565 fb-gh-sync-id: 9f81e2a87f6a48e9197413b843e452db345a7ff9 shipit-source-id: 9f81e2a87f6a48e9197413b843e452db345a7ff9 --- Libraries/CameraRoll/RCTCameraRollManager.m | 10 +++++----- Libraries/LinkingIOS/RCTLinkingManager.m | 6 +++--- Libraries/Utilities/MessageQueue.js | 6 +----- React/Modules/RCTClipboard.m | 2 +- React/Modules/RCTSourceCode.m | 2 +- 5 files changed, 11 insertions(+), 15 deletions(-) diff --git a/Libraries/CameraRoll/RCTCameraRollManager.m b/Libraries/CameraRoll/RCTCameraRollManager.m index fc97c9493b4..d060224ccc7 100644 --- a/Libraries/CameraRoll/RCTCameraRollManager.m +++ b/Libraries/CameraRoll/RCTCameraRollManager.m @@ -98,7 +98,7 @@ RCT_EXPORT_METHOD(saveImageWithTag:(NSString *)imageTag RCTLogWarn(@"Error saving cropped image: %@", saveError); reject(RCTErrorUnableToSave, nil, saveError); } else { - resolve(@[assetURL.absoluteString]); + resolve(assetURL.absoluteString); } }]; }); @@ -110,22 +110,22 @@ static void RCTResolvePromise(RCTPromiseResolveBlock resolve, BOOL hasNextPage) { if (!assets.count) { - resolve(@[@{ + resolve(@{ @"edges": assets, @"page_info": @{ @"has_next_page": @NO, } - }]); + }); return; } - resolve(@[@{ + resolve(@{ @"edges": assets, @"page_info": @{ @"start_cursor": assets[0][@"node"][@"image"][@"uri"], @"end_cursor": assets[assets.count - 1][@"node"][@"image"][@"uri"], @"has_next_page": @(hasNextPage), } - }]); + }); } RCT_EXPORT_METHOD(getPhotos:(NSDictionary *)params diff --git a/Libraries/LinkingIOS/RCTLinkingManager.m b/Libraries/LinkingIOS/RCTLinkingManager.m index d720631674c..b8bc364bd8b 100644 --- a/Libraries/LinkingIOS/RCTLinkingManager.m +++ b/Libraries/LinkingIOS/RCTLinkingManager.m @@ -80,7 +80,7 @@ RCT_EXPORT_METHOD(openURL:(NSURL *)URL // TODO: we should really report success/failure via the promise here // Doesn't really matter what thread we call this on since it exits the app [RCTSharedApplication() openURL:URL]; - resolve(@[@YES]); + resolve(@YES); } RCT_EXPORT_METHOD(canOpenURL:(NSURL *)URL @@ -90,13 +90,13 @@ RCT_EXPORT_METHOD(canOpenURL:(NSURL *)URL if (RCTRunningInAppExtension()) { // Technically Today widgets can open urls, but supporting that would require // a reference to the NSExtensionContext - resolve(@[@NO]); + resolve(@NO); return; } // This can be expensive, so we deliberately don't call on main thread BOOL canOpen = [RCTSharedApplication() canOpenURL:URL]; - resolve(@[@(canOpen)]); + resolve(@(canOpen)); } @end diff --git a/Libraries/Utilities/MessageQueue.js b/Libraries/Utilities/MessageQueue.js index 8e58ee06e3c..7b31c114914 100644 --- a/Libraries/Utilities/MessageQueue.js +++ b/Libraries/Utilities/MessageQueue.js @@ -324,11 +324,7 @@ class MessageQueue { method, args, (data) => { - // iOS always wraps the data in an Array regardless of what the - // shape of the data so we strip it out - // Android sends the data back properly - // TODO: Remove this once iOS has support for Promises natively (t9774697) - resolve(Platform.OS == 'ios' ? data[0] : data); + resolve(data); }, (errorData) => { var error = createErrorFromErrorData(errorData); diff --git a/React/Modules/RCTClipboard.m b/React/Modules/RCTClipboard.m index 0a0bf14816f..cad23d58b4e 100644 --- a/React/Modules/RCTClipboard.m +++ b/React/Modules/RCTClipboard.m @@ -33,7 +33,7 @@ RCT_EXPORT_METHOD(getString:(RCTPromiseResolveBlock)resolve rejecter:(__unused RCTPromiseRejectBlock)reject) { UIPasteboard *clipboard = [UIPasteboard generalPasteboard]; - resolve(@[RCTNullIfNil(clipboard.string)]); + resolve(RCTNullIfNil(clipboard.string)); } @end diff --git a/React/Modules/RCTSourceCode.m b/React/Modules/RCTSourceCode.m index e538fa5d0c9..f8d96fac68e 100644 --- a/React/Modules/RCTSourceCode.m +++ b/React/Modules/RCTSourceCode.m @@ -32,7 +32,7 @@ RCT_EXPORT_METHOD(getScriptText:(RCTPromiseResolveBlock)resolve if (RCT_DEV && self.scriptData && self.scriptURL) { NSString *scriptText = [[NSString alloc] initWithData:self.scriptData encoding:NSUTF8StringEncoding]; - resolve(@[@{@"text": scriptText, @"url": self.scriptURL.absoluteString}]); + resolve(@{@"text": scriptText, @"url": self.scriptURL.absoluteString}); } else { reject(RCTErrorUnavailable, nil, RCTErrorWithMessage(@"Source code is not available")); }