- Notifications
You must be signed in to change notification settings - Fork1
A µFramework for handling side effects in a Redux ReSwift applcation.
License
wickwirew/SideEffects
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
A µFramework for handling side effects in a ReduxReSwift applcation.
There are a lot of ways to manage side effects in a Redux application. Anything from using Thunks, Sagas, Observables and more. This is a different take on it from the normal approaches that plays to Swift's strengths, while taking inspiration fromredux-observables andngrx effects but without therx
part. The goal ofSideEffect
s is to allow you to easiliy, and declaritivly define a function that runs anytime anAction
of a defined type is dispatched. Allowing your views to dispatch vanilla Redux actions and be completely agnostic to any side effects.
For example if you have an actionItemSelected
that is dispatch when ever an item is selected from a list.When its fired you might want to call an API to load the record from the database.
SideEffect(of:ItemSelected.self){ action, state, dispatchin api.loadRecord(id: action.id){ response // using the dispatch function you can dispatch a success/failure action.}}
There can also be multipleSideEffect
s defined per action. So to coninute on the previous example, if you also wanted to display the item's view controller as well. I could define anotherSideEffect
to do so.
SideEffect(of:ItemSelected.self){ action, state, dispatchindispatch(SetRouteAction(...))}
Firstly, for ease of use first create a typealias for your application state's SideEffects.
typealiasSideEffect=Store<AppState>.SideEffect
Then define your applications SideEffects. The amount of SideEffects in the application can add up quick. For ease of organization it is recommended to separate them into multiple lists like so:
letmySideEffects:[SideEffect]=[SideEffect(of:ItemSelected.self){...},SideEffect(of:AnotherAction.self){...},]
Then create the middleware and add it to thestore
createSideEffectMiddleware(effects: mySideEffects+ moreSideEffects)
And thats it! 🎉
You can install SideEffects via Carthage by adding the following line to yourCartfile
:
github "wickwirew/SideEffects"
You can install SideEffects via CocoaPods by adding the following line to yourPodfile
:
pod 'SideEffects'
About
A µFramework for handling side effects in a Redux ReSwift applcation.