https://github.com/sumory/mas
a state management library.
https://github.com/sumory/mas
actions pubsub redux state-management
Last synced: about 1 month ago
JSON representation
a state management library.
- Host: GitHub
- URL: https://github.com/sumory/mas
- Owner: sumory
- License: mit
- Created: 2019-03-16T14:58:27.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2019-03-16T14:58:47.000Z (over 7 years ago)
- Last Synced: 2025-01-31T16:56:06.891Z (over 1 year ago)
- Topics: actions, pubsub, redux, state-management
- Language: C++
- Size: 4.88 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
### Mas
a C++ port of [redux](https://redux.js.org/) library with some custom features.
```c++
#include
struct State {
int id = 1;
};
struct Action {
std::string event;
};
auto reducer = [](const State& state, const Action& action) -> State {
State new_state = state;
if (action.event == "add") {
new_state.id++;
} else if (action.event == "subtract") {
new_state.id--;
}
return new_state;
};
int main(int argc, char** argv) {
using mas::SimpleStore;
State s;
SimpleStore store(s, reducer);
store.subscribe("add", [](const State& s) {
std::cout << "receiver 1, receive add event" << std::endl;
return 0;
});
store.subscribe("add", [](const State& s) {
std::cout << "receiver 2, receive add event" << std::endl;
return 0;
});
store.subscribe("subtract", [](const State& s) {
std::cout << "receiver 3, receive subtract event" << std::endl;
return 0;
});
Action action;
{
action.event = "add";
store.dispatch(action, "add");
auto& state = store.state();
std::cout << "state id:" << state.id << std::endl;
}
{
action.event = "subtract";
store.dispatch(action, [](const Action& a) { return a.event; });
auto& state = store.state();
std::cout << "state id:" << state.id << std::endl;
}
return 0;
}
```
### License
[MIT](./LICENSE) License