Hey all, I have lots of actions going on when my m...
# redux
w
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
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
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
I see. It that case maybe not push changes(subscribe), but pull when needed with get state. Would that work in your situation?
w
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