// Copyright (c) Facebook, Inc. and its affiliates. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #pragma once #include "yarpl/single/Single.h" #include "yarpl/single/SingleSubscriptions.h" #include namespace yarpl { namespace single { class Singles { public: template static std::shared_ptr> just(const T& value) { auto lambda = [value](std::shared_ptr> observer) { observer->onSubscribe(SingleSubscriptions::empty()); observer->onSuccess(value); }; return Single::create(std::move(lambda)); } template < typename T, typename OnSubscribe, typename = typename std::enable_if>>::value>::type> static std::shared_ptr> create(OnSubscribe&& function) { return std::make_shared< FromPublisherOperator>>( std::forward(function)); } template static std::shared_ptr> error(folly::exception_wrapper ex) { auto lambda = [e = std::move(ex)](std::shared_ptr> observer) { observer->onSubscribe(SingleSubscriptions::empty()); observer->onError(e); }; return Single::create(std::move(lambda)); } template static std::shared_ptr> error(const ExceptionType& ex) { auto lambda = [ex](std::shared_ptr> observer) { observer->onSubscribe(SingleSubscriptions::empty()); observer->onError(ex); }; return Single::create(std::move(lambda)); } template static std::shared_ptr> fromGenerator(TGenerator&& generator) { auto lambda = [generator = std::forward(generator)]( std::shared_ptr> observer) mutable { observer->onSubscribe(SingleSubscriptions::empty()); observer->onSuccess(generator()); }; return Single::create(std::move(lambda)); } private: Singles() = delete; }; } // namespace single } // namespace yarpl