https://github.com/francomelandri/redux-swift
Simple redux flow in swift
https://github.com/francomelandri/redux-swift
redux swift
Last synced: 6 months ago
JSON representation
Simple redux flow in swift
- Host: GitHub
- URL: https://github.com/francomelandri/redux-swift
- Owner: FrancoMelandri
- Created: 2018-06-23T08:37:00.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2018-06-25T10:31:50.000Z (over 7 years ago)
- Last Synced: 2025-02-05T04:51:17.094Z (8 months ago)
- Topics: redux, swift
- Language: Swift
- Homepage:
- Size: 8.79 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# redux-swift
Try to implement a simple and tiny redux flow in swift.
My goal is to reproduce exactly the same result as in the javascript file below:
```
'use strict'let redux = require ('redux');
function counter(state = 0, action) {
switch (action.type) {
case 'INCREMENT':
return state + 1
case 'DECREMENT':
return state - 1
default:
return state
}
};let store = redux.createStore(counter);
store.subscribe(() => {
console.log(store.getState())
}
);store.dispatch ( { type: 'INCREMENT' });
store.dispatch ( { type: 'INCREMENT' });
store.dispatch ( { type: 'DECREMENT' });
store.dispatch ( { type: 'NOP' });
```Output:
```
1
2
1
1
```You can clone the repo and launch the test using the command:
```
> swift test
```You may also run the main application using the command:
```
> swift run
```The main entry point looks like:
```
import Foundation
import redux_swiftlet state = MyState()
let store = Store(state)
let actionInc = Action(type: "INC")
let actionDec = Action(type: "DEC")
let actionNop = Action(type: "NOP")var callback: () -> Void = {
print(store.getState()?.counter ?? 0)
}
store.subscribe(callback)store.dispatch(actionInc)
store.dispatch(actionInc)
store.dispatch(actionDec)
store.dispatch(actionNop)
```and the output is:
```
1
2
1
1
```