Eliminate RCTWebSocketManager

Summary: This singleton was unnecessary and can be implemented with a single `static` in `RCTDevMenu`. In another diff, I will rename `RCTWebSocketManager.{h,m}` to reflect the only class that remains.

Reviewed By: javache

Differential Revision: D4296551

fbshipit-source-id: 653971dfb31de5b0a161b531eed82a067f536ce3
This commit is contained in:
Adam Ernst 2016-12-08 07:28:13 -08:00 committed by Martin Konicek
parent 48cb8d3f92
commit 53aef3eb5f
4 changed files with 30 additions and 61 deletions

View File

@ -15,7 +15,4 @@
@interface RCTWebSocketObserver : NSObject <RCTWebSocketObserver> @interface RCTWebSocketObserver : NSObject <RCTWebSocketObserver>
@end @end
@interface RCTWebSocketManager : NSObject <RCTWebSocketProxy>
@end
#endif #endif

View File

@ -18,8 +18,6 @@
#if RCT_DEV // Only supported in dev mode #if RCT_DEV // Only supported in dev mode
#pragma mark - RCTWebSocketObserver
@interface RCTWebSocketObserver () <RCTSRWebSocketDelegate> @interface RCTWebSocketObserver () <RCTSRWebSocketDelegate>
@end @end
@ -91,56 +89,4 @@
@end @end
#pragma mark - RCTWebSocketManager
@interface RCTWebSocketManager()
@property (nonatomic, strong) NSMutableDictionary *sockets;
@end
@implementation RCTWebSocketManager
+ (instancetype)sharedInstance
{
static RCTWebSocketManager *sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedInstance = [self new];
});
return sharedInstance;
}
- (void)setDelegate:(id<RCTWebSocketProxyDelegate>)delegate forURL:(NSURL *)url
{
NSString *key = [url absoluteString];
RCTWebSocketObserver *observer = _sockets[key];
if (observer) {
if (!delegate) {
[observer stop];
[_sockets removeObjectForKey:key];
} else {
observer.delegate = delegate;
}
} else {
RCTWebSocketObserver *newObserver = [[RCTWebSocketObserver alloc] initWithURL:url];
newObserver.delegate = delegate;
[newObserver start];
_sockets[key] = newObserver;
}
}
- (instancetype)init
{
if ((self = [super init])) {
_sockets = [NSMutableDictionary new];
}
return self;
}
@end
#endif #endif

View File

@ -17,6 +17,7 @@
@protocol RCTWebSocketObserver @protocol RCTWebSocketObserver
- (instancetype)initWithURL:(NSURL *)url; - (instancetype)initWithURL:(NSURL *)url;
@property (nonatomic, weak) id<RCTWebSocketProxyDelegate> delegate; @property (nonatomic, weak) id<RCTWebSocketProxyDelegate> delegate;
- (void)start;
@end @end
@protocol RCTWebSocketProxy @protocol RCTWebSocketProxy

View File

@ -267,14 +267,39 @@ RCT_EXPORT_MODULE()
// TODO: Move non-UI logic into separate RCTDevSettings module // TODO: Move non-UI logic into separate RCTDevSettings module
- (void)connectPackager - (void)connectPackager
{ {
Class webSocketManagerClass = objc_lookUpClass("RCTWebSocketManager"); RCTAssertMainQueue();
id<RCTWebSocketProxy> webSocketManager = (id <RCTWebSocketProxy>)[webSocketManagerClass sharedInstance];
NSURL *url = [self packagerURL]; NSURL *url = [self packagerURL];
if (url) { if (!url) {
[webSocketManager setDelegate:self forURL:url]; return;
}
Class webSocketObserverClass = objc_lookUpClass("RCTWebSocketObserver");
if (webSocketObserverClass == Nil) {
return;
}
// If multiple RCTDevMenus are created, the most recently connected one steals the RCTWebSocketObserver.
// (Why this behavior exists is beyond me, as of this writing.)
static NSMutableDictionary<NSString *, id<RCTWebSocketObserver>> *observers = nil;
if (observers == nil) {
observers = [NSMutableDictionary new];
}
NSString *key = [url absoluteString];
id<RCTWebSocketObserver> existingObserver = observers[key];
if (existingObserver) {
existingObserver.delegate = self;
} else {
id<RCTWebSocketObserver> newObserver = [(id<RCTWebSocketObserver>)[webSocketObserverClass alloc] initWithURL:url];
newObserver.delegate = self;
[newObserver start];
observers[key] = newObserver;
} }
} }
- (BOOL)isSupportedVersion:(NSNumber *)version - (BOOL)isSupportedVersion:(NSNumber *)version
{ {
NSArray<NSNumber *> *const kSupportedVersions = @[ @1 ]; NSArray<NSNumber *> *const kSupportedVersions = @[ @1 ];