proxygen
|
#include <Observer.h>
Public Member Functions | |
Observer (observer_detail::Core::Ptr core) | |
Snapshot< T > | getSnapshot () const |
Snapshot< T > | operator* () const |
bool | needRefresh (const Snapshot< T > &snapshot) const |
CallbackHandle | addCallback (folly::Function< void(Snapshot< T >)> callback) const |
Private Attributes | |
observer_detail::Core::Ptr | core_ |
Friends | |
template<typename Observable , typename Traits > | |
class | ObserverCreator |
Observer - a library which lets you create objects which track updates of their dependencies and get re-computed when any of the dependencies changes.
Given an Observer, you can get a snapshot of the current version of the object it holds:
Observer<int> myObserver = ...; Snapshot<int> mySnapshot = myObserver.getSnapshot(); or simply Snapshot<int> mySnapshot = *myObserver;
Snapshot will hold a view of the object, even if object in the Observer gets updated.
What makes Observer powerful is its ability to track updates to other Observers. Imagine we have two separate Observers A and B which hold integers.
Observer<int> observerA = ...; Observer<int> observerB = ...;
To compute a sum of A and B we can create a new Observer which would track updates to A and B and re-compute the sum only when necessary.
Observer<int> sumObserver = makeObserver( [observerA, observerB] { int a = **observerA; int b = **observerB; return a + b; });
int sum = **sumObserver;
Notice that a + b will be only called when either a or b is changed. Getting a snapshot from sumObserver won't trigger any re-computation.
TLObserver is very similar to Observer, but it also keeps a thread-local cache for the observed object.
Observer<int> observer = ...; TLObserver<int> tlObserver(observer); auto& snapshot = *tlObserver;
See ObserverCreator class if you want to wrap any existing subscription API in an Observer object.
Definition at line 77 of file Observer.h.
|
explicit |
Definition at line 33 of file Observer-inl.h.
CallbackHandle folly::observer::Observer< T >::addCallback | ( | folly::Function< void(Snapshot< T >)> | callback | ) | const |
Snapshot< T > folly::observer::Observer< T >::getSnapshot | ( | ) | const |
Definition at line 24 of file Observer-inl.h.
References folly::data(), folly::gen::move, and folly::T.
|
inline |
Check if we have a newer version of the observed object than the snapshot. Snapshot should have been originally from this Observer.
Definition at line 160 of file Observer.h.
References folly::observer::Snapshot< T >::core_, folly::observer::Snapshot< T >::getVersion(), and folly::observer_detail::Core::getVersionLastChange().
|
inline |
Definition at line 152 of file Observer.h.
|
friend |
Definition at line 169 of file Observer.h.
|
private |
Definition at line 171 of file Observer.h.