Skip to content
This repository has been archived by the owner on Nov 14, 2018. It is now read-only.

Best-practice for dealing with multi-callback listeners #505

Open
simonschiller opened this issue Apr 16, 2018 · 3 comments
Open

Best-practice for dealing with multi-callback listeners #505

simonschiller opened this issue Apr 16, 2018 · 3 comments

Comments

@simonschiller
Copy link
Contributor

As @JakeWharton mentioned here, we should define a common pattern for dealing with multi-callback listeners. There are many cases where this problem occurs (e.g. #382 or #351) and having a best-practice in place would help.

Take TextView.addTextChangedListener for example. The API could look like this

textView.addTextChangedListener(
    on = { /* Handle changes ... */ },
    after = { /* Handle changes ... */ }
)

with empty defaults for unused listeners.
Another option would be to add a function for every listener:

textView.onTextChanged { /* Handle changes ... */ }
textView.afterTextChanged { /* Handle changes ... */ }
@simonschiller
Copy link
Contributor Author

I guess one advantage of my second proposed option is, that we are able to remove individual listeners:

val onWatcher = textView.onTextChanged { /* Handle changes ... */ }
val afterWatcher = textView.afterTextChanged { /* Handle changes ... */ }

textView.removeTextChangedListener(afterWatcher)

This would not be possible (or at least not reasonable) with the first approach.

@JakeWharton
Copy link
Contributor

I don't think that's a use case we care about optimizing for. For the first style, in the exceedingly rare case that you want to remove only a single callback you would just invoke the function twice.

@simonschiller
Copy link
Contributor Author

You are right, I did not think about that. I guess in that case the first option would be preferable, as it would only create one TextWatcher, whereas the second one would create two.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants