#pragma once
// Spec: SPEC.md NFR-THR-01. Mutation counter plus subscribers so the UI can redraw. UI thread only.
// version() grows by one on every notifyChanged(); subscribers are called synchronously after the bump.
#include <cstdint>
#include <functional>
#include <map>
namespace dp {
class Observable {
public:
using Callback = std::function<void()>;
static constexpr std::size_t kMaxSubscribers = 64; // hard cap
std::uint64_t version() const { return version_; }
// Returns an id for unsubscribe(), 0 when the cap is reached.
int subscribe(Callback callback);
void unsubscribe(int id);
protected:
void notifyChanged();
private:
std::uint64_t version_ = 0;
int nextId_ = 1;
std::map<int, Callback> subscribers_;
};
} // namespace dp