This file contains hidden or 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
import kotlin.contracts.ExperimentalContracts | |
import kotlin.contracts.InvocationKind | |
import kotlin.contracts.contract | |
@ExperimentalContracts | |
inline fun guard(predicate: Boolean, block: () -> Nothing) { | |
contract { | |
returns() implies predicate | |
callsInPlace(block, InvocationKind.AT_MOST_ONCE) | |
} |
This file contains hidden or 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
/** | |
* Applies [transform] to values from the [Observable] and forwards values with non-null results. | |
*/ | |
inline fun <T : Any, S : Any> Observable<T>.mapNotNull( | |
crossinline transform: (T) -> S? | |
): Observable<S> { | |
return this | |
.flatMap { | |
val result = transform(it) | |
if (result == null) { |
This file contains hidden or 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
fun <T> Observable<T>.delaySucceeding(interval: Long, timeUnit: TimeUnit): Observable<T> { | |
return Observable.merge( | |
this.take(1), | |
this.skip(1).delayEach(interval, timeUnit) | |
) | |
} | |
private fun <T> Observable<T>.delayEach(interval: Long, timeUnit: TimeUnit): Observable<T> { | |
return Observable.zip( | |
this, |