https://github.com/wickwirew/sideeffects
A µFramework for handling side effects in a Redux ReSwift applcation.
https://github.com/wickwirew/sideeffects
ios middleware redux reswift swift unidirectional-data-flow
Last synced: about 1 year ago
JSON representation
A µFramework for handling side effects in a Redux ReSwift applcation.
- Host: GitHub
- URL: https://github.com/wickwirew/sideeffects
- Owner: wickwirew
- License: mit
- Created: 2019-01-08T00:38:08.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2019-02-24T23:15:30.000Z (over 7 years ago)
- Last Synced: 2025-04-10T00:16:20.482Z (about 1 year ago)
- Topics: ios, middleware, redux, reswift, swift, unidirectional-data-flow
- Language: Swift
- Homepage:
- Size: 26.4 KB
- Stars: 7
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# SideEffects
A µFramework for handling side effects in a Redux [ReSwift](https://www.github.com/ReSwift/ReSwift) 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 from [redux-observables](https://github.com/redux-observable/redux-observable) and [ngrx effects](https://github.com/ngrx/effects) but without the `rx` part. The goal of `SideEffect`s is to allow you to easiliy, and declaritivly define a function that runs anytime an `Action` of a defined type is dispatched. Allowing your views to dispatch vanilla Redux actions and be completely agnostic to any side effects.
## Usage
For example if you have an action `ItemSelected` 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.
```swift
SideEffect(of: ItemSelected.self) { action, state, dispatch in
api.loadRecord(id: action.id) { response
// using the dispatch function you can dispatch a success/failure action.
}
}
```
There can also be multiple `SideEffect`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 another `SideEffect` to do so.
```swift
SideEffect(of: ItemSelected.self) { action, state, dispatch in
dispatch(SetRouteAction(...))
}
```
## Setup
Firstly, for ease of use first create a typealias for your application state's SideEffects.
```swift
typealias SideEffect = Store.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:
```swift
let mySideEffects: [SideEffect] = [
SideEffect(of: ItemSelected.self) { ... },
SideEffect(of: AnotherAction.self) { ... },
]
```
Then create the middleware and add it to the `store`
```swift
createSideEffectMiddleware(effects: mySideEffects + moreSideEffects)
```
And thats it! 🎉
## Installation
### Carthage
You can install SideEffects via Carthage by adding the following line to your `Cartfile`:
```
github "wickwirew/SideEffects"
```
### CocoaPods
You can install SideEffects via CocoaPods by adding the following line to your `Podfile`:
```
pod 'SideEffects'
```