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