Skip to content

Add optional write completion handler for emit's #1097

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

Merged
merged 5 commits into from
Oct 4, 2018
Merged

Add optional write completion handler for emit's #1097

merged 5 commits into from
Oct 4, 2018

Conversation

headlessme
Copy link
Contributor

A first draft to add an optional completion handler for calls to emit, see #1096 for reasoning. I'm not a Swift expert so I'm quite likely doing something dumb here :)

Thanks to @AgentFeeble for providing a great starting point.

Fixes #1096

@nuclearace
Copy link
Member

This looks pretty good! Can you open this against the development branch?

@headlessme headlessme changed the base branch from master to development October 1, 2018 10:10
@headlessme
Copy link
Contributor Author

Great! I've changed the base branch

@nuclearace
Copy link
Member

Excellent, one thought I have though: Would it make sense to wrap the user provided callback with one that wraps and dispatches on the handleQueue? If we don't do that we'd need to document on the method that the provided closure won't be executed on the handle queue.

@nuclearace
Copy link
Member

Something maybe like

{[weak self] in 
  guard let this = self else { return }
  this.handleQueue.async { /* call callback */ }
}

@headlessme
Copy link
Contributor Author

Good idea – added!

@@ -295,8 +295,14 @@ open class SocketIOClient : NSObject, SocketIOClientSpec {
}

func emit(_ data: [Any], ack: Int? = nil, binary: Bool = true, isAck: Bool = false, completion: (() -> ())? = nil) {
// wrap the completion handler so it always runs async via handlerQueue
let wrappedCompletion = {[weak self, completion] in
Copy link
Member

Choose a reason for hiding this comment

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

You shouldn't need to put completion in the capture list.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed

/// - parameter items: The items to send with this event. Send an empty array to send no data.
/// - parameter completion: Callback called on transport write completion.
@objc
open func emit(_ event: String, with items: [Any], completion: @escaping () -> ()) {
Copy link
Member

Choose a reason for hiding this comment

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

One last thing, does it make sense to also add a nicer Swift version of this that uses variadics? I think it should be possible to define

open func emit(_ event: String, _ items: SocketData..., completion: () -> ())  { /* call to base version like the other methods */ }

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added a method for Swift. I also added the method signature to the SocketIOClientSpec, but not sure if that's needed?

/// - parameter items: The items to send with this event. May be left out.
/// - parameter completion: Callback called on transport write completion.
open func emit(_ event: String, _ items: SocketData..., completion: @escaping () -> ()) {
emit([event] + items, completion: completion)
Copy link
Member

Choose a reason for hiding this comment

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

This needs to do the transformation from SocketData to something representable in JSON. See the other swift-centric methods.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah yea of course, added.

@nuclearace
Copy link
Member

Awesome, this looks good! I'll merge this in once 13.3.1 is merged into master (should be this week.)

@headlessme
Copy link
Contributor Author

Great! Thanks for the quick review turnaround 👍

@nuclearace nuclearace merged commit e5f3fb1 into socketio:development Oct 4, 2018
@vonox7
Copy link
Contributor

vonox7 commented Nov 7, 2018

This PR introduces major crashes in productions, see #1096 (comment)

I created a fork which reverts this PR and leads to a crash-free library: https://github.com/vonox7/socket.io-client-swift/commits/v-revert-completion-handler

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