Commit f1b05f02 authored by Dominik Charousset's avatar Dominik Charousset

Add new observer::cancel utility factory

parent f06f38c6
...@@ -112,6 +112,9 @@ public: ...@@ -112,6 +112,9 @@ public:
/// Returns an observer that ignores any of its inputs. /// Returns an observer that ignores any of its inputs.
static observer ignore(); static observer ignore();
/// Returns an observer that disposes its subscription immediately.
static observer cancel();
private: private:
intrusive_ptr<impl> pimpl_; intrusive_ptr<impl> pimpl_;
}; };
...@@ -170,6 +173,26 @@ private: ...@@ -170,6 +173,26 @@ private:
flow::subscription sub_; flow::subscription sub_;
}; };
template <class T>
class canceling_observer : public flow::observer_impl_base<T> {
public:
void on_next(const T&) override {
// nop
}
void on_error(const error&) override {
// nop
}
void on_complete() override {
// nop
}
void on_subscribe(flow::subscription sub) override {
sub.dispose();
}
};
template <class OnNextSignature> template <class OnNextSignature>
struct on_next_trait; struct on_next_trait;
...@@ -262,6 +285,11 @@ observer<T> observer<T>::ignore() { ...@@ -262,6 +285,11 @@ observer<T> observer<T>::ignore() {
return observer<T>{make_counted<detail::ignoring_observer<T>>()}; return observer<T>{make_counted<detail::ignoring_observer<T>>()};
} }
template <class T>
observer<T> observer<T>::cancel() {
return observer<T>{make_counted<detail::canceling_observer<T>>()};
}
/// Creates an observer from given callbacks. /// Creates an observer from given callbacks.
/// @param on_next Callback for handling incoming elements. /// @param on_next Callback for handling incoming elements.
/// @param on_error Callback for handling an error. /// @param on_error Callback for handling an error.
......
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