Commit 69ab6ff9 authored by Pranjal Raihan's avatar Pranjal Raihan Committed by Facebook GitHub Bot

Use DelayedInit for SimpleObservable

Summary: The lazy initialization logic can now be simplified.

Reviewed By: andriigrynenko

Differential Revision: D25675031

fbshipit-source-id: 048328220a2b02c13a672bebe47b79521d5f5e7a
parent 56d1379c
......@@ -74,13 +74,12 @@ struct SimpleObservable<T>::Wrapper {
template <typename T>
auto SimpleObservable<T>::getObserver() const {
folly::call_once(observerInit_, [&]() {
return observer_.try_emplace_with([&]() {
SimpleObservable<T>::Wrapper wrapper;
wrapper.context = context_;
ObserverCreator<SimpleObservable<T>::Wrapper> creator(std::move(wrapper));
observer_ = unwrap(std::move(creator).getObserver());
return unwrap(std::move(creator).getObserver());
});
return *observer_;
}
} // namespace observer
......
......@@ -19,7 +19,7 @@
#include <folly/Function.h>
#include <folly/Synchronized.h>
#include <folly/experimental/observer/Observer.h>
#include <folly/synchronization/CallOnce.h>
#include <folly/synchronization/DelayedInit.h>
namespace folly {
namespace observer {
......@@ -51,8 +51,7 @@ class SimpleObservable {
struct Wrapper;
std::shared_ptr<Context> context_;
mutable folly::once_flag observerInit_;
mutable folly::Optional<Observer<typename observer_detail::Unwrap<T>::type>>
folly::DelayedInit<Observer<typename observer_detail::Unwrap<T>::type>>
observer_;
};
} // namespace observer
......
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