Skip to content

Commit dde3e82

Browse files
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 3074927 commit dde3e82

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
@@ -80,6 +80,8 @@ extern NSString *const RCTSRHTTPResponseErrorKey;
8080
- (void)open;
8181

8282
- (void)close;
83+
- (void)flush;
84+
8385
- (void)closeWithCode:(NSInteger)code reason:(NSString *)reason;
8486

8587
// Send a UTF8 String or Data.

Libraries/WebSocket/RCTSRWebSocket.m

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

552+
- (void)flush
553+
{
554+
// By queueing an empty block, all blocks queued before
555+
// need to finish executing as this is a serial queue
556+
dispatch_sync(_workQueue, ^{});
557+
}
558+
552559
- (void)close
553560
{
554561
[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)