Commit 4548ec7b authored by Andrii Grynenko's avatar Andrii Grynenko Committed by Facebook GitHub Bot

Fix makeValueObserver to call creator once on construction

Reviewed By: spalamarchuk

Differential Revision: D29784994

fbshipit-source-id: 0faac9c21f56061d8d787f8bd4e2e61ed5fcaf6c
parent b8fdbc94
......@@ -299,15 +299,16 @@ Observer<observer_detail::ResultOfUnwrapObserver<F>> makeValueObserver(
template <typename F>
Observer<observer_detail::ResultOfUnwrapSharedPtr<F>> makeValueObserver(
F&& creator) {
auto activeValue = creator();
return makeObserver([activeValue = std::move(activeValue),
creator = std::forward<F>(creator)]() mutable {
auto newValue = creator();
if (!(*activeValue == *newValue)) {
activeValue = newValue;
}
return activeValue;
});
return makeObserver(
[activeValue =
std::shared_ptr<const observer_detail::ResultOfUnwrapSharedPtr<F>>(),
creator = std::forward<F>(creator)]() mutable {
auto newValue = creator();
if (!activeValue || !(*activeValue == *newValue)) {
activeValue = newValue;
}
return activeValue;
});
}
template <typename T>
......
......@@ -585,6 +585,14 @@ TEST(Observer, MakeValueObserver) {
EXPECT_EQ(observedIds, std::vector<int>({1, 2, 3, 4, 5}));
EXPECT_EQ(observedValues, std::vector<int>({1, 2, 3}));
EXPECT_EQ(observedValues2, std::vector<int>({1, 2, 3}));
size_t creatorCalls = 0;
auto o = makeValueObserver([&] {
++creatorCalls;
return 42;
});
EXPECT_EQ(42, **o);
EXPECT_EQ(1, creatorCalls);
}
TEST(Observer, MakeStaticObserver) {
......
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