Skip to content

Commit

Permalink
Make RCTPackagerConnection a singleton
Browse files Browse the repository at this point in the history
Reviewed By: fromcelticpark

Differential Revision: D6361741

fbshipit-source-id: 96e92dff5dd3d7aa1f7555442b0eba90e7dbf47c
  • Loading branch information
adamjernst authored and facebook-github-bot committed Nov 21, 2017
1 parent c91d872 commit 9180d4e
Show file tree
Hide file tree
Showing 24 changed files with 448 additions and 714 deletions.
27 changes: 14 additions & 13 deletions Libraries/WebSocket/RCTReconnectingWebSocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,29 @@

#if RCT_DEV // Only supported in dev mode

@class RCTSRWebSocket;

@protocol RCTWebSocketProtocolDelegate

- (void)webSocketDidOpen:(RCTSRWebSocket *)webSocket;

- (void)webSocket:(RCTSRWebSocket *)webSocket didReceiveMessage:(id)message;

- (void)webSocket:(RCTSRWebSocket *)webSocket didCloseWithCode:(NSInteger)code reason:(NSString *)reason wasClean:(BOOL)wasClean;
@class RCTReconnectingWebSocket;

@protocol RCTReconnectingWebSocketDelegate
- (void)reconnectingWebSocketDidOpen:(RCTReconnectingWebSocket *)webSocket;
- (void)reconnectingWebSocket:(RCTReconnectingWebSocket *)webSocket didReceiveMessage:(id)message;
/** Sent when the socket has closed due to error or clean shutdown. An automatic reconnect will start shortly. */
- (void)reconnectingWebSocketDidClose:(RCTReconnectingWebSocket *)webSocket;
@end

@interface RCTReconnectingWebSocket : NSObject

- (instancetype)initWithURL:(NSURL *)url;
@property (nonatomic, weak) id<RCTWebSocketProtocolDelegate> delegate;
/** @brief Must be set before -start to have effect */
@property (nonatomic, strong) dispatch_queue_t delegateDispatchQueue;
/** Delegate will be messaged on the given queue (required). */
- (instancetype)initWithURL:(NSURL *)url queue:(dispatch_queue_t)queue;

@property (nonatomic, weak) id<RCTReconnectingWebSocketDelegate> delegate;
- (void)send:(id)data;
- (void)start;
- (void)stop;

- (instancetype)initWithURL:(NSURL *)url __deprecated_msg("Use initWithURL:queue: instead");
/** @brief Must be set before -start to have effect */
@property (nonatomic, strong) dispatch_queue_t delegateDispatchQueue __deprecated_msg("Use initWithURL:queue: instead");

@end

#endif
23 changes: 12 additions & 11 deletions Libraries/WebSocket/RCTReconnectingWebSocket.m
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@ @implementation RCTReconnectingWebSocket {
RCTSRWebSocket *_socket;
}

@synthesize delegate = _delegate;

+ (void)load
{
static dispatch_once_t onceToken;
Expand All @@ -75,14 +73,20 @@ + (void)load
});
}

- (instancetype)initWithURL:(NSURL *)url
- (instancetype)initWithURL:(NSURL *)url queue:(dispatch_queue_t)queue
{
if (self = [super init]) {
_url = url;
_delegateDispatchQueue = queue;
}
return self;
}

- (instancetype)initWithURL:(NSURL *)url
{
return [self initWithURL:url queue:dispatch_get_main_queue()];
}

- (void)send:(id)data
{
[_socket send:data];
Expand All @@ -93,9 +97,7 @@ - (void)start
[self stop];
_socket = [[RCTSRWebSocket alloc] initWithURL:_url];
_socket.delegate = self;
if (_delegateDispatchQueue) {
[_socket setDelegateDispatchQueue:_delegateDispatchQueue];
}
[_socket setDelegateDispatchQueue:_delegateDispatchQueue];
[_socket open];
}

Expand All @@ -108,9 +110,7 @@ - (void)stop

- (void)webSocket:(RCTSRWebSocket *)webSocket didReceiveMessage:(id)message
{
if (_delegate) {
[_delegate webSocket:webSocket didReceiveMessage:message];
}
[_delegate reconnectingWebSocket:self didReceiveMessage:message];
}

- (void)reconnect
Expand All @@ -126,17 +126,18 @@ - (void)reconnect

- (void)webSocketDidOpen:(RCTSRWebSocket *)webSocket
{
[self.delegate webSocketDidOpen:webSocket];
[_delegate reconnectingWebSocketDidOpen:self];
}

- (void)webSocket:(RCTSRWebSocket *)webSocket didFailWithError:(NSError *)error
{
[_delegate reconnectingWebSocketDidClose:self];
[self reconnect];
}

- (void)webSocket:(RCTSRWebSocket *)webSocket didCloseWithCode:(NSInteger)code reason:(NSString *)reason wasClean:(BOOL)wasClean
{
[self.delegate webSocket:webSocket didCloseWithCode:code reason:reason wasClean:wasClean];
[_delegate reconnectingWebSocketDidClose:self];
[self reconnect];
}

Expand Down
10 changes: 10 additions & 0 deletions React/Base/RCTBridge+Private.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* of patent rights can be found in the PATENTS file in the same directory.
*/

#import <JavaScriptCore/JavaScriptCore.h>
#import <JavaScriptCore/JSBase.h>

#import <React/RCTBridge.h>
Expand Down Expand Up @@ -158,6 +159,15 @@ RCT_EXTERN void RCTVerifyAllModulesExported(NSArray *extraModules);

@end

@interface RCTBridge (JavaScriptCore)

/**
* The raw JSGlobalContextRef used by the bridge.
*/
@property (nonatomic, readonly, assign) JSGlobalContextRef jsContextRef;

@end

@interface RCTBatchedBridge : RCTBridge <RCTInvalidating>

@property (nonatomic, weak, readonly) RCTBridge *parentBridge;
Expand Down
10 changes: 0 additions & 10 deletions React/Base/RCTBridge.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
#if RCT_ENABLE_INSPECTOR
#import "RCTInspectorDevServerHelper.h"
#endif
#import "RCTJSEnvironment.h"
#import "RCTLog.h"
#import "RCTModuleData.h"
#import "RCTPerformanceLogger.h"
Expand Down Expand Up @@ -403,15 +402,6 @@ - (void)registerSegmentWithId:(NSUInteger)segmentId path:(NSString *)path
[self.batchedBridge registerSegmentWithId:segmentId path:path];
}

@end

@implementation RCTBridge (JavaScriptCore)

- (JSContext *)jsContext
{
return [self.batchedBridge jsContext];
}

- (JSGlobalContextRef)jsContextRef
{
return [self.batchedBridge jsContextRef];
Expand Down
5 changes: 0 additions & 5 deletions React/Base/RCTBundleURLProvider.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,6 @@ extern const NSUInteger kRCTBundleURLProviderDefaultPort;
resourceExtension:(NSString *)extension
offlineBundle:(NSBundle *)offlineBundle;

/**
* Returns the URL of the packager server.
*/
- (NSURL *)packagerServerURL;

/**
* The IP address or hostname of the packager.
*/
Expand Down
6 changes: 0 additions & 6 deletions React/Base/RCTBundleURLProvider.m
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,6 @@ - (NSString *)packagerServerHost
return nil;
}

- (NSURL *)packagerServerURL
{
NSString *const host = [self packagerServerHost];
return host ? serverRootWithHost(host) : nil;
}

- (NSURL *)jsBundleURLForBundleRoot:(NSString *)bundleRoot fallbackResource:(NSString *)resourceName fallbackExtension:(NSString *)extension
{
NSString *packagerServerHost = [self packagerServerHost];
Expand Down
29 changes: 0 additions & 29 deletions React/Base/RCTJSEnvironment.h

This file was deleted.

5 changes: 0 additions & 5 deletions React/CxxBridge/RCTCxxBridge.mm
Original file line number Diff line number Diff line change
Expand Up @@ -188,11 +188,6 @@ + (void)initialize
}
}

- (JSContext *)jsContext
{
return contextForGlobalContextRef([self jsContextRef]);
}

- (JSGlobalContextRef)jsContextRef
{
return (JSGlobalContextRef)(self->_reactInstance ? self->_reactInstance->getJavaScriptContext() : nullptr);
Expand Down
8 changes: 4 additions & 4 deletions React/DevSupport/RCTPackagerClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@
#if RCT_DEV // Only supported in dev mode

@class RCTPackagerClientResponder;
@class RCTSRWebSocket;
@class RCTReconnectingWebSocket;

extern const int RCT_PACKAGER_CLIENT_PROTOCOL_VERSION;

@protocol RCTPackagerClientMethod <NSObject>

- (void)handleRequest:(id)params withResponder:(RCTPackagerClientResponder *)responder;
- (void)handleNotification:(id)params;
- (void)handleRequest:(NSDictionary<NSString *, id> *)params withResponder:(RCTPackagerClientResponder *)responder;
- (void)handleNotification:(NSDictionary<NSString *, id> *)params;

@optional

Expand All @@ -30,7 +30,7 @@ extern const int RCT_PACKAGER_CLIENT_PROTOCOL_VERSION;

@interface RCTPackagerClientResponder : NSObject

- (instancetype)initWithId:(id)msgId socket:(RCTSRWebSocket *)socket;
- (instancetype)initWithId:(id)msgId socket:(RCTReconnectingWebSocket *)socket;
- (void)respondWithResult:(id)result;
- (void)respondWithError:(id)error;

Expand Down
6 changes: 3 additions & 3 deletions React/DevSupport/RCTPackagerClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#import "RCTPackagerClient.h"

#import <React/RCTLog.h>
#import <React/RCTSRWebSocket.h>
#import <React/RCTReconnectingWebSocket.h>
#import <React/RCTUtils.h>

#if RCT_DEV // Only supported in dev mode
Expand All @@ -19,10 +19,10 @@

@implementation RCTPackagerClientResponder {
id _msgId;
__weak RCTSRWebSocket *_socket;
__weak RCTReconnectingWebSocket *_socket;
}

- (instancetype)initWithId:(id)msgId socket:(RCTSRWebSocket *)socket
- (instancetype)initWithId:(id)msgId socket:(RCTReconnectingWebSocket *)socket
{
if (self = [super init]) {
_msgId = msgId;
Expand Down
61 changes: 48 additions & 13 deletions React/DevSupport/RCTPackagerConnection.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,62 @@

NS_ASSUME_NONNULL_BEGIN

@class RCTBridge;
@protocol RCTPackagerClientMethod;
@protocol RCTPackagerConnectionConfig;
@class RCTPackagerClientResponder;

typedef uint32_t RCTHandlerToken;
typedef void (^RCTNotificationHandler)(NSDictionary<NSString *, id> *);
typedef void (^RCTRequestHandler)(NSDictionary<NSString *, id> *, RCTPackagerClientResponder *);
typedef void (^RCTConnectedHandler)(void);

/** Encapsulates singleton connection to React Native packager. */
@interface RCTPackagerConnection : NSObject

+ (instancetype)sharedPackagerConnection;

/**
* Encapsulates connection to React Native packager.
* Dispatches messages from websocket to message handlers that must implement
* <RCTPackagerClientMethod> protocol.
* Message dispatch is performed on the main queue, unless message handler
* provides its own queue by overriding "methodQueue" method.
* Registers a handler for a notification broadcast from the packager. An
* example is "reload" - an instruction to reload from the packager.
* If multiple notification handlers are registered for the same method, they
* will all be invoked sequentially.
*/
@interface RCTPackagerConnection : NSObject
- (RCTHandlerToken)addNotificationHandler:(RCTNotificationHandler)handler
queue:(dispatch_queue_t)queue
forMethod:(NSString *)method;

/**
* Registers a handler for a request from the packager. An example is
* pokeSamplingProfiler; it asks for profile data from the client.
* Only one handler can be registered for a given method; calling this
* displaces any previous request handler registered for that method.
*/
- (RCTHandlerToken)addRequestHandler:(RCTRequestHandler)handler
queue:(dispatch_queue_t)queue
forMethod:(NSString *)method;

/**
* Registers a handler that runs at most once, when the connection to the
* packager has been established. The handler will be dispatched immediately
* if the connection is already established.
*/
- (RCTHandlerToken)addConnectedHandler:(RCTConnectedHandler)handler
queue:(dispatch_queue_t)queue;

+ (void)checkDefaultConnectionWithCallback:(void (^)(BOOL isRunning))callback
queue:(dispatch_queue_t)queue;
/** Removes a handler. Silently does nothing if the token is not valid. */
- (void)removeHandler:(RCTHandlerToken)token;

+ (instancetype)connectionForBridge:(RCTBridge *)bridge;
- (instancetype)initWithConfig:(id<RCTPackagerConnectionConfig>)config;
- (void)addHandler:(id<RCTPackagerClientMethod>)handler forMethod:(NSString *)name;
/** Disconnects and removes all handlers. */
- (void)stop;

/**
* Historically no distinction was made between notification and request
* handlers. If you use this method, it will be registered as *both* a
* notification handler *and* a request handler. You should migrate to the
* new block-based API instead.
*/
- (void)addHandler:(id<RCTPackagerClientMethod>)handler
forMethod:(NSString *)method __deprecated_msg("Use addRequestHandler or addNotificationHandler instead");

@end

NS_ASSUME_NONNULL_END
Expand Down
Loading

0 comments on commit 9180d4e

Please sign in to comment.