Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion binder/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ tasks.withType<Test> {
}

dependencies {
api(libs.rxjava2)
api(libs.rxjava3)
implementation(libs.rxkotlin)
implementation(libs.kotlin.stdlib)

Expand Down
2 changes: 1 addition & 1 deletion binder/detekt-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<CurrentIssues>
<ID>MaxLineLength:Connection.kt$infix</ID>
<ID>MaxLineLength:ManualLifecycle.kt$ManualLifecycle$class</ID>
<ID>ReturnCount:Consumer.kt$fun &lt;In> Consumer&lt;In>.wrapWithMiddleware( standalone: Boolean = true, name: String? = null, postfix: String? = null, wrapperOf: Any? = null ): Consumer&lt;In></ID>
<ID>ReturnCount:Consumer.kt$fun &lt;In : Any> Consumer&lt;In>.wrapWithMiddleware( standalone: Boolean = true, name: String? = null, postfix: String? = null, wrapperOf: Any? = null ): Consumer&lt;In></ID>
<ID>TooManyFunctions:Binder.kt$Binder : Disposable</ID>
<ID>UseCheckOrError:StandaloneMiddleware.kt$StandaloneMiddleware$throw IllegalStateException("Middleware was initialised in standalone mode, can't accept other connections")</ID>
</CurrentIssues>
Expand Down
39 changes: 22 additions & 17 deletions binder/src/main/java/com/badoo/binder/Binder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import com.badoo.binder.lifecycle.Lifecycle.Event.BEGIN
import com.badoo.binder.lifecycle.Lifecycle.Event.END
import com.badoo.binder.middleware.base.Middleware
import com.badoo.binder.middleware.wrapWithMiddleware
import io.reactivex.Observable
import io.reactivex.Observable.wrap
import io.reactivex.ObservableSource
import io.reactivex.Scheduler
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.disposables.Disposable
import io.reactivex.functions.Consumer
import io.reactivex.rxkotlin.plusAssign
import io.reactivex.rxjava3.core.Observable
import io.reactivex.rxjava3.core.Observable.wrap
import io.reactivex.rxjava3.core.ObservableSource
import io.reactivex.rxjava3.core.Scheduler
import io.reactivex.rxjava3.disposables.CompositeDisposable
import io.reactivex.rxjava3.disposables.Disposable
import io.reactivex.rxjava3.functions.Consumer
import io.reactivex.rxjava3.kotlin.plusAssign

class Binder(
private val lifecycle: Lifecycle? = null,
Expand Down Expand Up @@ -56,27 +56,32 @@ class Binder(
subscribeWithLifecycle(connection, middleware)
}
}

else -> subscribe(connection, middleware)
}
}

private fun <Out, In> subscribeWithLifecycle(
private fun <Out : Any, In : Any> subscribeWithLifecycle(
connection: Connection<Out, In>,
middleware: Middleware<Out, In>?
) {
connectionDisposables += wrap(connection.from)
.subscribeWithMiddleware(connection, middleware)
connection.from?.let {
connectionDisposables += wrap(it)
.subscribeWithMiddleware(connection, middleware)
}
}

private fun <Out, In> subscribe(
private fun <Out : Any, In : Any> subscribe(
connection: Connection<Out, In>,
middleware: Middleware<Out, In>?
) {
disposables += wrap(connection.from)
.subscribeWithMiddleware(connection, middleware)
connection.from?.let {
disposables += wrap(it)
.subscribeWithMiddleware(connection, middleware)
}
}

private fun <Out, In> Observable<out Out>.subscribeWithMiddleware(
private fun <Out : Any, In : Any> Observable<out Out>.subscribeWithMiddleware(
connection: Connection<Out, In>,
middleware: Middleware<Out, In>?
): Disposable =
Expand All @@ -97,7 +102,7 @@ class Binder(
}
}

private fun <Out, In> Observable<out Out>.applyTransformer(
private fun <Out : Any, In : Any> Observable<out Out>.applyTransformer(
connection: Connection<Out, In>
): Observable<In> =
connection.connector?.let {
Expand Down Expand Up @@ -153,7 +158,7 @@ class Binder(
disposables.clear()
}

private fun <T> Observable<T>.optionalObserveOn(scheduler: Scheduler?) =
private fun <T : Any> Observable<T>.optionalObserveOn(scheduler: Scheduler?) =
if (scheduler != null) {
observeOn(scheduler)
} else {
Expand Down
20 changes: 10 additions & 10 deletions binder/src/main/java/com/badoo/binder/Connection.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package com.badoo.binder

import com.badoo.binder.connector.Connector
import com.badoo.binder.connector.NotNullConnector
import io.reactivex.ObservableSource
import io.reactivex.Scheduler
import io.reactivex.functions.Consumer
import io.reactivex.rxjava3.core.ObservableSource
import io.reactivex.rxjava3.core.Scheduler
import io.reactivex.rxjava3.functions.Consumer

data class Connection<Out, In>(
data class Connection<Out : Any, In : Any>(
val from: ObservableSource<out Out>? = null,
val to: Consumer<in In>,
val connector: Connector<Out, In>? = null,
Expand All @@ -24,36 +24,36 @@ data class Connection<Out, In>(
"<${name ?: ANONYMOUS}> (${from ?: "?"} --> $to${connector?.let { " using $it" } ?: ""})"
}

infix fun <Out, In> Pair<ObservableSource<out Out>, Consumer<in In>>.using(transformer: (Out) -> In?): Connection<Out, In> =
infix fun <Out : Any, In : Any> Pair<ObservableSource<out Out>, Consumer<in In>>.using(transformer: (Out) -> In?): Connection<Out, In> =
using(NotNullConnector(transformer))

infix fun <Out, In> Pair<ObservableSource<out Out>, Consumer<in In>>.using(connector: Connector<Out, In>): Connection<Out, In> =
infix fun <Out : Any, In : Any> Pair<ObservableSource<out Out>, Consumer<in In>>.using(connector: Connector<Out, In>): Connection<Out, In> =
Connection(
from = first,
to = second,
connector = connector
)

infix fun <T> Pair<ObservableSource<out T>, Consumer<in T>>.named(name: String): Connection<T, T> =
infix fun <T : Any> Pair<ObservableSource<out T>, Consumer<in T>>.named(name: String): Connection<T, T> =
Connection(
from = first,
to = second,
name = name
)

infix fun <T> Pair<ObservableSource<out T>, Consumer<in T>>.observeOn(scheduler: Scheduler): Connection<T, T> =
infix fun <T : Any> Pair<ObservableSource<out T>, Consumer<in T>>.observeOn(scheduler: Scheduler): Connection<T, T> =
Connection(
from = first,
to = second,
observeScheduler = scheduler
)

infix fun <Out, In> Connection<Out, In>.named(name: String) =
infix fun <Out : Any, In : Any> Connection<Out, In>.named(name: String) =
copy(
name = name
)

infix fun <Out, In> Connection<Out, In>.observeOn(scheduler: Scheduler) =
infix fun <Out : Any, In : Any> Connection<Out, In>.observeOn(scheduler: Scheduler) =
copy(
observeScheduler = scheduler
)
4 changes: 2 additions & 2 deletions binder/src/main/java/com/badoo/binder/connector/Connector.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package com.badoo.binder.connector

import io.reactivex.ObservableSource
import io.reactivex.rxjava3.core.ObservableSource

interface Connector<Out, In>: (ObservableSource<out Out>) -> ObservableSource<In>
interface Connector<Out : Any, In : Any> : (ObservableSource<out Out>) -> ObservableSource<In>
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.badoo.binder.connector

import io.reactivex.Observable
import io.reactivex.Observable.wrap
import io.reactivex.ObservableSource
import io.reactivex.rxjava3.core.Observable
import io.reactivex.rxjava3.core.Observable.wrap
import io.reactivex.rxjava3.core.ObservableSource

internal class NotNullConnector<Out, In>(private val mapper: (Out) -> In?): Connector<Out, In> {
internal class NotNullConnector<Out : Any, In : Any>(
private val mapper: (Out) -> In?
) : Connector<Out, In> {
override fun invoke(element: ObservableSource<out Out>): ObservableSource<In> =
wrap(element)
.flatMap {
Expand Down
4 changes: 2 additions & 2 deletions binder/src/main/java/com/badoo/binder/lifecycle/Lifecycle.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.badoo.binder.lifecycle

import com.badoo.binder.lifecycle.internal.FromObservableSource
import io.reactivex.ObservableSource
import io.reactivex.rxjava3.core.ObservableSource

interface Lifecycle : ObservableSource<Lifecycle.Event> {

Expand All @@ -15,7 +15,7 @@ interface Lifecycle : ObservableSource<Lifecycle.Event> {
fun manual(): ManualLifecycle =
ManualLifecycle()

fun wrap(source: ObservableSource<Event>) : Lifecycle =
fun wrap(source: ObservableSource<Event>): Lifecycle =
FromObservableSource(source)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package com.badoo.binder.lifecycle
import com.badoo.binder.lifecycle.Lifecycle.Event
import com.badoo.binder.lifecycle.Lifecycle.Event.BEGIN
import com.badoo.binder.lifecycle.Lifecycle.Event.END
import io.reactivex.subjects.BehaviorSubject
import io.reactivex.rxjava3.subjects.BehaviorSubject

class ManualLifecycle(private val subject : BehaviorSubject<Event> = BehaviorSubject.create()) : Lifecycle by Lifecycle.wrap(subject) {
class ManualLifecycle(
private val subject: BehaviorSubject<Event> = BehaviorSubject.create()
) : Lifecycle by Lifecycle.wrap(subject) {

fun begin() = subject.onNext(BEGIN)
fun end() = subject.onNext(END)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.badoo.binder.lifecycle.internal

import com.badoo.binder.lifecycle.Lifecycle
import io.reactivex.ObservableSource
import io.reactivex.rxjava3.core.ObservableSource

internal class FromObservableSource(
source: ObservableSource<Lifecycle.Event>
Expand Down
6 changes: 3 additions & 3 deletions binder/src/main/java/com/badoo/binder/middleware/Consumer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import com.badoo.binder.middleware.base.Middleware
import com.badoo.binder.middleware.base.StandaloneMiddleware
import com.badoo.binder.middleware.config.Middlewares
import com.badoo.binder.middleware.config.NonWrappable
import io.reactivex.functions.Consumer
import io.reactivex.rxjava3.functions.Consumer

/**
* Wraps a Consumer<T> with Middlewares. The list of Middlewares that will be applied is resolved
Expand All @@ -19,7 +19,7 @@ import io.reactivex.functions.Consumer
* @param postfix Passed on to [ConsumerMiddleware], in most cases you shouldn't need to override this.
* @param wrapperOf Passed on to [ConsumerMiddleware], in most cases you shouldn't need to override this.
*/
fun <In> Consumer<In>.wrapWithMiddleware(
fun <In : Any> Consumer<In>.wrapWithMiddleware(
standalone: Boolean = true,
name: String? = null,
postfix: String? = null,
Expand Down Expand Up @@ -52,7 +52,7 @@ fun <In> Consumer<In>.wrapWithMiddleware(
"com.badoo.binder.middleware.wrapWithMiddleware"
)
)
fun <In: Any> Consumer<In>.wrap(
fun <In : Any> Consumer<In>.wrap(
standalone: Boolean = true,
name: String? = null,
postfix: String? = null,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package com.badoo.binder.middleware.base

import com.badoo.binder.Connection
import io.reactivex.functions.Consumer
import io.reactivex.rxjava3.functions.Consumer

abstract class Middleware<Out, In>(
abstract class Middleware<Out : Any, In : Any>(
protected val wrapped: Consumer<In>
): Consumer<In> {
) : Consumer<In> {

open fun onBind(connection: Connection<Out, In>) {
wrapped.applyIfMiddleware { onBind(connection) }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package com.badoo.binder.middleware.base

import com.badoo.binder.Connection
import io.reactivex.disposables.Disposable
import io.reactivex.rxjava3.disposables.Disposable

internal class StandaloneMiddleware<In>(
internal class StandaloneMiddleware<In : Any>(
private val wrappedMiddleware: Middleware<In, In>,
name: String? = null,
postfix: String? = null
): Middleware<In, In>(wrappedMiddleware), Disposable {
) : Middleware<In, In>(wrappedMiddleware), Disposable {

private var bound = false
private var disposed = false
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.badoo.binder.middleware.config

import com.badoo.binder.middleware.base.Middleware
import io.reactivex.functions.Consumer
import io.reactivex.rxjava3.functions.Consumer

typealias ConsumerMiddlewareFactory<T> = (Consumer<T>) -> Middleware<Any, T>
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
package com.badoo.binder.middleware.config

import com.badoo.binder.middleware.base.Middleware
import io.reactivex.functions.Consumer
import io.reactivex.rxjava3.functions.Consumer

data class MiddlewareConfiguration(
private val condition: WrappingCondition,
private val factories: List<ConsumerMiddlewareFactory<*>>
) {

fun <T> applyOn(
fun <T : Any> applyOn(
consumerToWrap: Consumer<T>,
targetToCheck: Any,
name: String?,
standalone: Boolean
): Consumer<T> {
var current = consumerToWrap
val middlewares = if (condition.shouldWrap(targetToCheck, name, standalone)) factories else listOf()
val middlewares = if (condition.shouldWrap(targetToCheck, name, standalone)) {
factories
} else {
listOf()
}
middlewares.forEach {
current = it.invoke(current) as Middleware<Any, T>
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.badoo.binder.middleware.config

interface WrappingCondition {

fun shouldWrap(target: Any, name: String?, standalone: Boolean) : Boolean
fun shouldWrap(target: Any, name: String?, standalone: Boolean): Boolean

object Always : WrappingCondition {
override fun shouldWrap(target: Any, name: String?, standalone: Boolean) =
Expand All @@ -21,12 +21,12 @@ interface WrappingCondition {
condition()
}

object IsStandalone: WrappingCondition {
object IsStandalone : WrappingCondition {
override fun shouldWrap(target: Any, name: String?, standalone: Boolean): Boolean =
standalone
}

object IsNamed: WrappingCondition {
object IsNamed : WrappingCondition {
override fun shouldWrap(target: Any, name: String?, standalone: Boolean): Boolean =
name != null
}
Expand Down
12 changes: 6 additions & 6 deletions binder/src/test/java/com/badoo/binder/BinderTest.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.badoo.binder

import com.badoo.binder.connector.Connector
import io.reactivex.Observable
import io.reactivex.ObservableSource
import io.reactivex.subjects.PublishSubject
import io.reactivex.rxjava3.core.Observable
import io.reactivex.rxjava3.core.ObservableSource
import io.reactivex.rxjava3.subjects.PublishSubject
import org.junit.jupiter.api.Test

class BinderTest {
Expand Down Expand Up @@ -36,15 +36,15 @@ class BinderTest {

@Test
fun `covariant endpoints compile for connection dsl`() {
val anyConsumer = TestConsumer<Any?>()
val anyConsumer = TestConsumer<Any>()
binder.bind(source to anyConsumer using IntToString)
}

object IntToString: (Int) -> String {
object IntToString : (Int) -> String {
override fun invoke(it: Int): String = it.toString()
}

object TestConnector: Connector<Int, String> {
object TestConnector : Connector<Int, String> {
override fun invoke(it: ObservableSource<out Int>): ObservableSource<String> =
Observable.wrap(it).flatMap {
Observable.just(it.toString(), (it + 1).toString())
Expand Down
6 changes: 3 additions & 3 deletions binder/src/test/java/com/badoo/binder/LifecycleTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package com.badoo.binder

import com.badoo.binder.lifecycle.Lifecycle
import com.badoo.binder.lifecycle.ManualLifecycle
import io.reactivex.ObservableSource
import io.reactivex.functions.Consumer
import io.reactivex.subjects.PublishSubject
import io.reactivex.rxjava3.core.ObservableSource
import io.reactivex.rxjava3.functions.Consumer
import io.reactivex.rxjava3.subjects.PublishSubject
import org.junit.jupiter.api.Test
import org.mockito.kotlin.any
import org.mockito.kotlin.mock
Expand Down
Loading