From d0c3e98d1d941b726cd1cb8932df2df036fed7fe Mon Sep 17 00:00:00 2001 From: Adam Ernst Date: Thu, 8 Dec 2016 07:28:13 -0800 Subject: [PATCH] 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 --- Libraries/WebSocket/RCTWebSocketManager.h | 3 -- Libraries/WebSocket/RCTWebSocketManager.m | 54 ----------------------- React/Base/RCTWebSocketProxy.h | 1 + React/Modules/RCTDevMenu.m | 33 ++++++++++++-- 4 files changed, 30 insertions(+), 61 deletions(-) diff --git a/Libraries/WebSocket/RCTWebSocketManager.h b/Libraries/WebSocket/RCTWebSocketManager.h index 9b0764695..7a1248976 100644 --- a/Libraries/WebSocket/RCTWebSocketManager.h +++ b/Libraries/WebSocket/RCTWebSocketManager.h @@ -15,7 +15,4 @@ @interface RCTWebSocketObserver : NSObject @end -@interface RCTWebSocketManager : NSObject -@end - #endif diff --git a/Libraries/WebSocket/RCTWebSocketManager.m b/Libraries/WebSocket/RCTWebSocketManager.m index a9330899c..61d96da9d 100644 --- a/Libraries/WebSocket/RCTWebSocketManager.m +++ b/Libraries/WebSocket/RCTWebSocketManager.m @@ -18,8 +18,6 @@ #if RCT_DEV // Only supported in dev mode -#pragma mark - RCTWebSocketObserver - @interface RCTWebSocketObserver () @end @@ -91,56 +89,4 @@ @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)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 diff --git a/React/Base/RCTWebSocketProxy.h b/React/Base/RCTWebSocketProxy.h index 692e687fd..9587af7dd 100644 --- a/React/Base/RCTWebSocketProxy.h +++ b/React/Base/RCTWebSocketProxy.h @@ -17,6 +17,7 @@ @protocol RCTWebSocketObserver - (instancetype)initWithURL:(NSURL *)url; @property (nonatomic, weak) id delegate; +- (void)start; @end @protocol RCTWebSocketProxy diff --git a/React/Modules/RCTDevMenu.m b/React/Modules/RCTDevMenu.m index 92640da79..e8862ff19 100644 --- a/React/Modules/RCTDevMenu.m +++ b/React/Modules/RCTDevMenu.m @@ -267,14 +267,39 @@ RCT_EXPORT_MODULE() // TODO: Move non-UI logic into separate RCTDevSettings module - (void)connectPackager { - Class webSocketManagerClass = objc_lookUpClass("RCTWebSocketManager"); - id webSocketManager = (id )[webSocketManagerClass sharedInstance]; + RCTAssertMainQueue(); + NSURL *url = [self packagerURL]; - if (url) { - [webSocketManager setDelegate:self forURL:url]; + if (!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> *observers = nil; + if (observers == nil) { + observers = [NSMutableDictionary new]; + } + + NSString *key = [url absoluteString]; + id existingObserver = observers[key]; + if (existingObserver) { + existingObserver.delegate = self; + } else { + id newObserver = [(id)[webSocketObserverClass alloc] initWithURL:url]; + newObserver.delegate = self; + [newObserver start]; + observers[key] = newObserver; } } + + - (BOOL)isSupportedVersion:(NSNumber *)version { NSArray *const kSupportedVersions = @[ @1 ];