Skip to content

Commit

Permalink
Eliminate RCTWebSocketManager
Browse files Browse the repository at this point in the history
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
  • Loading branch information
adamjernst authored and Facebook Github Bot committed Dec 8, 2016
1 parent 2ca6138 commit d0c3e98
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 61 deletions.
3 changes: 0 additions & 3 deletions Libraries/WebSocket/RCTWebSocketManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,4 @@
@interface RCTWebSocketObserver : NSObject <RCTWebSocketObserver>
@end

@interface RCTWebSocketManager : NSObject <RCTWebSocketProxy>
@end

#endif
54 changes: 0 additions & 54 deletions Libraries/WebSocket/RCTWebSocketManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@

#if RCT_DEV // Only supported in dev mode

#pragma mark - RCTWebSocketObserver

@interface RCTWebSocketObserver () <RCTSRWebSocketDelegate>
@end

Expand Down Expand Up @@ -91,56 +89,4 @@ - (void)webSocket:(RCTSRWebSocket *)webSocket didCloseWithCode:(NSInteger)code r

@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
1 change: 1 addition & 0 deletions React/Base/RCTWebSocketProxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
@protocol RCTWebSocketObserver
- (instancetype)initWithURL:(NSURL *)url;
@property (nonatomic, weak) id<RCTWebSocketProxyDelegate> delegate;
- (void)start;
@end

@protocol RCTWebSocketProxy
Expand Down
33 changes: 29 additions & 4 deletions React/Modules/RCTDevMenu.m
Original file line number Diff line number Diff line change
Expand Up @@ -267,14 +267,39 @@ - (NSURL *)packagerURL
// 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 ];
Expand Down

0 comments on commit d0c3e98

Please sign in to comment.