etiennelenhart / Eiffel

Redux-inspired Android architecture library leveraging Architecture Components and Kotlin Coroutines
MIT License
211 stars 14 forks source link

Debouncing action dispatching #117

Open jordond opened 5 years ago

jordond commented 5 years ago

This is less of an issue, and more asking for an opinion. I've run into a use-case where I need to debounce the dispatching of an action. For example a user is entering text into a search box, which updates a property on the state, which then launches a async network request.

You wouldn't want it to send a request every time a character is typed, but rather after some delay.

Currently I am using a Timer in the Fragment, the approach is simple and it works. I'm just wondering if it's worth coming up with an EiffelViewModel solution.

etiennelenhart commented 5 years ago

That's definitely something that would be useful.

I'd probably go for an eiffel-ui module that provides extension functions for common Android view listeners, since debouncing seems like something the dispatching side should take care of.

The new Flow type from Kotlin Coroutines seems like a good fit, since they're already working on UI-related operators. (https://github.com/Kotlin/kotlinx.coroutines/issues/1107)

So the extensions could expect an EiffelViewModel and an Action as parameters, internally add the required listener that pushes updates to a debounced Flow channel and dispatch the given action on each emit.

That's still a bit rough, especially considering that the Flow API isn't stable yet and documentation is currently scarce, but it could work.

I'm not sure whether something similar is planned for Anko as this would be a good fit but haven't found any information on this.