Skip to content

Commit f255bda

Browse files
javacheFacebook Github Bot
authored andcommitted
Fix loop when websocket executor is used without packager present
Reviewed By: mhorowitz Differential Revision: D4159911 fbshipit-source-id: db913704641daf055060f5fe4561479daf76cd5a
1 parent d63ba47 commit f255bda

File tree

2 files changed

+31
-19
lines changed

2 files changed

+31
-19
lines changed

Libraries/WebSocket/RCTWebSocketExecutor.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99

1010
#import "RCTDefines.h"
1111

12-
#if RCT_DEV // Debug executors are only supported in dev mode
13-
1412
#import "RCTJavaScriptExecutor.h"
1513

14+
#if RCT_DEV // Debug executors are only supported in dev mode
15+
1616
@interface RCTWebSocketExecutor : NSObject <RCTJavaScriptExecutor>
1717

1818
- (instancetype)initWithURL:(NSURL *)URL;

Libraries/WebSocket/RCTWebSocketExecutor.m

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,17 @@
99

1010
#import "RCTDefines.h"
1111

12-
#if RCT_DEV // Debug executors are only supported in dev mode
13-
1412
#import "RCTWebSocketExecutor.h"
1513

14+
#import "RCTAssert.h"
1615
#import "RCTBridge.h"
1716
#import "RCTConvert.h"
1817
#import "RCTLog.h"
1918
#import "RCTUtils.h"
2019
#import "RCTSRWebSocket.h"
2120

21+
#if RCT_DEV // Debug executors are only supported in dev mode
22+
2223
typedef void (^RCTWSMessageCallback)(NSError *error, NSDictionary<NSString *, id> *reply);
2324

2425
@interface RCTWebSocketExecutor () <RCTSRWebSocketDelegate>
@@ -33,6 +34,7 @@ @implementation RCTWebSocketExecutor
3334
dispatch_semaphore_t _socketOpenSemaphore;
3435
NSMutableDictionary<NSString *, NSString *> *_injectedObjects;
3536
NSURL *_url;
37+
NSError *_setupError;
3638
}
3739

3840
RCT_EXPORT_MODULE()
@@ -81,10 +83,12 @@ - (void)setUp
8183
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error){}];
8284
[dataTask resume];
8385
if (![self connectToProxy]) {
84-
RCTLogError(@"Connection to %@ timed out. Are you running node proxy? If "
85-
"you are running on the device, check if you have the right IP "
86-
"address in `RCTWebSocketExecutor.m`.", _url);
8786
[self invalidate];
87+
NSString *error = [NSString stringWithFormat:@"Connection to %@ timed out. Are you "
88+
"running node proxy? If you are running on the device, check if "
89+
"you have the right IP address in `RCTWebSocketExecutor.m`.", _url];
90+
_setupError = RCTErrorWithMessage(error);
91+
RCTFatal(_setupError);
8892
return;
8993
}
9094

@@ -95,10 +99,13 @@ - (void)setUp
9599
retries--;
96100
}
97101
if (!runtimeIsReady) {
98-
RCTLogError(@"Runtime is not ready for debugging.\n "
99-
"- Make sure Packager server is running.\n"
100-
"- Make sure the JavaScript Debugger is running and not paused on a breakpoint or exception and try reloading again.");
101102
[self invalidate];
103+
NSString *error = @"Runtime is not ready for debugging.\n "
104+
"- Make sure Packager server is running.\n"
105+
"- Make sure the JavaScript Debugger is running and not paused on a "
106+
"breakpoint or exception and try reloading again.";
107+
_setupError = RCTErrorWithMessage(error);
108+
RCTFatal(_setupError);
102109
return;
103110
}
104111
}
@@ -108,18 +115,21 @@ - (BOOL)connectToProxy
108115
_socketOpenSemaphore = dispatch_semaphore_create(0);
109116
[_socket open];
110117
long connected = dispatch_semaphore_wait(_socketOpenSemaphore, dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC * 10));
111-
return connected == 0;
118+
return connected == 0 && _socket.readyState == RCTSR_OPEN;
112119
}
113120

114121
- (BOOL)prepareJSRuntime
115122
{
116123
__block NSError *initError;
117124
dispatch_semaphore_t s = dispatch_semaphore_create(0);
118-
[self sendMessage:@{@"method": @"prepareJSRuntime"} waitForReply:^(NSError *error, NSDictionary<NSString *, id> *reply) {
125+
[self sendMessage:@{@"method": @"prepareJSRuntime"} onReply:^(NSError *error, NSDictionary<NSString *, id> *reply) {
119126
initError = error;
120127
dispatch_semaphore_signal(s);
121128
}];
122129
long runtimeIsReady = dispatch_semaphore_wait(s, dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC));
130+
if (initError) {
131+
RCTLogInfo(@"Websocket runtime setup failed: %@", initError);
132+
}
123133
return runtimeIsReady == 0 && initError == nil;
124134
}
125135

@@ -142,16 +152,18 @@ - (void)webSocketDidOpen:(RCTSRWebSocket *)webSocket
142152
- (void)webSocket:(RCTSRWebSocket *)webSocket didFailWithError:(NSError *)error
143153
{
144154
dispatch_semaphore_signal(_socketOpenSemaphore);
145-
dispatch_async(dispatch_get_main_queue(), ^{
146-
// Give the setUp method an opportunity to report an error first
147-
RCTLogError(@"WebSocket connection failed with error %@", error);
148-
});
155+
RCTLogInfo(@"WebSocket connection failed with error %@", error);
149156
}
150157

151-
- (void)sendMessage:(NSDictionary<NSString *, id> *)message waitForReply:(RCTWSMessageCallback)callback
158+
- (void)sendMessage:(NSDictionary<NSString *, id> *)message onReply:(RCTWSMessageCallback)callback
152159
{
153160
static NSUInteger lastID = 10000;
154161

162+
if (_setupError) {
163+
callback(_setupError, nil);
164+
return;
165+
}
166+
155167
dispatch_async(_jsQueue, ^{
156168
if (!self.valid) {
157169
NSError *error = [NSError errorWithDomain:@"WS" code:1 userInfo:@{
@@ -176,7 +188,7 @@ - (void)executeApplicationScript:(NSData *)script sourceURL:(NSURL *)URL onCompl
176188
@"url": RCTNullIfNil(URL.absoluteString),
177189
@"inject": _injectedObjects,
178190
};
179-
[self sendMessage:message waitForReply:^(NSError *error, NSDictionary<NSString *, id> *reply) {
191+
[self sendMessage:message onReply:^(NSError *error, NSDictionary<NSString *, id> *reply) {
180192
onComplete(error);
181193
}];
182194
}
@@ -208,7 +220,7 @@ - (void)_executeJSCall:(NSString *)method arguments:(NSArray *)arguments callbac
208220
@"method": method,
209221
@"arguments": arguments
210222
};
211-
[self sendMessage:message waitForReply:^(NSError *socketError, NSDictionary<NSString *, id> *reply) {
223+
[self sendMessage:message onReply:^(NSError *socketError, NSDictionary<NSString *, id> *reply) {
212224
if (socketError) {
213225
onComplete(nil, socketError);
214226
return;

0 commit comments

Comments
 (0)