com.noheltcj.rxcommon.operators.MapOperator.kt Maven / Gradle / Ivy
package com.noheltcj.rxcommon.operators
import com.noheltcj.rxcommon.Source
import com.noheltcj.rxcommon.disposables.Disposable
import com.noheltcj.rxcommon.disposables.Disposables
import com.noheltcj.rxcommon.emitters.ColdEmitter
import com.noheltcj.rxcommon.emitters.Emitter
import com.noheltcj.rxcommon.observers.AllObserver
import com.noheltcj.rxcommon.observers.Observer
class MapOperator(private val upstream: Source, private val transform: (U) -> E) : Operator() {
override val emitter: Emitter = ColdEmitter {}
override fun subscribe(observer: Observer): Disposable {
emitter.addObserver(observer)
val upstreamDisposable = upstream.subscribe(AllObserver (
onNext = { emitter.next(transform(it)) },
onError = { emitter.terminate(it) },
onComplete = { emitter.complete() }
))
return Disposables.create {
emitter.removeObserver(observer)
upstreamDisposable.dispose()
}
}
}