SwiftSH
SwiftSH copied to clipboard
Add `return self` to methods with completion closure that already had…
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.
What's the use case for this?
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.