Permalink
Browse files

Split off websocket related items to RCTPackagerConnection

Reviewed By: cwdick

Differential Revision: D4588557

fbshipit-source-id: 3f4176b9bab2b06cacbb06f1ff514a5f99a76d73
  • Loading branch information...
javache authored and facebook-github-bot committed Apr 20, 2017
1 parent 2403b42 commit db8efd9c4bbacb24fe40200383512535dd16f8e8
View
@@ -43,15 +43,15 @@ Pod::Spec.new do |s|
ss.dependency "Yoga", "#{package["version"]}.React"
ss.dependency "React/cxxreact"
ss.source_files = "React/**/*.{c,h,m,mm,S}"
ss.exclude_files = "**/__tests__/*", "IntegrationTests/*", "React/DevSupport/*", "React/Modules/RCTDev{LoadingView,Menu}.*", "React/**/RCTTVView.*", "ReactCommon/yoga/*", "React/Cxx*/*"
ss.exclude_files = "**/__tests__/*", "IntegrationTests/*", "React/DevSupport/*", "React/**/RCTTVView.*", "ReactCommon/yoga/*", "React/Cxx*/*"
ss.framework = "JavaScriptCore"
ss.libraries = "stdc++"
end
s.subspec "DevSupport" do |ss|
ss.dependency "React/Core"
ss.dependency "React/RCTWebSocket"
ss.source_files = "React/DevSupport/*", "React/Modules/RCTDev{LoadingView,Menu}.*"
ss.source_files = "React/DevSupport/*"
end
s.subspec "tvOS" do |ss|
@@ -9,6 +9,8 @@
#import <React/RCTBridgeModule.h>
@class RCTLoadingProgress;
@interface RCTDevLoadingView : NSObject <RCTBridgeModule>
+ (void)setEnabled:(BOOL)enabled;
@@ -7,13 +7,14 @@
* of patent rights can be found in the PATENTS file in the same directory.
*/
#import "RCTDevLoadingView.h"
#import <QuartzCore/QuartzCore.h>
#import "RCTBridge.h"
#import "RCTDevLoadingView.h"
#import "RCTDefines.h"
#import "RCTUtils.h"
#import "RCTModalHostViewController.h"
#import "RCTUtils.h"
#if RCT_DEV
File renamed without changes.
File renamed without changes.
@@ -0,0 +1,28 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
#import <Foundation/Foundation.h>
#import <React/RCTDefines.h>
#if RCT_DEV
@class RCTBridge;
/**
* Encapsulates connection to React Native packager
*/
@interface RCTPackagerConnection : NSObject
- (instancetype)initWithBridge:(RCTBridge *)bridge;
- (void)connect;
@end
#endif
@@ -0,0 +1,87 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
#import "RCTPackagerConnection.h"
#import <objc/runtime.h>
#import <React/RCTAssert.h>
#import <React/RCTBridge.h>
#import <React/RCTWebSocketObserverProtocol.h>
#import "RCTPackagerClient.h"
#import "RCTReloadPackagerMethod.h"
#import "RCTSamplingProfilerPackagerMethod.h"
#if RCT_DEV
@implementation RCTPackagerConnection {
RCTBridge *_bridge;
}
- (instancetype)initWithBridge:(RCTBridge *)bridge
{
if (self = [super init]) {
_bridge = bridge;
}
return self;
}
- (void)connect
{
RCTAssertMainQueue();
NSURL *url = [self packagerURL];
if (!url) {
return;
}
// The jsPackagerClient is a static map that holds different packager clients per the packagerURL
// In case many instances of DevMenu are created, the latest instance that use the same URL as
// previous instances will override given packager client's method handlers
static NSMutableDictionary<NSString *, RCTPackagerClient *> *jsPackagerClients = nil;
if (jsPackagerClients == nil) {
jsPackagerClients = [NSMutableDictionary new];
}
NSString *key = [url absoluteString];
RCTPackagerClient *packagerClient = jsPackagerClients[key];
if (!packagerClient) {
packagerClient = [[RCTPackagerClient alloc] initWithURL:url];
jsPackagerClients[key] = packagerClient;
} else {
[packagerClient stop];
}
[packagerClient addHandler:[[RCTReloadPackagerMethod alloc] initWithBridge:_bridge]
forMethod:@"reload"];
[packagerClient addHandler:[[RCTSamplingProfilerPackagerMethod alloc] initWithBridge:_bridge]
forMethod:@"pokeSamplingProfiler"];
[packagerClient start];
}
- (NSURL *)packagerURL
{
NSString *host = [_bridge.bundleURL host];
NSString *scheme = [_bridge.bundleURL scheme];
if (!host) {
host = @"localhost";
scheme = @"http";
}
NSNumber *port = [_bridge.bundleURL port];
if (!port) {
port = @8081; // Packager default port
}
return [NSURL URLWithString:[NSString stringWithFormat:@"%@://%@:%@/message?role=ios-rn-rctdevmenu", scheme, host, port]];
}
@end
#endif
@@ -7,12 +7,13 @@
* of patent rights can be found in the PATENTS file in the same directory.
*/
#import <React/RCTBridge.h>
#import <React/RCTPackagerClient.h>
#if RCT_DEV // Only supported in dev mode
@interface RCTReloadPackagerMethod : NSObject<RCTPackagerClientMethod>
@class RCTBridge;
@interface RCTReloadPackagerMethod : NSObject <RCTPackagerClientMethod>
- (instancetype)initWithBridge:(RCTBridge *)bridge;
@@ -11,6 +11,8 @@
#import <objc/runtime.h>
#import "RCTBridge.h"
#if RCT_DEV // Only supported in dev mode
@implementation RCTReloadPackagerMethod {
@@ -14,6 +14,7 @@
#import <React/RCTBridge+JavaScriptCore.h>
#import <jschelpers/JavaScriptCore.h>
#import "RCTBridge.h"
#import "RCTLog.h"
#if RCT_DEV // Only supported in dev mode
@@ -37,13 +37,14 @@
NSString *const kRCTDevSettingsUserDefaultsKey = @"RCTDevMenu";
#if RCT_DEV
#if __has_include("RCTPackagerClient.h")
#import "RCTPackagerClient.h"
#import "RCTReloadPackagerMethod.h"
#import "RCTSamplingProfilerPackagerMethod.h"
#define ENABLE_PACKAGER_CONNECTION RCT_DEV && __has_include("RCTPackagerConnection.h")
#if ENABLE_PACKAGER_CONNECTION
#import "RCTPackagerConnection.h"
#endif
#if RCT_DEV
@interface RCTDevSettingsUserDefaultsDataSource : NSObject <RCTDevSettingsDataSource>
@end
@@ -109,6 +110,10 @@ @interface RCTDevSettings () <RCTBridgeModule, RCTInvalidating>
NSURLSessionDataTask *_liveReloadUpdateTask;
NSURL *_liveReloadURL;
BOOL _isJSLoaded;
#if ENABLE_PACKAGER_CONNECTION
RCTPackagerConnection *_packagerConnection;
#endif
}
@property (nonatomic, strong) Class executorClass;
@@ -144,7 +149,7 @@ - (instancetype)initWithDataSource:(id<RCTDevSettingsDataSource>)dataSource
// Delay setup until after Bridge init
dispatch_async(dispatch_get_main_queue(), ^{
[self _synchronizeAllSettings];
[self connectPackager];
[self _configurePackagerConnection];
});
}
return self;
@@ -380,7 +385,19 @@ - (void)setExecutorClass:(Class)executorClass
}
}
#pragma mark - internal
#pragma mark - Internal
- (void)_configurePackagerConnection
{
#if ENABLE_PACKAGER_CONNECTION
if (_packagerConnection) {
return;
}
_packagerConnection = [[RCTPackagerConnection alloc] initWithBridge:_bridge];
[_packagerConnection connect];
#endif
}
/**
* Query the data source for all possible settings and make sure we're doing the right
@@ -459,64 +476,6 @@ - (void)jsLoaded:(NSNotification *)notification
});
}
#pragma mark - RCTWebSocketObserver
- (NSURL *)packagerURL
{
#if !__has_include("RCTWebSocketObserver.h")
return nil;
#else
NSString *host = [_bridge.bundleURL host];
NSString *scheme = [_bridge.bundleURL scheme];
if (!host) {
host = @"localhost";
scheme = @"http";
}
NSNumber *port = [_bridge.bundleURL port];
if (!port) {
port = @8081; // Packager default port
}
return [NSURL URLWithString:[NSString stringWithFormat:@"%@://%@:%@/message?role=ios-rn-rctdevmenu", scheme, host, port]];
#endif
}
// TODO: Move non-UI logic into separate RCTDevSettings module
- (void)connectPackager
{
RCTAssertMainQueue();
NSURL *url = [self packagerURL];
if (!url) {
return;
}
#if __has_include("RCTPackagerClient.h")
// The jsPackagerClient is a static map that holds different packager clients per the packagerURL
// In case many instances of DevMenu are created, the latest instance that use the same URL as
// previous instances will override given packager client's method handlers
static NSMutableDictionary<NSString *, RCTPackagerClient *> *jsPackagerClients = nil;
if (jsPackagerClients == nil) {
jsPackagerClients = [NSMutableDictionary new];
}
NSString *key = [url absoluteString];
RCTPackagerClient *packagerClient = jsPackagerClients[key];
if (!packagerClient) {
packagerClient = [[RCTPackagerClient alloc] initWithURL:url];
jsPackagerClients[key] = packagerClient;
} else {
[packagerClient stop];
}
[packagerClient addHandler:[[RCTReloadPackagerMethod alloc] initWithBridge:_bridge]
forMethod:@"reload"];
[packagerClient addHandler:[[RCTSamplingProfilerPackagerMethod alloc] initWithBridge:_bridge]
forMethod:@"pokeSamplingProfiler"];
[packagerClient start];
#endif
}
@end
#else // #if RCT_DEV
Oops, something went wrong.

0 comments on commit db8efd9

Please sign in to comment.