Skip to content

Commit 1c49c47

Browse files
nealmanaktolaFlewp
authored andcommitted
expose flush on RCTWebSocketModule to close all open websockets synchronously (#14)
* expose `flush` on RCTWebSocketModule to close all open websockets synchronously add log statement back * simplify code * simplify even more * change comment
1 parent eaf355a commit 1c49c47

File tree

4 files changed

+20
-0
lines changed

4 files changed

+20
-0
lines changed

Libraries/WebSocket/RCTSRWebSocket.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ extern NSString *const RCTSRHTTPResponseErrorKey;
8181
- (void)open;
8282

8383
- (void)close;
84+
- (void)flush;
85+
8486
- (void)closeWithCode:(NSInteger)code reason:(NSString *)reason;
8587

8688
// Send a UTF8 String or Data.

Libraries/WebSocket/RCTSRWebSocket.m

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,13 @@ - (void)unscheduleFromRunLoop:(NSRunLoop *)aRunLoop forMode:(NSString *)mode
583583
[_scheduledRunloops removeObject:@[ aRunLoop, mode ]];
584584
}
585585

586+
- (void)flush
587+
{
588+
// By queueing an empty block, all blocks queued before
589+
// need to finish executing as this is a serial queue
590+
dispatch_sync(_workQueue, ^{});
591+
}
592+
586593
- (void)close
587594
{
588595
[self closeWithCode:RCTSRStatusCodeNormal reason:nil];

React/CoreModules/RCTWebSocketModule.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ NS_ASSUME_NONNULL_BEGIN
2424

2525
- (void)sendData:(NSData *)data forSocketID:(nonnull NSNumber *)socketID;
2626

27+
// Blocking call that waits until there are no more remaining actions on the queue
28+
- (void)flush;
29+
2730
@end
2831

2932
@interface RCTBridge (RCTWebSocketModule)

React/CoreModules/RCTWebSocketModule.mm

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,14 @@ - (NSArray *)supportedEvents
5151
return @[ @"websocketMessage", @"websocketOpen", @"websocketFailed", @"websocketClosed" ];
5252
}
5353

54+
55+
- (void)flush
56+
{
57+
for (RCTSRWebSocket *socket in _sockets.allValues) {
58+
[socket flush];
59+
}
60+
}
61+
5462
- (void)invalidate
5563
{
5664
[super invalidate];

0 commit comments

Comments
 (0)