Last active
December 13, 2022 08:53
-
-
Save nihil84/98889ce9e22f414052de0ae460114a0d to your computer and use it in GitHub Desktop.
The store class (simplified)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
template <class S, class A> | |
class reducpp::store { | |
public: | |
typedef std::function<S(const S&, const A&)> reducer_t; | |
typedef std::function<void()> callback_t; | |
template <class F> | |
store(const F& reducer) : m_reducer(reducer) { m_history.push_back(S()); } | |
const S& getState() const { return m_history.back(); } | |
void dispatch(const A& action) { | |
m_history.push_back(m_reducer(m_history.back(), action)); | |
for (const callback_t& callback : m_subscriptions) { | |
callback(); | |
} | |
} | |
template <class F> | |
void subscribe(const F& callback) { m_subscriptions.push_back(callback); } | |
private: | |
const reducer_t m_reducer; | |
std::list<S> m_history; | |
std::vector<callback_t> m_subscriptions; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment