Skip to content

Commit

Permalink
Use promises in 'SourceCodeModule'
Browse files Browse the repository at this point in the history
Summary: Closes facebook#5504

Reviewed By: svcscm

Differential Revision: D2861158

Pulled By: dmmiller

fb-gh-sync-id: 3e9c257288539183f6156b8d360b54dc570bc7ad
  • Loading branch information
satya164 authored and facebook-github-bot-9 committed Jan 25, 2016
1 parent ea1aec3 commit 0007bff
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ function fetchSourceMap(): Promise {
return Promise.reject(new Error('RCTNetworking module is not available'));
}

return new Promise(RCTSourceCode.getScriptText)
return RCTSourceCode.getScriptText()
.then(extractSourceMapURL)
.then((url) => {
if (url === null) {
Expand Down
10 changes: 6 additions & 4 deletions React/Modules/RCTSourceCode.m
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,17 @@ @implementation RCTSourceCode
- (void)setScriptText:(NSString *)scriptText {}
#endif

RCT_EXPORT_METHOD(getScriptText:(RCTResponseSenderBlock)successCallback
failureCallback:(RCTResponseErrorBlock)failureCallback)
NSString *const RCTErrorUnavailable = @"E_SOURCE_CODE_UNAVAILABLE";

RCT_EXPORT_METHOD(getScriptText:(RCTPromiseResolveBlock)resolve
reject:(RCTPromiseRejectBlock)reject)
{
if (RCT_DEV && self.scriptData && self.scriptURL) {
NSString *scriptText = [[NSString alloc] initWithData:self.scriptData encoding:NSUTF8StringEncoding];

successCallback(@[@{@"text": scriptText, @"url": self.scriptURL.absoluteString}]);
resolve(@[@{@"text": scriptText, @"url": self.scriptURL.absoluteString}]);
} else {
failureCallback(RCTErrorWithMessage(@"Source code is not available"));
reject(RCTErrorUnavailable, nil, RCTErrorWithMessage(@"Source code is not available"));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import java.util.HashMap;
import java.util.Map;

import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.BaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.WritableMap;
Expand All @@ -35,14 +35,14 @@ public SourceCodeModule(String sourceUrl, String sourceMapUrl) {

@Override
public String getName() {
return "RKSourceCode";
return "RCTSourceCode";
}

@ReactMethod
public void getScriptText(final Callback onSuccess, final Callback onError) {
public void getScriptText(final Promise promise) {
WritableMap map = new WritableNativeMap();
map.putString("fullSourceMappingURL", mSourceMapUrl);
onSuccess.invoke(map);
promise.resolve(map);
}

@Override
Expand Down

0 comments on commit 0007bff

Please sign in to comment.