Roman Elizarov | d4dcbe2 | 2017-02-22 09:57:46 +0300 | [diff] [blame] | 1 | /* |
Roman Elizarov | 1f74a2d | 2018-06-29 19:19:45 +0300 | [diff] [blame^] | 2 | * Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. |
Roman Elizarov | d4dcbe2 | 2017-02-22 09:57:46 +0300 | [diff] [blame] | 3 | */ |
| 4 | |
| 5 | // This file was automatically generated from coroutines-guide.md by Knit tool. Do not edit. |
Roman Elizarov | a9687a3 | 2018-06-29 17:28:38 +0300 | [diff] [blame] | 6 | package kotlinx.coroutines.experimental.guide.select05 |
Roman Elizarov | d4dcbe2 | 2017-02-22 09:57:46 +0300 | [diff] [blame] | 7 | |
| 8 | import kotlinx.coroutines.experimental.* |
Roman Elizarov | 9669578 | 2017-10-01 10:48:15 -0700 | [diff] [blame] | 9 | import kotlinx.coroutines.experimental.channels.* |
| 10 | import kotlinx.coroutines.experimental.selects.* |
Roman Elizarov | 9fe5f46 | 2018-02-21 19:05:52 +0300 | [diff] [blame] | 11 | import kotlin.coroutines.experimental.* |
Roman Elizarov | d4dcbe2 | 2017-02-22 09:57:46 +0300 | [diff] [blame] | 12 | |
Roman Elizarov | 66f018c | 2017-09-29 21:39:03 +0300 | [diff] [blame] | 13 | fun switchMapDeferreds(input: ReceiveChannel<Deferred<String>>) = produce<String> { |
Roman Elizarov | a84730b | 2017-02-22 11:58:50 +0300 | [diff] [blame] | 14 | var current = input.receive() // start with first received deferred value |
Roman Elizarov | d4dcbe2 | 2017-02-22 09:57:46 +0300 | [diff] [blame] | 15 | while (isActive) { // loop while not cancelled/closed |
| 16 | val next = select<Deferred<String>?> { // return next deferred value from this select or null |
| 17 | input.onReceiveOrNull { update -> |
| 18 | update // replaces next value to wait |
| 19 | } |
| 20 | current.onAwait { value -> |
| 21 | send(value) // send value that current deferred has produced |
| 22 | input.receiveOrNull() // and use the next deferred from the input channel |
| 23 | } |
| 24 | } |
| 25 | if (next == null) { |
| 26 | println("Channel was closed") |
| 27 | break // out of loop |
| 28 | } else { |
| 29 | current = next |
| 30 | } |
| 31 | } |
| 32 | } |
| 33 | |
Roman Elizarov | 66f018c | 2017-09-29 21:39:03 +0300 | [diff] [blame] | 34 | fun asyncString(str: String, time: Long) = async { |
Roman Elizarov | d4dcbe2 | 2017-02-22 09:57:46 +0300 | [diff] [blame] | 35 | delay(time) |
| 36 | str |
| 37 | } |
| 38 | |
| 39 | fun main(args: Array<String>) = runBlocking<Unit> { |
| 40 | val chan = Channel<Deferred<String>>() // the channel for test |
Roman Elizarov | 43e3af7 | 2017-07-21 16:01:31 +0300 | [diff] [blame] | 41 | launch(coroutineContext) { // launch printing coroutine |
Roman Elizarov | d4dcbe2 | 2017-02-22 09:57:46 +0300 | [diff] [blame] | 42 | for (s in switchMapDeferreds(chan)) |
| 43 | println(s) // print each received string |
| 44 | } |
| 45 | chan.send(asyncString("BEGIN", 100)) |
| 46 | delay(200) // enough time for "BEGIN" to be produced |
| 47 | chan.send(asyncString("Slow", 500)) |
Roman Elizarov | a84730b | 2017-02-22 11:58:50 +0300 | [diff] [blame] | 48 | delay(100) // not enough time to produce slow |
Roman Elizarov | d4dcbe2 | 2017-02-22 09:57:46 +0300 | [diff] [blame] | 49 | chan.send(asyncString("Replace", 100)) |
Roman Elizarov | a84730b | 2017-02-22 11:58:50 +0300 | [diff] [blame] | 50 | delay(500) // give it time before the last one |
Roman Elizarov | d4dcbe2 | 2017-02-22 09:57:46 +0300 | [diff] [blame] | 51 | chan.send(asyncString("END", 500)) |
| 52 | delay(1000) // give it time to process |
Roman Elizarov | a84730b | 2017-02-22 11:58:50 +0300 | [diff] [blame] | 53 | chan.close() // close the channel ... |
Roman Elizarov | d4dcbe2 | 2017-02-22 09:57:46 +0300 | [diff] [blame] | 54 | delay(500) // and wait some time to let it finish |
| 55 | } |