{"action":"created","issue":{"url":"https://api.github.com/repos/Kotlin/kotlinx.coroutines/issues/254","repository_url":"https://api.github.com/repos/Kotlin/kotlinx.coroutines","labels_url":"https://api.github.com/repos/Kotlin/kotlinx.coroutines/issues/254/labels{/name}","comments_url":"https://api.github.com/repos/Kotlin/kotlinx.coroutines/issues/254/comments","events_url":"https://api.github.com/repos/Kotlin/kotlinx.coroutines/issues/254/events","html_url":"https://github.com/Kotlin/kotlinx.coroutines/issues/254","id":298944899,"node_id":"MDU6SXNzdWUyOTg5NDQ4OTk=","number":254,"title":"Provide abstraction for cold streams","user":{"login":"elizarov","id":478679,"node_id":"MDQ6VXNlcjQ3ODY3OQ==","avatar_url":"https://avatars2.githubusercontent.com/u/478679?v=4","gravatar_id":"","url":"https://api.github.com/users/elizarov","html_url":"https://github.com/elizarov","followers_url":"https://api.github.com/users/elizarov/followers","following_url":"https://api.github.com/users/elizarov/following{/other_user}","gists_url":"https://api.github.com/users/elizarov/gists{/gist_id}","starred_url":"https://api.github.com/users/elizarov/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/elizarov/subscriptions","organizations_url":"https://api.github.com/users/elizarov/orgs","repos_url":"https://api.github.com/users/elizarov/repos","events_url":"https://api.github.com/users/elizarov/events{/privacy}","received_events_url":"https://api.github.com/users/elizarov/received_events","type":"User","site_admin":false},"labels":[{"id":396785979,"node_id":"MDU6TGFiZWwzOTY3ODU5Nzk=","url":"https://api.github.com/repos/Kotlin/kotlinx.coroutines/labels/enhancement","name":"enhancement","color":"84b6eb","default":true}],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":41,"created_at":"2018-02-21T12:06:51Z","updated_at":"2018-08-05T13:28:18Z","closed_at":null,"author_association":"COLLABORATOR","body":"All the currently provided channel abstractions in `kotlinx.coroutines` are _hot_. The data is being produced regardless of the presence of subscriber. This is good for data sources and applications that are inherently hot, like incoming network and UI-events. \r\n\r\nHowever, hot streams are not an ideal solution for cases where data stream is produced on demand. Consider, for example, the following simple code that produces `ReceiveChannel<Int>`:\r\n\r\n\r\n```\r\nproduce<Int> { \r\n    while (true) {\r\n        val x = computeNextValue()\r\n        send(x)\r\n    } \r\n}\r\n```\r\n\r\nOne obvious downside is the `computeNextValue()` is invoked before `send`, so even when receiver is not ready, the next value gets computed. Of course, it gets suspended in `send` if there is no receiver, but it is not as lazy as you get with cold reactive Publisher/Observable/Flowable/Flux/Flow.\r\n\r\nWe need the abstraction for cold streams in `kotlinx.coroutines` that is going to be just as lazy, computing data in \"push\" mode versus \"pull\" mode of hot channels that we have now.\r\n\r\nThere are the following related discussions:\r\n* https://github.com/reactor/reactor-core/issues/979#issuecomment-351770494 describes preliminary performance test that indicates that \"push\" mode is much faster for same-thread cases.\r\n* https://github.com/Kotlin/kotlinx.coroutines/issues/113 (SPSC channels) seems to get superseded by the support of cold streams.\r\n"},"comment":{"url":"https://api.github.com/repos/Kotlin/kotlinx.coroutines/issues/comments/410520277","html_url":"https://github.com/Kotlin/kotlinx.coroutines/issues/254#issuecomment-410520277","issue_url":"https://api.github.com/repos/Kotlin/kotlinx.coroutines/issues/254","id":410520277,"node_id":"MDEyOklzc3VlQ29tbWVudDQxMDUyMDI3Nw==","user":{"login":"jmfayard","id":459464,"node_id":"MDQ6VXNlcjQ1OTQ2NA==","avatar_url":"https://avatars3.githubusercontent.com/u/459464?v=4","gravatar_id":"","url":"https://api.github.com/users/jmfayard","html_url":"https://github.com/jmfayard","followers_url":"https://api.github.com/users/jmfayard/followers","following_url":"https://api.github.com/users/jmfayard/following{/other_user}","gists_url":"https://api.github.com/users/jmfayard/gists{/gist_id}","starred_url":"https://api.github.com/users/jmfayard/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jmfayard/subscriptions","organizations_url":"https://api.github.com/users/jmfayard/orgs","repos_url":"https://api.github.com/users/jmfayard/repos","events_url":"https://api.github.com/users/jmfayard/events{/privacy}","received_events_url":"https://api.github.com/users/jmfayard/received_events","type":"User","site_admin":false},"created_at":"2018-08-05T13:28:18Z","updated_at":"2018-08-05T13:28:18Z","author_association":"NONE","body":"Would it perhaps work to annotate the Source type to both \r\n- document that a given function returns a source that is cold/hot/must be closed/ ...\r\n- gives an hint for linters, IDE integration, ..?\r\n\r\n```\r\nfun parseFile(file: File): @Cold Source<Data> = \r\n  source {  file.bufferedReader().use { \r\n         //...\r\n    } }\r\n\r\nfun postComment(comment: String) : @Hot @MustBeClosed Source<NetworkResult> {\r\n      // ...\r\n  }\r\n```"}}
{"actor":{"display_login":"jmfayard"}}