-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathmain.cpp
126 lines (102 loc) · 3.51 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include <iostream>
#include <vector>
#include <flowcpp/flow.h>
enum class counter_action_type {
thunk,
increment,
decrement,
};
struct increment_action {
flow::any payload() const { return _payload; }
flow::any type() const { return _type; }
flow::any meta() const { return _meta; }
bool error() const { return _error; }
int _payload = {1};
counter_action_type _type = {counter_action_type::increment};
flow::any _meta;
bool _error = false;
};
struct decrement_action {
flow::any payload() const { return _payload; }
flow::any type() const { return _type; }
flow::any meta() const { return _meta; }
bool error() const { return _error; }
int _payload = {1};
counter_action_type _type = {counter_action_type::decrement};
flow::any _meta;
bool _error = false;
};
struct counter_state {
std::string to_string() { return "counter: " + std::to_string(_counter); }
int _counter{0};
};
auto reducer = [](counter_state state, flow::action action) {
int multiplier = 1;
auto type = action.type().as<counter_action_type>();
switch (type) {
case counter_action_type::decrement:
multiplier = -1;
break;
case counter_action_type::increment:
multiplier = 1;
break;
default:
break;
}
auto payload = action.payload().as<int>();
state._counter += multiplier * payload;
return state;
};
std::string to_string(counter_action_type type) {
switch (type) {
case counter_action_type::increment:
return "inc";
case counter_action_type::decrement:
return "dec";
case counter_action_type::thunk:
return "thunk";
}
}
auto logging_middleware = [](flow::basic_middleware<counter_state>) {
return [=](const flow::dispatch_t &next) {
return [=](flow::action action) {
auto next_action = next(action);
std::cout << "after dispatch: " << to_string(action.type().as<counter_action_type>()) << std::endl;
return next_action;
};
};
};
void simple_example() {
std::cout << "Start: Simple example" << std::endl;
auto store = flow::create_store_with_action<counter_state>(reducer, counter_state{}, increment_action{5});
auto disposable = store.subscribe([](counter_state state) { std::cout << state.to_string() << std::endl; });
store.dispatch(increment_action{2});
store.dispatch(decrement_action{10});
disposable.dispose(); // call dispose to stop notification prematurely
store.dispatch(increment_action{3});
store.dispatch(decrement_action{6});
std::cout << "End: Simple example " << store.state().to_string() << std::endl;
}
void thunk_middleware_example() {
std::cout << "Start: Thunk Middleware example" << std::endl;
auto store = flow::apply_middleware<counter_state>(
reducer, counter_state(), {flow::thunk_middleware<counter_state, counter_action_type>, logging_middleware});
std::cout << store.state().to_string() << std::endl;
store.dispatch(flow::thunk_action<counter_state, counter_action_type>{[&](auto dispatch, auto get_state) {
dispatch(increment_action{1});
dispatch(decrement_action{2});
dispatch(increment_action{3});
}});
store.dispatch(flow::thunk_action<counter_state, counter_action_type>{[&](auto dispatch, auto get_state) {
dispatch(increment_action{4});
dispatch(decrement_action{5});
dispatch(increment_action{6});
}});
std::cout << "End: Thunk Middleware example " << store.state().to_string() << std::endl;
}
int main() {
simple_example();
std::cout << "------------------------------" << std::endl;
thunk_middleware_example();
return 0;
}