• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

badoo/Reaktive: Kotlin multi-platform implementation of Reactive Extensions

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

badoo/Reaktive

开源软件地址(OpenSource Url):

https://github.com/badoo/Reaktive

开源编程语言(OpenSource Language):

Kotlin 99.5%

开源软件介绍(OpenSource Introduction):

Maven Central Build Status License kotlinlang|reaktive

Kotlin multiplatform implementation of Reactive Extensions.

Should you have any questions or feedback welcome to the Kotlin Slack channel: #reaktive

Setup

There are a number of modules published to Maven Central:

  • reaktive - the main Reaktive library (multiplatform)
  • reaktive-annotations - collection of annotations (mutiplatform)
  • reaktive-testing - testing utilities (multiplatform)
  • utils - some utilities like Clock, AtomicReference, Lock, etc. (multiplatform)
  • coroutines-interop - Kotlin coroutines interoperability helpers (multiplatform)
  • rxjava2-interop - RxJava v2 interoperability helpers (JVM and Android)
  • rxjava3-interop - RxJava v3 interoperability helpers (JVM and Android)

Configuring dependencies

kotlin {
    sourceSets {
        commonMain {
            dependencies {
                implementation 'com.badoo.reaktive:reaktive:<version>'
                implementation 'com.badoo.reaktive:reaktive-annotations:<version>'
                implementation 'com.badoo.reaktive:coroutines-interop:<version>' // For interop with coroutines
                implementation 'com.badoo.reaktive:rxjava2-interop:<version>' // For interop with RxJava v2
                implementation 'com.badoo.reaktive:rxjava3-interop:<version>' // For interop with RxJava v3
            }
        }

        commonTest {
            dependencies {
                implementation 'com.badoo.reaktive:reaktive-testing:<version>'
            }
        }
    }
}

Features:

  • Multiplatform: JVM, Android, iOS, macOS, watchOS, tvOS, JavaScript, Linux X64, Linux ARM 32 hfp
  • Schedulers support:
    • computationScheduler - fixed thread pool equal to a number of cores
    • ioScheduler - unbound thread pool with caching policy
    • newThreadScheduler - creates a new thread for each unit of work
    • singleScheduler - executes tasks on a single shared background thread
    • trampolineScheduler - queues tasks and executes them on one of the participating threads
    • mainScheduler - executes tasks on main thread
  • True multithreading for Kotlin/Native (there are some limitations)
  • Thread local subscriptions without freezing for Kotlin/Native
  • Supported sources: Observable, Maybe, Single, Completable
  • Subjects: PublishSubject, BehaviorSubject, ReplaySubject, UnicastSubject
  • Interoperability with Kotlin Coroutines: conversions between coroutines (including Flow) and Reaktive
  • Interoperability with RxJava2 and RxJava3: conversion of sources between Reaktive and RxJava, ability to reuse RxJava's schedulers

Reaktive and the old (strict) Kotlin/Native memory model

The old (strict) Kotlin Native memory model and concurrency are very special. In general shared mutable state between threads is not allowed. Since Reaktive supports multithreading in Kotlin Native, please read the following documents before using it:

Object detachment is relatively difficult to achieve and is very error-prone when the objects are created from outside and are not fully managed by the library. This is why Reaktive prefers frozen state. Here are some hints:

  • Any callback (and any captured objects) submitted to a Scheduler will be frozen
  • subscribeOn freezes both its upstream source and downstream observer, all the Disposables (upstream's and downstream's) are frozen as well, all the values (including errors) are not frozen by the operator
  • observeOn freezes only its downstream observer and all the values (including errors) passed through it, plus all the Disposables, upstream source is not frozen by the operator
  • Other operators that use scheduler (like debounce, timer, delay, etc.) behave same as observeOn in most of the cases

Thread local tricks to avoid freezing

Sometimes freezing is not acceptable, e.g. we might want to load some data in background and then update the UI. Obviously UI can not be frozen. With Reaktive it is possible to achieve such a behaviour in two ways:

Use threadLocal operator:

val values = mutableListOf<Any>()
var isFinished = false

observable<Any> { emitter ->
    // Background job
}
    .subscribeOn(ioScheduler)
    .observeOn(mainScheduler)
    .threadLocal()
    .doOnBeforeNext { values += it } // Callback is not frozen, we can updated the mutable list
    .doOnBeforeFinally { isFinished = true } // Callback is not frozen, we can change the flag
    .subscribe()

Set isThreadLocal flag to true in subscribe operator:

val values = mutableListOf<Any>()
var isComplete = false

observable<Any> { emitter ->
    // Background job
}
    .subscribeOn(ioScheduler)
    .observeOn(mainScheduler)
    .subscribe(
        isThreadLocal = true,
        onNext = { values += it }, // Callback is not frozen, we can updated the mutable list
        onComplete = { isComplete = true } // Callback is not frozen, we can change the flag
    )

In both cases subscription (subscribe call) must be performed on the Main thread.

Reaktive and the new (relaxed) Kotlin/Native memory model

The new (relaxed) Kotlin/Native memory model allows passing objects between threads without freezing. When using this memory model, there is no need to use the threadLocal operator/argument anymore. Please make sure that you also disabled freezing as described in the documentation.

Coroutines interop

This functionality is provided by the coroutines-interop module which is published in two versions:

  • coroutines-interop:<version> is based on stable kotlinx.coroutines - use this variant with the stable version of coroutines and with the old (strict) memory model.
  • coroutines-interop:<version>-nmtc is based on work-in-progress multi-threaded kotlinx.coroutines - use this variant with either the multi-threaded version of coroutines or the new (relaxed) memory model.

Coroutines interop based on stable kotlinx.coroutines

There are few important limitations:

  • Neither Job nor CoroutineContext can be frozen (until release of the multi-threaded coroutines).
  • Because of the first limitation all xxxFromCoroutine {} builders and Flow.asObservable() converter are executed inside runBlocking block in Kotlin/Native and should be subscribed on a background Scheduler.

Consider the following example for corutines-interop:

singleFromCoroutine {
    // This block will be executed inside `runBlocking` in Kotlin/Native
}
    .subscribeOn(ioScheduler) // Switching to a background thread is necessary
    .observeOn(mainScheduler)
    .subscribe { /* Get the result here */ }

Please note that Ktor uses multi-threaded coroutines by default. If you are using Ktor, please use coroutines-interop module based on multi-threaded coroutines and proceed to the next Readme secion.

Coroutines interop based on multi-threaded kotlinx.coroutines

The multi-threaded kotlinx.coroutines variant lifts some unpleasant restrictions - both Job and CoroutineContext can be frozen.

So there is one crucial difference - all xxxFromCoroutine {} builders and Flow.asObservable() converter are executed asynchronously in all targets (including Kotlin/Native), so can be subscribed on any scheduler.

Notes:

  • Because multi-threaded coroutines are work-in-progress, there are possible issues.
  • Ktor can be used out of the box without any known limitations
Coroutines interop general limitations

Converters Scheduler.asCoroutineDispatcher() and CoroutineContext.asScheduler() are available only in JVM and JS currently.

Subscription management with DisposableScope

Reaktive provides an easy way to manage subscriptions: DisposableScope.

Take a look at the following examples:

val scope =
    disposableScope {
        observable.subscribeScoped(...) // Subscription will be disposed when the scope is disposed

        doOnDispose {
            // Will be called when the scope is disposed
        }

        someDisposable.scope() // `someDisposable` will be disposed when the scope is disposed
    }

// At some point later
scope.dispose()
class MyPresenter(
    private val view: MyView,
    private val longRunningAction: Completable
) : DisposableScope by DisposableScope() {

    init {
        doOnDispose {
            // Will be called when the presenter is disposed
        }
    }

    fun load() {
        view.showProgressBar()

        // Subscription will be disposed when the presenter is disposed
        longRunningAction.subscribeScoped(onComplete = view::hideProgressBar)
    }
}

class MyActivity : AppCompatActivity(), DisposableScope by DisposableScope() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        MyPresenter(...).scope()
    }

    override fun onDestroy() {
        dispose()

        super.onDestroy()
    }
}

Reaktive and Swift interoperability

Please see the corresponding documentation page: Reaktive and Swift interoperability.

Samples:




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap