Skip to content

Instantly share code, notes, and snippets.

@AlvarezAriel
Created November 29, 2017 19:51
Show Gist options
  • Select an option

  • Save AlvarezAriel/3eff3928a2404dd6ac137e00fbca4ed7 to your computer and use it in GitHub Desktop.

Select an option

Save AlvarezAriel/3eff3928a2404dd6ac137e00fbca4ed7 to your computer and use it in GitHub Desktop.
Implementación naive del punto 1. No sirve para otros puntos porque, por ejemplo, no admite más de una subscripción a la vez. Es sólo para que sepan por dónde empezar si están trabados.
package ayudita
object ObservableFactory {
def create[T](f:(MySubscriber[T])=>Unit): MyObservable[T] = {
new MyObservable[T](f)
}
}
class MyObservable[T](val act:(MySubscriber[T])=>Unit) {
def subscribe(subscriber:MySubscriber[T]) {
act(subscriber)
}
}
trait MySubscriber[-T] {
def onNext(t:T)
def onError()
def onCompleted()
}
object Ejemplo extends App {
val o: MyObservable[Int] = ObservableFactory.create[Int] {
subscriber =>
Range(1, 5).foreach(subscriber.onNext)
subscriber.onCompleted()
}
println("Recien ahora se emiten elementos")
o.subscribe(new MySubscriber[Int] {
override def onError(): Unit = println("Error")
override def onCompleted(): Unit = println("Completado")
override def onNext(t: Int): Unit = println(s"Evento $t")
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment