All Downloads are FREE. Search and download functionalities are using the official Maven repository.

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()
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy