Uh oh!
There was an error while loading. Please reload this page.
http: DRY ClientRequest.prototype._deferToConnect - #2769
Conversation
Logic for calling the passed in socket method and/or callback was duplicated.
There was a problem hiding this comment.
Can you make this function callSocketMethod()
ahoym
commented
Sep 9, 2015
Comments have been addressed. Of course, any more are welcome. |
kahnvex
commented
Sep 9, 2015
🍇 |
There was a problem hiding this comment.
While you're here, self doesn't seem to be necessary. Mind removing it? It can be replaced with, as far as I can tell, a var socket = this.socket. The bottom reference to self can just be used as this.
There was a problem hiding this comment.
I converted the code as you suggested (good catch btw), and that caused the error
cannotreadproperty'writable'ofnullif(socket.writable){// socket used to be self.socket
...This seems to stem from the initial _deferToConnect call, where this.socket is undefined. When the ClientRequest catches the 'socket' event, the socket variable in onSocket() refers to the initial undefined value which was scoped in. As a result, everything blows up when trying to read the writable property of that.
A work around that I found was to have onSocket() accept a sock argument (given by the socket event), and update socket with that parameter. So this piece of code works:
ClientRequest.prototype._deferToConnect=function(method,arguments_,cb){// ...varsocket=this.socket;functioncallSocketMethod(){if(method)socket[method].apply(socket,arguments_);if(typeofcb==='function')cb();}varonSocket=function(sock){// Now accepts the socket from the 'socket' event.socket=sock||socket;// Update socket reference.if(socket.writable){callSocketMethod();}else{socket.once('connect',callSocketMethod);}};if(!socket){this.once('socket',onSocket);}else{onSocket();}};However I'm not completely sure on the repercussions of this. Maybe leaving the existing implementation would be for the better? I'm interested in what you think
There was a problem hiding this comment.
Hmm, you're right, that is something I missed originally. I'd say let's keep the current implementation, as that new one wouldn't really improve the quality of the code. Sorry about the run around!
There was a problem hiding this comment.
Sounds good, then I'll leave this PR as it is. And no need to apologize, we got some documentation and a deeper understanding of the code here 😄
brendanashworth
commented
Sep 12, 2015
ahoym
commented
Sep 14, 2015
Seems like the build failed? Looks like only the |
rvagg
commented
Sep 15, 2015
https://ci.nodejs.org/job/node-test-pull-request/302/ those arm errors were fixed yesterday |
brendanashworth
commented
Sep 16, 2015
CI is happy, this LGTM. @cjihrig ? |
cjihrig
commented
Sep 16, 2015
Yep, LGTM |
ahoym
commented
Sep 21, 2015
Bump |
Logic for calling the passed in socket method and/or callback was duplicated. This commit refactors the relevant code to remove the redundancy. PR-URL: #2769 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Brendan Ashworth <brendan.ashworth@me.com>
cjihrig
commented
Sep 22, 2015
Thanks! Landed in 79d2c4e after squashing. |
Logic for calling the passed in socket method and/or callback was duplicated. This commit refactors the relevant code to remove the redundancy. PR-URL: #2769 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Brendan Ashworth <brendan.ashworth@me.com>
Came across some repetitive lines when reviewing code.
Logic for calling the passed in socket
methodand/orcbwas duplicated. Extracted these calls into a separate function,callSocketMethod, and invoked this new function in place of the replaced code.