Yarikx / reductor

Redux for Android. Predictable state container library for Java/Android
Apache License 2.0
463 stars 27 forks source link

Question: Is it possible to listen in UI to specific ACTION only? #23

Closed sandsaber closed 7 years ago

sandsaber commented 7 years ago

Hi, Thanks for so cool implementation of redux, just quick question, is it possible to listen in UI to specific ACTION only? I see:

mCancelable = Cursors.forEach(store, state -> {
            adapter.setNotes(state.getFilteredNotes());
            spinner.setSelection(state.filter().ordinal());
        });

In the example, so each time any action popup, it will call update, but what, if I want to do specific UI changes on specific ACTION? :)

Yarikx commented 7 years ago

Hi @sandsaber

There are a couple of ways to do that. Can you provide some example why do you need one?

You can create Middleware that will listen to specific actions and propagate them however you want.

sandsaber commented 7 years ago

Basically, idea is simple: update UI when it really needed. I can modify data but UI should be not affected. Middleware sounds good, yes, forgot about it >_< Any examples how it can be used with UI? If I need pass to UI that it need redraw store state.

Maybe this example will be better: If we have store with a lot of data, which take some time to filter fo example, we can filter it async and then let UI know that data ready to show.

Yarikx commented 7 years ago

But if you just want to only update the UI when some subset of your data changes you can use Cursors.map(fn). provide the function that will map your state to some substate (let's say a few fields), and map will propagate it only when that substate changes. That is equal to stateObserable.map(fn).distinctUntilChanged() in RxJava

sandsaber commented 7 years ago

Good point, thank you! One more question :) As I understood currently whole flow of redux running synchronously? You mention that you plan create Async example, can you share? :) Wanna see your version of it :)

Yarikx commented 7 years ago

Oh, yeah. It's supported, I just need to provide an example. You can use RxJava for it. Check out how JS community is using https://github.com/redux-observable/redux-observable. Reudctor-observable id direct port of it. With it you can define epics like that:

Epic<String> pingPongEpic = (actions, store) ->            
        actions.filter(Epics.ofType("PING"))           
                .delay(1, TimeUnit.SECONDS)            
                .map(action -> Action.create("PONG")); 

Basically you can observe actions (and state) as observable and dispatch actions back to the store.

sandsaber commented 7 years ago

Thanks for help!