-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #290 from LachlanMcKee/issue-289-add-rxjava3-support
[#289] Added interop-rx3 for Rxjava3 support
- Loading branch information
Showing
13 changed files
with
452 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
plugins { | ||
id("com.android.library") | ||
id("kotlin-android") | ||
id("appyx-publish-android") | ||
id("appyx-lint") | ||
id("appyx-detekt") | ||
} | ||
|
||
android { | ||
namespace = "com.bumble.appyx.interop.rx3" | ||
compileSdk = libs.versions.androidCompileSdk.get().toInt() | ||
|
||
defaultConfig { | ||
minSdk = libs.versions.androidMinSdk.get().toInt() | ||
targetSdk = libs.versions.androidTargetSdk.get().toInt() | ||
|
||
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" | ||
} | ||
testOptions { | ||
unitTests.all { | ||
it.useJUnitPlatform() | ||
} | ||
} | ||
} | ||
|
||
dependencies { | ||
api(project(":libraries:core")) | ||
api(libs.rxjava3) | ||
api(libs.rxrelay3) | ||
|
||
implementation(libs.androidx.lifecycle.java8) | ||
|
||
testImplementation(libs.junit.api) | ||
testRuntimeOnly(libs.junit.engine) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<issues format="6" by="lint 7.3.1" type="baseline" client="gradle" dependencies="false" name="AGP (7.3.1)" variant="all" version="7.3.1"> | ||
|
||
</issues> |
9 changes: 9 additions & 0 deletions
9
...aries/interop-rx3/src/main/kotlin/com/bumble/appyx/interop/rx3/connectable/Connectable.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.bumble.appyx.interop.rx3.connectable | ||
|
||
import com.bumble.appyx.core.plugin.NodeLifecycleAware | ||
import com.jakewharton.rxrelay3.Relay | ||
|
||
interface Connectable<Input, Output> : NodeLifecycleAware { | ||
val input: Relay<Input> | ||
val output: Relay<Output> | ||
} |
60 changes: 60 additions & 0 deletions
60
...ies/interop-rx3/src/main/kotlin/com/bumble/appyx/interop/rx3/connectable/NodeConnector.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package com.bumble.appyx.interop.rx3.connectable | ||
|
||
import androidx.lifecycle.Lifecycle | ||
import com.bumble.appyx.core.lifecycle.subscribe | ||
import com.jakewharton.rxrelay3.PublishRelay | ||
import com.jakewharton.rxrelay3.Relay | ||
import io.reactivex.rxjava3.core.Observer | ||
|
||
class NodeConnector<Input, Output : Any>( | ||
override val input: Relay<Input> = PublishRelay.create(), | ||
) : Connectable<Input, Output> { | ||
|
||
private val intake: Relay<Output> = PublishRelay.create() | ||
private val exhaust: Relay<Output> = PublishRelay.create() | ||
private var isFlushed = false | ||
private val outputCache = mutableListOf<Output>() | ||
|
||
override val output: Relay<Output> = object : Relay<Output>() { | ||
|
||
override fun subscribeActual(observer: Observer<in Output>) { | ||
exhaust.subscribe(observer) | ||
} | ||
|
||
override fun accept(value: Output) { | ||
intake.accept(value) | ||
} | ||
|
||
override fun hasObservers() = exhaust.hasObservers() | ||
|
||
} | ||
|
||
override fun onCreate(lifecycle: Lifecycle) { | ||
lifecycle.subscribe(onCreate = { flushOutputCache() }) | ||
} | ||
|
||
private val cacheSubscription = intake.subscribe { | ||
synchronized(this) { | ||
if (!isFlushed) { | ||
outputCache.add(it) | ||
} else { | ||
exhaust.accept(it) | ||
switchToExhaust() | ||
} | ||
} | ||
} | ||
|
||
private fun flushOutputCache() { | ||
synchronized(this) { | ||
if (isFlushed) error("Already flushed") | ||
isFlushed = true | ||
outputCache.forEach { exhaust.accept(it) } | ||
outputCache.clear() | ||
} | ||
} | ||
|
||
private fun switchToExhaust() { | ||
intake.subscribe { exhaust.accept(it) } | ||
cacheSubscription.dispose() | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
...aries/interop-rx3/src/main/kotlin/com/bumble/appyx/interop/rx3/plugin/DisposeOnDestroy.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.bumble.appyx.interop.rx3.plugin | ||
|
||
import com.bumble.appyx.core.plugin.Destroyable | ||
import com.bumble.appyx.core.plugin.Plugin | ||
import io.reactivex.rxjava3.disposables.CompositeDisposable | ||
import io.reactivex.rxjava3.disposables.Disposable | ||
|
||
private class DisposeOnDestroy(disposables: List<Disposable>) : Destroyable { | ||
private val disposable = CompositeDisposable(disposables) | ||
|
||
override fun destroy() { | ||
disposable.dispose() | ||
} | ||
} | ||
|
||
fun disposeOnDestroyPlugin(vararg disposables: Disposable): Plugin = | ||
DisposeOnDestroy(disposables.toList()) |
Oops, something went wrong.