Revert observable refactorings
This commit is contained in:
parent
2d9e862f23
commit
7c5ee69418
|
@ -7,46 +7,19 @@ namespace esphome {
|
|||
namespace ratgdo {
|
||||
|
||||
template <typename T>
|
||||
class distinct_observable;
|
||||
|
||||
|
||||
|
||||
template <typename T>
|
||||
class observable_base {
|
||||
class observable {
|
||||
public:
|
||||
template <typename Observer>
|
||||
void subscribe(Observer&& observer)
|
||||
observable(const T& value)
|
||||
: value_(value)
|
||||
{
|
||||
this->observers_.push_back(std::forward<Observer>(observer));
|
||||
}
|
||||
|
||||
void notify(T value) const
|
||||
{
|
||||
for (const auto& observer : this->observers_) {
|
||||
observer(value);
|
||||
}
|
||||
}
|
||||
|
||||
distinct_observable<T> distinct()
|
||||
{
|
||||
return std::make_shared(this);
|
||||
}
|
||||
|
||||
private:
|
||||
std::vector<std::function<void(T)>> observers_;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
class observable : public observable_base<T> {
|
||||
public:
|
||||
observable(const T& value) : value_(value) {}
|
||||
|
||||
template <typename U>
|
||||
observable& operator=(U value)
|
||||
{
|
||||
if (value != this->value_) {
|
||||
this->value_ = value;
|
||||
this->notify(value);
|
||||
this->notify();
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
@ -54,27 +27,22 @@ namespace ratgdo {
|
|||
T const* operator&() const { return &this->value_; }
|
||||
T const& operator*() const { return this->value_; }
|
||||
|
||||
private:
|
||||
T value_;
|
||||
};
|
||||
template <typename Observer>
|
||||
void subscribe(Observer&& observer)
|
||||
{
|
||||
this->observers_.push_back(std::forward<Observer>(observer));
|
||||
}
|
||||
|
||||
|
||||
|
||||
template <typename T>
|
||||
class distinct_observable : public observable<T> {
|
||||
public:
|
||||
distinct_observable(std::shared_ptr<observable<T>> inner) : inner_(inner) {
|
||||
inner.subscribe([=] (T value) {
|
||||
if (value != this->value_) {
|
||||
this->value_ = value;
|
||||
this->notify(value);
|
||||
}
|
||||
});
|
||||
void notify() const
|
||||
{
|
||||
for (const auto& observer : this->observers_) {
|
||||
observer(this->value_);
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
std::shared_ptr<observable<T>> inner_;
|
||||
T value_;
|
||||
std::vector<std::function<void(T)>> observers_;
|
||||
};
|
||||
|
||||
} // namespace ratgdo
|
||||
|
|
Loading…
Reference in New Issue