RxJava或RxKotlin中是否有生存期?

时间:2018-11-15 09:12:57

标签: java kotlin reactive-programming

JetBrains/rd库具有Lifetime的概念,可以将其视为AutoCloseableIDisposeable的反向版本(请参阅here和{{3 }}了解更多信息。

最简单的用例示例如下:

import com.jetbrains.rider.util.lifetime.Lifetime
import com.jetbrains.rider.util.lifetime.onTermination
import java.io.FileInputStream
import java.io.InputStream

class LifetimeExample(lifetime: Lifetime, private val input: InputStream) {
    init {
        lifetime.onTermination {
            input.close()
            println("File closed.")
        }
    }

    fun process() {
        println("${input.bufferedReader().readText().length} char(s) read.")
    }

    companion object {
        @JvmStatic
        fun main(vararg args: String) {
            Lifetime.using {
                LifetimeExample(it, FileInputStream("/etc/passwd")).process()
            }
        }
    }
}

hereRxJava中是否有类似的概念?

0 个答案:

没有答案
相关问题