18#ifndef LIVE_CELLS_WATCHER_HPP
19#define LIVE_CELLS_WATCHER_HPP
23#include <unordered_set>
26#include "observable.hpp"
38 class watcher :
public std::enable_shared_from_this<watcher> {
88 struct base_observer :
public observer,
public std::enable_shared_from_this<base_observer> {
91 std::unordered_set<cell> arguments;
93 virtual ~base_observer() =
default;
98 virtual void init() = 0;
107 for (
auto arg : arguments) {
121 template <
typename F>
122 struct watch_observer :
public base_observer {
126 watch_observer(F callback) : callback(callback) {}
128 watch_observer(
const watch_observer &) =
delete;
130 void init()
override {
138 bool is_updating =
false;
144 bool waiting_for_change =
false;
149 void will_update(
const key_ref &k)
override {
152 waiting_for_change =
false;
156 void update(
const key_ref &k,
bool changed)
override {
157 if (is_updating || (changed && waiting_for_change)) {
159 waiting_for_change = !changed;
171 void call_with_tracker() {
173 if (!this->arguments.count(cell)) {
174 this->arguments.emplace(cell);
176 std::static_pointer_cast<observer>(this->shared_from_this())
192 std::shared_ptr<base_observer> observer;
211 template <
typename F>
213 auto w = std::make_shared<watcher>(
fn);
static argument_tracker & global()
Definition tracker.hpp:99
A computed cell which determines its argument cells at runtime.
Definition dynamic_compute_cell.hpp:153
Defines the interface for observing changes to the value of a Cell.
Definition types.hpp:32
Handle for a cell watch function.
Definition watcher.hpp:38
void stop()
Remove the watch function.
Definition watcher.hpp:79
watcher(F callback)
Register the cell watch function callback.
Definition watcher.hpp:53
~watcher()
Remove the registered cell watch function.
Definition watcher.hpp:67
Definition boolean.hpp:26
std::shared_ptr< watcher > watch(F fn)
Register a cell watch function.
Definition watcher.hpp:212