Skip to content

Add the callback of send operation.#458

Open
NathanLi wants to merge 10 commits into
facebookincubator:mainfrom
NathanLi:master
Open

Add the callback of send operation.#458
NathanLi wants to merge 10 commits into
facebookincubator:mainfrom
NathanLi:master

Conversation

@NathanLi
Copy link
Copy Markdown

Add feature:
Callback of send data.

Add feature:
Callback of send data.
@ghost
Copy link
Copy Markdown

ghost commented Aug 14, 2016

Thank you for your pull request and welcome to our community. We require contributors to sign our Contributor License Agreement, and we don't seem to have you on file. In order for us to review and merge your code, please sign up at https://code.facebook.com/cla - and if you have received this in error or have any questions, please drop us a line at cla@fb.com. Thanks!

@ghost ghost added the CLA Signed label Aug 15, 2016
@NathanLi
Copy link
Copy Markdown
Author

I signed in CLA. Thank you.

@ghost
Copy link
Copy Markdown

ghost commented Aug 15, 2016

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Facebook open source project. Thanks!

@nlutsenko nlutsenko self-assigned this Aug 23, 2016
@nlutsenko
Copy link
Copy Markdown
Contributor

This looks like a nice idea, will review soon and provide some feedback on naming and polish so we can get this merged in!

@ghost ghost added the CLA Signed label Aug 23, 2016
Copy link
Copy Markdown
Contributor

@nlutsenko nlutsenko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a great idea, and I absolutely love it!
Let's work together to get this merged in!

There are a lot of minor nits that need to be fixed before we feel comfortable merging it in.

On top of that, please revert all whitespace additions in SRWebSocket.m lines 1398-1465

Comment thread SocketRocket/SRWebSocket.h Outdated

@protocol SRWebSocketDelegate;

typedef void (^SRSendCompleteBlock)(NSError * _Nullable error);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Rename to SRSendCompletionBlock.

this block will invoked with `nil`.

@return `YES` if the string was scheduled to send, otherwise - `NO`.
*/
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's improve the naming here to the following. Also, will improve the Swift imported method.

- (BOOL)sendString:(NSString *)string 
        completion:(nullable SRSendCompletionBlock)completion NS_SWIFT_NAME(send(string:completion:));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The above code block also ensures that everyone can pass nil to the completion argument, since it looks like you are validating it anyway.

Comment thread SocketRocket/SRWebSocket.h Outdated
/**
Send a UTF-8 String to the server.

@param string String to send.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Double space after complete


/**
Send a UTF-8 String to the server.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Please align String to send with the next parameter description for better readability.

Comment thread SocketRocket/SRWebSocket.h Outdated
Send a UTF-8 String to the server.

@param string String to send.
@param complete The call back of send result.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: NSError
Nit: containing
Nit: , otherwise this block will be invoked

Comment thread SocketRocket/SRWebSocket.m Outdated
return NO;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Revert white space addition.

Comment thread SocketRocket/SRWebSocket.m Outdated
return;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Remove whitespace addition.

Comment thread SocketRocket/SRWebSocket.m Outdated


SRDataCallback *record = [[SRDataCallback alloc] init];
record.completeBlock = complete;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This completion can be nil and we are going to waste precious (on iOS at least) CPU cycles for running all this math later.
What do you think if we don't add the record to the _sendCallbacks at all if completion is nil?
This is going to ease the work later as well.

Comment thread SocketRocket/SRWebSocket.m Outdated
[_sendCallbacks enumerateKeysAndObjectsUsingBlock:^(NSNumber * _Nonnull key, SRDataCallback * _Nonnull obj, BOOL * _Nonnull stop) {
if (NSMaxRange(obj.range) <= _outputBufferOffset) {
[removeKeys addObject:key];
if (obj.completeBlock) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can go away once we can ensure that _sendCallbacks only contain objects with non-nil completion.

Comment thread SocketRocket/SRWebSocket.m Outdated

- (void)_sendFrameWithOpcode:(SROpCode)opCode data:(NSData *)data
{
- (void)_sendFrameWithOpcode:(SROpCode)opCode data:(NSData *)data complete:(SRSendCompleteBlock)complete {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Move the opening bracket to the next line.

@nlutsenko nlutsenko removed their assignment Oct 31, 2016
@NathanLi
Copy link
Copy Markdown
Author

NathanLi commented Nov 3, 2016

@nlutsenko Thank you for taking the time to review my code. I have changed you mentioned, please review again.

Copy link
Copy Markdown
Contributor

@nlutsenko nlutsenko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Almost there...
Few nits, but most importantly:

  • Using NSValue valueWithRange: for the key
  • Improving the method chains and signatures for error:completion: style methods

Comment thread SocketRocket/SRWebSocket.m Outdated
NSString *const SRHTTPResponseErrorKey = @"HTTPResponseStatusCode";

@interface SRDataCallback : NSObject
@property (nonatomic, assign) NSRange range;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Remove extra space aka make it look like NSRange range;

Comment thread SocketRocket/SRWebSocket.m Outdated
{
dispatch_async(_workQueue, ^{
[_sendCallbacks enumerateKeysAndObjectsUsingBlock:^(NSNumber * _Nonnull key, SRDataCallback * _Nonnull obj, BOOL * _Nonnull stop) {
if (obj.completion) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since completion is never nil - you don't need this check.

Comment thread SocketRocket/SRWebSocket.m Outdated
}


if (completion != nil) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Replace with if (completion) {

Comment thread SocketRocket/SRWebSocket.m Outdated
SRDataCallback *record = [[SRDataCallback alloc] initWithRange:dataRange completion:completion];

static NSUInteger keyCount = 0;
_sendCallbacks[@(keyCount++)] = record;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if this is the best way to use a key here.
Can we use a [NSValue valueWithRange:dataRange] as a key?

Comment thread SocketRocket/SRWebSocket.m Outdated
return [self sendString:string error:NULL completion:completion];
}

- (BOOL)sendString:(NSString *)string error:(NSError **)error completion:(nullable SRSendCompletionBlock)completion {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think if we would remove error argument from this method and move it only to the method that needs it? Thus we can eliminate the need for weird method with synchronous and asynchronous signatures?

Comment thread SocketRocket/SRWebSocket.m Outdated
{
return [self sendDataNoCopy:data error:NULL completion:completion];
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as for sendString:error:completion:


static const size_t SRFrameHeaderOverhead = 32;

- (void)_sendFrameWithOpcode:(SROpCode)opCode data:(NSData *)data
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

completion argument needs proper nullability annotation aka completion:(nullable SRSendCompletionBlock)completion

Repository owner removed the GH Review: review-needed label Nov 7, 2016
@NathanLi
Copy link
Copy Markdown
Author

NathanLi commented Nov 9, 2016

Yes, you are right. Use dataRange as a key is correct.

@NathanLi
Copy link
Copy Markdown
Author

@nlutsenko Can you review the code?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants