https://kotlinlang.org logo
#redux
Title
# redux
w

willyrs

01/29/2021, 12:55 PM
Hey all, I have lots of actions going on when my mobile app launches, I was wondering if there's a way to Throttle the store subscription and reduce the number of re-render triggered
p

Patrick Jackson

01/29/2021, 1:06 PM
Typical best practice is to compare the data that you are rendering with the past rendered data a d only update if there is a diff.
The reselect library can fill that purpose, although I think it needs some fixing.
w

willyrs

01/29/2021, 1:08 PM
The problem is that I have a homepage with a vertical list of horizontal lists, and the download of each one is an action. So, when new data arrives it's indeed different, but I was aiming to not render it anyway and wait for a bunch of them
I'm gonna look to reselect and think about it, thanks!
p

Patrick Jackson

01/29/2021, 1:26 PM
I see. It that case maybe not push changes(subscribe), but pull when needed with get state. Would that work in your situation?
w

willyrs

01/29/2021, 1:31 PM
I'm not sure about it, because it's all async so I can't know when it's time to pull.. I'm trying like this:
Copy code
fun <T> LiveData<T>.debounce(duration: Long = 250L) = MediatorLiveData<T>().also { mld ->
    val source = this
    val handler = Handler(Looper.getMainLooper())

    val runnable = Runnable {
        mld.value = source.value
    }

    mld.addSource(source) {
        handler.removeCallbacks(runnable)
        handler.postDelayed(runnable, duration)
    }
}


val data: MutableLiveData<AppState> = MutableLiveData()

store.subscribe {
    data.postValue(it)
}

data.debounce().observe(this) {
    adapter.blablabla()
    adapter.notifyDataSetChanged()
}
It's a bit odd but it seems to do the trick
6 Views