Home | History | Annotate | Download | only in sources
      1 // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
      2 
      3 #pragma once
      4 
      5 #if !defined(RXCPP_SOURCES_RX_CREATE_HPP)
      6 #define RXCPP_SOURCES_RX_CREATE_HPP
      7 
      8 #include "../rx-includes.hpp"
      9 
     10 /*! \file rx-create.hpp
     11 
     12     \brief Returns an observable that executes the specified function when a subscriber subscribes to it.
     13 
     14     \tparam T  the type of the items that this observable emits
     15     \tparam OnSubscribe  the type of OnSubscribe handler function
     16 
     17     \param  os  OnSubscribe event handler
     18 
     19     \return  Observable that executes the specified function when a Subscriber subscribes to it.
     20 
     21     \sample
     22     \snippet create.cpp Create sample
     23     \snippet output.txt Create sample
     24 
     25     \warning
     26     It is good practice to check the observer's is_subscribed state from within the function you pass to create
     27     so that your observable can stop emitting items or doing expensive calculations when there is no longer an interested observer.
     28 
     29     \badcode
     30     \snippet create.cpp Create bad code
     31     \snippet output.txt Create bad code
     32 
     33     \goodcode
     34     \snippet create.cpp Create good code
     35     \snippet output.txt Create good code
     36 
     37     \warning
     38     It is good practice to use operators like observable::take to control lifetime rather than use the subscription explicitly.
     39 
     40     \goodcode
     41     \snippet create.cpp Create great code
     42     \snippet output.txt Create great code
     43 */
     44 
     45 namespace rxcpp {
     46 
     47 namespace sources {
     48 
     49 namespace detail {
     50 
     51 template<class T, class OnSubscribe>
     52 struct create : public source_base<T>
     53 {
     54     typedef create<T, OnSubscribe> this_type;
     55 
     56     typedef rxu::decay_t<OnSubscribe> on_subscribe_type;
     57 
     58     on_subscribe_type on_subscribe_function;
     59 
     60     create(on_subscribe_type os)
     61         : on_subscribe_function(std::move(os))
     62     {
     63     }
     64 
     65     template<class Subscriber>
     66     void on_subscribe(Subscriber o) const {
     67 
     68         on_exception(
     69             [&](){
     70                 this->on_subscribe_function(o);
     71                 return true;
     72             },
     73             o);
     74     }
     75 };
     76 
     77 }
     78 
     79 /*! @copydoc rx-create.hpp
     80     */
     81 template<class T, class OnSubscribe>
     82 auto create(OnSubscribe os)
     83     ->      observable<T,   detail::create<T, OnSubscribe>> {
     84     return  observable<T,   detail::create<T, OnSubscribe>>(
     85                             detail::create<T, OnSubscribe>(std::move(os)));
     86 }
     87 
     88 }
     89 
     90 }
     91 
     92 #endif
     93