Commit 3e010905 authored by Andrii Grynenko's avatar Andrii Grynenko Committed by Facebook Github Bot

Fix Observable to not trigger unneccessary refresh if the value didn't change

Differential Revision: D5251218

fbshipit-source-id: 1ceb37dd727e8ac2fd842e2c437cdaa9017221c8
parent 5dd16dc0
......@@ -49,7 +49,10 @@ class ObserverCreator<Observable, Traits>::Context {
// callbacks (getting new value from observable and storing it into value_
// is not atomic).
std::lock_guard<std::mutex> lg(updateMutex_);
updateValue();
if (!updateValue()) {
// Value didn't change, so we can skip the version update.
return;
}
bool expected = false;
if (updateRequested_.compare_exchange_strong(expected, true)) {
......@@ -63,12 +66,14 @@ class ObserverCreator<Observable, Traits>::Context {
}
private:
void updateValue() {
bool updateValue() {
auto newValue = Traits::get(observable_);
auto newValuePtr = newValue.get();
if (!newValue) {
throw std::logic_error("Observable returned nullptr.");
}
value_.swap(newValue);
return newValuePtr != newValue.get();
}
folly::Synchronized<std::shared_ptr<const T>> value_;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment