ReactivePlusPlus
One more implementation of ReactiveX approach in C++ with care about performance and templates in mind
 
Loading...
Searching...
No Matches
interval.hpp
1// ReactivePlusPlus library
2//
3// Copyright Aleksey Loginov 2022 - present.
4// Distributed under the Boost Software License, Version 1.0.
5// (See accompanying file LICENSE_1_0.txt or copy at
6// https://www.boost.org/LICENSE_1_0.txt)
7//
8// Project home: https://github.com/victimsnino/ReactivePlusPlus
9//
10
11#pragma once
12
13#include <rpp/sources/fwd.hpp>
14#include <rpp/sources/create.hpp>
15#include <rpp/schedulers/fwd.hpp>
16#include <rpp/schedulers/trampoline_scheduler.hpp>
17
18#include <type_traits>
19
20
21IMPLEMENTATION_FILE(interval_tag);
22
23namespace rpp::observable
24{
45 template<schedulers::constraint::scheduler TScheduler /*= schedulers::trampoline */>
46 auto interval(schedulers::duration period, const TScheduler& scheduler /* = TScheduler{} */) requires rpp::details::is_header_included<rpp::details::interval_tag, TScheduler>
47 {
48 return interval(period, period, scheduler);
49 }
50
70 template<schedulers::constraint::scheduler TScheduler /*= schedulers::trampoline*/>
71 auto interval(schedulers::duration first_delay, schedulers::duration period, const TScheduler& scheduler /* = TScheduler{} */) requires rpp::details::is_header_included<rpp::details::interval_tag, TScheduler>
72 {
73 return source::create<size_t>([first_delay, period, scheduler](auto&& subscriber)
74 {
75 auto worker = scheduler.create_worker(subscriber.get_subscription());
76 worker.schedule(first_delay, [counter = size_t{}, subscriber = std::forward<decltype(subscriber)>(subscriber), period]() mutable-> schedulers::optional_duration
77 {
78 subscriber.on_next(counter++);
79 return period;
80 });
81 });
82 }
83} // namespace rpp::observable
auto interval(schedulers::duration period, const TScheduler &scheduler=TScheduler{})
Creates rpp::specific_observable which emits sequence of size_t every provided time interval.
Definition: interval.hpp:43