2 * Copyright 2015 Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include <folly/wangle/rx/types.h> // must come first
23 #include <folly/Memory.h>
25 namespace folly { namespace wangle {
27 template <class T> struct FunctionObserver;
29 /// Observer interface. You can subclass it, or you can just use create()
30 /// to use std::functions.
33 // These are what it means to be an Observer.
34 virtual void onNext(const T&) = 0;
35 virtual void onError(Error) = 0;
36 virtual void onCompleted() = 0;
38 virtual ~Observer() = default;
40 /// Create an Observer with std::function callbacks. Handy to make ad-hoc
41 /// Observers with lambdas.
43 /// Templated for maximum perfect forwarding flexibility, but ultimately
44 /// whatever you pass in has to implicitly become a std::function for the
45 /// same signature as onNext(), onError(), and onCompleted() respectively.
46 /// (see the FunctionObserver typedefs)
47 template <class N, class E, class C>
48 static std::unique_ptr<Observer> create(
49 N&& onNextFn, E&& onErrorFn, C&& onCompletedFn)
51 return folly::make_unique<FunctionObserver<T>>(
52 std::forward<N>(onNextFn),
53 std::forward<E>(onErrorFn),
54 std::forward<C>(onCompletedFn));
57 /// Create an Observer with only onNext and onError callbacks.
58 /// onCompleted will just be a no-op.
59 template <class N, class E>
60 static std::unique_ptr<Observer> create(N&& onNextFn, E&& onErrorFn) {
61 return folly::make_unique<FunctionObserver<T>>(
62 std::forward<N>(onNextFn),
63 std::forward<E>(onErrorFn),
67 /// Create an Observer with only an onNext callback.
68 /// onError and onCompleted will just be no-ops.
70 static std::unique_ptr<Observer> create(N&& onNextFn) {
71 return folly::make_unique<FunctionObserver<T>>(
72 std::forward<N>(onNextFn),
78 /// An observer that uses std::function callbacks. You don't really want to
79 /// make one of these directly - instead use the Observer::create() methods.
81 struct FunctionObserver : public Observer<T> {
82 typedef std::function<void(const T&)> OnNext;
83 typedef std::function<void(Error)> OnError;
84 typedef std::function<void()> OnCompleted;
86 /// We don't need any fancy overloads of this constructor because that's
87 /// what Observer::create() is for.
88 template <class N = OnNext, class E = OnError, class C = OnCompleted>
89 FunctionObserver(N&& n, E&& e, C&& c)
90 : onNext_(std::forward<N>(n)),
91 onError_(std::forward<E>(e)),
92 onCompleted_(std::forward<C>(c))
95 void onNext(const T& val) override {
96 if (onNext_) onNext_(val);
99 void onError(Error e) override {
100 if (onError_) onError_(e);
103 void onCompleted() override {
104 if (onCompleted_) onCompleted_();
110 OnCompleted onCompleted_;