blob: 1a214ce308971b955ae8c11ec54438805bbde3b6 [file] [log] [blame]
Roman Elizarov8a4a8e12017-03-09 19:52:58 +03001/*
Roman Elizarovdb0ef0c2019-07-03 15:02:44 +03002 * Copyright 2016-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
Roman Elizarov8a4a8e12017-03-09 19:52:58 +03003 */
4
5// This file was automatically generated from coroutines-guide-reactive.md by Knit tool. Do not edit.
Roman Elizarov0950dfa2018-07-13 10:33:25 +03006package kotlinx.coroutines.rx2.guide.context03
Roman Elizarov8a4a8e12017-03-09 19:52:58 +03007
Roman Elizarov95981f32017-03-17 18:12:04 +03008import io.reactivex.*
Roman Elizarov0950dfa2018-07-13 10:33:25 +03009import kotlinx.coroutines.*
10import kotlinx.coroutines.reactive.*
Roman Elizarovdc29b072018-09-11 18:42:03 +030011import io.reactivex.schedulers.Schedulers
Roman Elizarov0950dfa2018-07-13 10:33:25 +030012import kotlin.coroutines.CoroutineContext
Roman Elizarov8a4a8e12017-03-09 19:52:58 +030013
Vsevolod Tolstopyatovd100a3f2019-07-16 16:14:35 +030014fun rangeWithInterval(context: CoroutineContext, time: Long, start: Int, count: Int) = publish<Int>(context) {
Roman Elizarov8a4a8e12017-03-09 19:52:58 +030015 for (x in start until start + count) {
16 delay(time) // wait before sending each number
17 send(x)
18 }
19}
20
Prendota65e6c8c2018-10-17 11:51:08 +030021fun main() {
Roman Elizarovf189cec2018-09-11 19:16:49 +030022 Flowable.fromPublisher(rangeWithInterval(Dispatchers.Default, 100, 1, 3))
Roman Elizarov8a4a8e12017-03-09 19:52:58 +030023 .observeOn(Schedulers.computation()) // <-- THIS LINE IS ADDED
Roman Elizarov86349be2017-03-17 16:47:37 +030024 .subscribe { println("$it on thread ${Thread.currentThread().name}") }
Roman Elizarov8a4a8e12017-03-09 19:52:58 +030025 Thread.sleep(1000)
26}