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:
parent
48cb8d3f92
commit
53aef3eb5f
|
@ -15,7 +15,4 @@
|
|||
@interface RCTWebSocketObserver : NSObject <RCTWebSocketObserver>
|
||||
@end
|
||||
|
||||
@interface RCTWebSocketManager : NSObject <RCTWebSocketProxy>
|
||||
@end
|
||||
|
||||
#endif
|
||||
|
|
|
@ -18,8 +18,6 @@
|
|||
|
||||
#if RCT_DEV // Only supported in dev mode
|
||||
|
||||
#pragma mark - RCTWebSocketObserver
|
||||
|
||||
@interface RCTWebSocketObserver () <RCTSRWebSocketDelegate>
|
||||
@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<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
|
||||
|
|
|
@ -17,6 +17,7 @@
|
|||
@protocol RCTWebSocketObserver
|
||||
- (instancetype)initWithURL:(NSURL *)url;
|
||||
@property (nonatomic, weak) id<RCTWebSocketProxyDelegate> delegate;
|
||||
- (void)start;
|
||||
@end
|
||||
|
||||
@protocol RCTWebSocketProxy
|
||||
|
|
|
@ -267,14 +267,39 @@ RCT_EXPORT_MODULE()
|
|||
// TODO: Move non-UI logic into separate RCTDevSettings module
|
||||
- (void)connectPackager
|
||||
{
|
||||
Class webSocketManagerClass = objc_lookUpClass("RCTWebSocketManager");
|
||||
id<RCTWebSocketProxy> webSocketManager = (id <RCTWebSocketProxy>)[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<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
|
||||
{
|
||||
NSArray<NSNumber *> *const kSupportedVersions = @[ @1 ];
|
||||
|
|
Loading…
Reference in New Issue