我如何实现这样的情景:在创建Observable之后添加元素,是否能够做到?在观察者模式中,我只需触发事件等操作。你有什么想法吗?
import rx.lang.scala._
val target = Observable(1,2,3,4)
val subscription1 = target subscribe(println(_))
val subscription2 = target subscribe(println(_))
def addToObservable(toAdd: Int, target: Observable[Int]): Observable[Int] = {
target/*.addElementAndNotifyObservers(toAdd)*/
}
addToObservable(4, target) //should print 4 on all subscriptions
addToObservable(6, target) //should print 6 on all subscriptions