Skip to content

Commit

Permalink
[Loader] Post a notification when the JS fails to load
Browse files Browse the repository at this point in the history
Summary:
This provides a way to get notified when a bridge fails to load JS, allowing apps to handle the error.

Closes #1085
Github Author: James Ide <ide@jameside.com>

Test Plan: run the UIExplorer app with the packager server not running, and verify that the notification is posted.
  • Loading branch information
ide committed May 7, 2015
1 parent 205c22b commit b8bf4d0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
5 changes: 5 additions & 0 deletions React/Base/RCTBridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ extern NSString *const RCTReloadNotification;
*/
extern NSString *const RCTJavaScriptDidLoadNotification;

/**
* This notification fires when the bridge failed to load.
*/
extern NSString *const RCTJavaScriptDidFailToLoadNotification;

/**
* This block can be used to instantiate modules that require additional
* init parameters, or additional configuration prior to being used.
Expand Down
14 changes: 12 additions & 2 deletions React/Base/RCTBridge.m
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

NSString *const RCTReloadNotification = @"RCTReloadNotification";
NSString *const RCTJavaScriptDidLoadNotification = @"RCTJavaScriptDidLoadNotification";
NSString *const RCTJavaScriptDidFailToLoadNotification = @"RCTJavaScriptDidFailToLoadNotification";

dispatch_queue_t const RCTJSThread = nil;

Expand Down Expand Up @@ -1082,16 +1083,18 @@ - (void)initJS

RCTJavaScriptLoader *loader = [[RCTJavaScriptLoader alloc] initWithBridge:self];
[loader loadBundleAtURL:bundleURL onComplete:^(NSError *error, NSString *script) {

_loading = NO;
if (!self.isValid) {
return;
}

RCTSourceCode *sourceCodeModule = self.modules[RCTBridgeModuleNameForClass([RCTSourceCode class])];
sourceCodeModule.scriptURL = bundleURL;
sourceCodeModule.scriptText = script;
if (error != nil) {
if (error) {

NSArray *stack = [[error userInfo] objectForKey:@"stack"];
NSArray *stack = [error userInfo][@"stack"];
if (stack) {
[[RCTRedBox sharedInstance] showErrorMessage:[error localizedDescription]
withStack:stack];
Expand All @@ -1100,10 +1103,17 @@ - (void)initJS
withDetails:[error localizedFailureReason]];
}

NSDictionary *userInfo = @{@"error": error};
[[NSNotificationCenter defaultCenter] postNotificationName:RCTJavaScriptDidFailToLoadNotification
object:self
userInfo:userInfo];

} else {

[self enqueueApplicationScript:script url:bundleURL onComplete:^(NSError *loadError) {

if (!loadError) {

/**
* Register the display link to start sending js calls after everything
* is setup
Expand Down

0 comments on commit b8bf4d0

Please sign in to comment.