Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dispatch from reducer? #28

Closed
hoopes opened this issue Feb 2, 2020 · 4 comments
Closed

Dispatch from reducer? #28

hoopes opened this issue Feb 2, 2020 · 4 comments

Comments

@hoopes
Copy link

hoopes commented Feb 2, 2020

Hi - first of all, this library is great, and I hope you keep working on it. I'm kind of a beginning in swift development, and this is realllllly helping me get my feet under me.

I was wondering if it was possible to dispatch from a reducer function? My current playground use-case is there is a single AppState, and it has a bunch of sub-states. Each substate initializes itself, and the AppState keeps track of the master "the whole app is initialized" boolean. It also has a list of strings of the overall state of each sub-state init. So, when each sub-state changes its init status, or the list of strings describing it, I wanted to dispatch to the main app state to go inspect each sub-state, and update the global init flag and set of init messages to display.

Obviously, I might be thinking about this all wrong, and there's already a solution for this that I'm not picking up on. If so, my apologies, and a pointer to that would be greatly appreciated.

Again - keep it up, man. This is great.

@StevenLambion
Copy link
Owner

Hi, and thank you! It does sound like you might be doing too much work managing state. If you haven't seen it, you should take a look at my todo reference app. The AppState in that example is a struct that contains substates as properties. Each substate is initialized in its parent's init function.

The idea is that you have a single store with a root state. This state propegates changes down to substates using pure functions (reducers) like branches on a tree. It may sound expensive to rebuild the tree each time an action is dispatched, but Swift is very good at managing the memory of value types like structs. In fact, it's so good that SwiftUI was built around it.

I may need to see the code you've written to completely understand. Maybe your substates are lazily loaded some how?

@hoopes
Copy link
Author

hoopes commented Feb 4, 2020

Yes, the substates initialization are async - they get values from an api, perhaps. In my case, they start an audio engine (audiokit.io), and retrieve stuff from firebase. So, i have an init spinner screen when the app starts, where it (currently) is monitoring a single boolean in the root state. As each of the sub-states finish their init, I'd like to dispatch an action that says "update your global app init state".

Alternatively, my init screen could monitor each of the sub-states init flags, but it feels cleaner to me to keep the overall app in one flag, know what I mean? Also, it feels like this might be a pattern useful for other things - when one action is handled, I might want to dispatch other actions that follow on from it.

For far-left-field reference, I've been doing frontend web stuff with clojurescript and re-frame (https://github.com/day8/re-frame) which allows this. I'm of course not saying you necessarily should follow that pattern, but it's been super useful to me. I also don't have 100% grasp on Combine yet, so this might all be something I just don't know yet.

I can try to come up with a simple example of this, if you think it would be helpful in any way.

Thanks!

@StevenLambion
Copy link
Owner

I initially designed this library around redux and elm, but I'm open to what works best with SwiftUI. I've heard of re-frame, but haven't used it myself.

With the library as it is, it sounds like it might be useful to have two actions that are placed inside an action plan. One action initiates the substate, and the other action tells the root state about it. Events from the auditkit.io API can then dispatch the action plan.

Another route would be middleware that subscribes to store changes. It would dispatch an action to notify the root state whenever the substate is initialized.

@hoopes
Copy link
Author

hoopes commented Feb 7, 2020

Yep, after some experience, I think I'm in good shape. Thanks so much for your help, and your library.

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

No branches or pull requests

2 participants