Skip to content
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

Expose and use once #664

Merged
merged 5 commits into from
Jul 30, 2019
Merged

Expose and use once #664

merged 5 commits into from
Jul 30, 2019

Conversation

tinovyatkin
Copy link
Contributor

@tinovyatkin tinovyatkin commented Jul 26, 2019

Fixes a misspelling in EventEmitter property declaration, exposes once method and uses once where it's more appropriate than on

@tinovyatkin tinovyatkin changed the title use once Expose and use once Jul 26, 2019
@@ -43,10 +43,11 @@ function Stripe(key, version) {
value: new EventEmitter(),
enumerable: false,
configurable: false,
writeable: false,
writable: false,
});
Copy link
Contributor Author

Choose a reason for hiding this comment

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

🧐

Copy link
Contributor

Choose a reason for hiding this comment

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

heh, oops! Good catch.

Copy link
Contributor

@rattrayalex-stripe rattrayalex-stripe left a comment

Choose a reason for hiding this comment

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

Nice, great idea! Pretty sure this is safe. Thanks Tino!

One quick question, I think we might want to keep 'socket' as .on...

Mind assigning this back to me when you've answered/amended that?

@@ -381,13 +381,16 @@ StripeResource.prototype = {
}
});

req.on('socket', (socket) => {
req.once('socket', (socket) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not 100% sure that 'socket' event can only be sent once, and the body of the function seems to imply it might be more than once. Do you know of a guarantee that this is at-most-once?

Copy link
Contributor Author

@tinovyatkin tinovyatkin Jul 30, 2019

Choose a reason for hiding this comment

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

Look at the code - it checks a boolean flag socket.connecting (https://nodejs.org/api/net.html#net_socket_connecting) and in case of true waits for connection to be established by setting other event listener (socket.once).
In both cases this function are finishing with res.end() - so, I'm pretty sure it's designed to be running by once.
I can keep an on, as you wish, but see no error in this refactoring.

Copy link
Contributor Author

@tinovyatkin tinovyatkin Jul 30, 2019

Choose a reason for hiding this comment

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

@rattrayalex-stripe In fact, not, keeping on is an error. Just trace what will happen if this event handler will be called more than once - it will attempt to write to the req after calling req.end in previous call or in socket.once event handler.

Copy link
Contributor

Choose a reason for hiding this comment

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

Okay great, I think that looks right. Thanks for digging here.

@@ -43,10 +43,11 @@ function Stripe(key, version) {
value: new EventEmitter(),
enumerable: false,
configurable: false,
writeable: false,
writable: false,
});
Copy link
Contributor

Choose a reason for hiding this comment

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

heh, oops! Good catch.

@tinovyatkin tinovyatkin removed their assignment Jul 30, 2019
@rattrayalex-stripe rattrayalex-stripe merged commit ce71a59 into stripe:master Jul 30, 2019
@tinovyatkin tinovyatkin deleted the fix-event-emitter branch July 30, 2019 22:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants