Skip to content

Instantly share code, notes, and snippets.

@nihil84
Last active December 13, 2022 08:53
Show Gist options
  • Save nihil84/98889ce9e22f414052de0ae460114a0d to your computer and use it in GitHub Desktop.
Save nihil84/98889ce9e22f414052de0ae460114a0d to your computer and use it in GitHub Desktop.
The store class (simplified)
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