-
Notifications
You must be signed in to change notification settings - Fork 29.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
http: DRY ClientRequest.prototype._deferToConnect #2769
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -504,19 +504,17 @@ ClientRequest.prototype._deferToConnect = function(method, arguments_, cb) { | |
// in the future (when a socket gets assigned out of the pool and is | ||
// eventually writable). | ||
var self = this; | ||
var callSocketMethod = function() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you make this |
||
if (method) { | ||
self.socket[method].apply(self.socket, arguments_); | ||
} | ||
if (cb) { cb(); } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if (typeof cb === 'function')
cb(); There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are the absence of curly braces preferred? I'm just wondering because I've seen plenty of cases of both in this file, and I'd like to conform to consistency. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, core prefers to leave out braces in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Cool, good to know. Then on a similar vein, we should also do this above? if (method)
self.socket[method].apply(self.socket, arguments_); There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah. Yes :-) |
||
}; | ||
var onSocket = function() { | ||
if (self.socket.writable) { | ||
if (method) { | ||
self.socket[method].apply(self.socket, arguments_); | ||
} | ||
if (cb) { cb(); } | ||
callSocketMethod(); | ||
} else { | ||
self.socket.once('connect', function() { | ||
if (method) { | ||
self.socket[method].apply(self.socket, arguments_); | ||
} | ||
if (cb) { cb(); } | ||
}); | ||
self.socket.once('connect', callSocketMethod); | ||
} | ||
}; | ||
if (!self.socket) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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, avar socket = this.socket
. The bottom reference toself
can just be used asthis
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I converted the code as you suggested (good catch btw), and that caused the error
This seems to stem from the initial
_deferToConnect
call, wherethis.socket
isundefined
. When the ClientRequest catches the 'socket' event, thesocket
variable inonSocket()
refers to the initialundefined
value which was scoped in. As a result, everything blows up when trying to read thewritable
property of that.A work around that I found was to have
onSocket()
accept asock
argument (given by thesocket
event), and updatesocket
with that parameter. So this piece of code works: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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 😄