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

Improve clarity of asyncAfter code and other timeout #259

Merged
merged 1 commit into from
Sep 30, 2016
Merged

Improve clarity of asyncAfter code and other timeout #259

merged 1 commit into from
Sep 30, 2016

Conversation

robinkunde
Copy link
Contributor

Hi, I'd like to improve the clarity of the deadline calculation for Dispatch.asyncAfter in WebSocket.disconnect(). I think the existing code is a result of the Swift3 code updater and could use some syntactic sugar.

I also added underscores to time-related constants to improve legibility.

@@ -201,7 +201,8 @@ public class WebSocket : NSObject, StreamDelegate {
public func disconnect(forceTimeout: TimeInterval? = nil, closeCode: UInt16 = CloseCode.normal.rawValue) {
switch forceTimeout {
case .some(let seconds) where seconds > 0:
callbackQueue.asyncAfter(deadline: DispatchTime.now() + Double(Int64(seconds * Double(NSEC_PER_SEC))) / Double(NSEC_PER_SEC)) { [weak self] in
let milliseconds = Int(seconds * 1_000)
callbackQueue.asyncAfter(deadline: .now() + .milliseconds(milliseconds)) { [weak self] in

Choose a reason for hiding this comment

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

You already have seconds to use now() + .seconds(Int(x)) function.

Copy link
Contributor Author

@robinkunde robinkunde Sep 28, 2016

Choose a reason for hiding this comment

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

That would remove the ability to specify fractional timeouts like 2.5 seconds. Well, you could pass them in, but the fractional part would be silently dropped. That's why I'm doing the conversion to milliseconds first.

@daltoniam
Copy link
Owner

It was and I like the clarity changes. Thanks!

@daltoniam daltoniam merged commit b0fa08c into daltoniam:master Sep 30, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants