Frugghi / SwiftSH

A Swift SSH framework that wraps libssh2.
MIT License
194 stars 71 forks source link

Add `return self` to methods with completion closure that already had… #17

Open jpalten opened 6 years ago

jpalten commented 6 years ago

When adding a completion block to open/write/connect/download etc, I noticed the behaviour of chaining those commands is a little different. This fixes that problem, so adding a completion block to any of these method calls does not change other results.

Frugghi commented 6 years ago

What's the use case for this?

jpalten commented 6 years ago

for example:

if I have this code:

        self.shell
            .withCallback { [unowned self] (string: String?, error: String?) in
                DispatchQueue.main.async {
                    if let string = string {
                        self.appendToTextView(string)
                    }
                    if let error = error {
                        self.appendToTextView("[ERROR] \(error)")
                    }
                }
            }
            .connect()
            .authenticate(self.authenticationChallenge)
            .open { [unowned self] (error) in
                if let error = error {
                    self.appendToTextView("[ERROR] \(error)")
                    self.textView.isEditable = false
                } else {
                    self.textView.isEditable = true
                }                
            }

If I now want to add a callback method to connect(), I cannot change the connect to this without the fix:

            }
            .connect { error in
                print("error? \(error)")
            }
            .authenticate(self.authenticationChallenge)
            .open { [unowned self] (error) in

but it needs this in the old situation:

            }
            .connect { error in
                print("error? \(error)")
            }

        self.shell
            .authenticate(self.authenticationChallenge)
            .open { [unowned self] (error) in

which I think is unfortunate.

Hope that clarifies the use case.