|
| 1 | +/* |
| 2 | + * Copyright (c) 2014-2015 Alexandru Nedelcu |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package monifu.reactive.operators |
| 18 | + |
| 19 | +import monifu.concurrent.cancelables.{BooleanCancelable, RefCountCancelable} |
| 20 | +import monifu.reactive.Ack.{Cancel, Continue} |
| 21 | +import monifu.reactive._ |
| 22 | +import monifu.reactive.internals._ |
| 23 | +import monifu.reactive.observers.SynchronousObserver |
| 24 | +import scala.concurrent.Future |
| 25 | + |
| 26 | +object switch { |
| 27 | + /** |
| 28 | + * Implementation for [[Observable.concat]]. |
| 29 | + */ |
| 30 | + def apply[T, U](source: Observable[T])(implicit ev: T <:< Observable[U]): Observable[U] = |
| 31 | + Observable.create { subscriber:Subscriber[U] => |
| 32 | + implicit val s = subscriber.scheduler |
| 33 | + val observerU = subscriber.observer |
| 34 | + |
| 35 | + source.unsafeSubscribe(new SynchronousObserver[T] { self => |
| 36 | + // Global subscription, is canceled by the downstream |
| 37 | + // observer and if canceled all streaming is supposed to stop |
| 38 | + private[this] val upstream = BooleanCancelable() |
| 39 | + |
| 40 | + // MUST BE synchronized by `self` |
| 41 | + private[this] var ack: Future[Ack] = Continue |
| 42 | + // To be accessed only in `self.onNext` |
| 43 | + private[this] var current: BooleanCancelable = null |
| 44 | + |
| 45 | + private[this] val refCount = RefCountCancelable { |
| 46 | + self.synchronized { |
| 47 | + ack.onContinueSignalComplete(observerU) |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + def onNext(childObservable: T) = { |
| 52 | + if (upstream.isCanceled) Cancel else { |
| 53 | + // canceling current observable in order to |
| 54 | + // start the new stream |
| 55 | + val activeSubscription = refCount.acquire() |
| 56 | + if (current != null) current.cancel() |
| 57 | + current = activeSubscription |
| 58 | + |
| 59 | + childObservable.unsafeSubscribe(new Observer[U] { |
| 60 | + def onNext(elem: U) = self.synchronized { |
| 61 | + if (upstream.isCanceled || activeSubscription.isCanceled) |
| 62 | + Cancel |
| 63 | + else { |
| 64 | + ack = ack.onContinueStreamOnNext(observerU, elem) |
| 65 | + ack.ifCanceledDoCancel(upstream) |
| 66 | + ack |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + def onError(ex: Throwable): Unit = |
| 71 | + self.onError(ex) |
| 72 | + |
| 73 | + def onComplete(): Unit = { |
| 74 | + // NOTE: we aren't sending this onComplete signal downstream to our observerU |
| 75 | + // we will eventually do that after all of them are complete |
| 76 | + activeSubscription.cancel() |
| 77 | + } |
| 78 | + }) |
| 79 | + |
| 80 | + Continue |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + def onError(ex: Throwable): Unit = |
| 85 | + self.synchronized { |
| 86 | + if (!upstream.isCanceled) { |
| 87 | + upstream.cancel() |
| 88 | + ack.onContinueSignalError(observerU, ex) |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + def onComplete(): Unit = { |
| 93 | + refCount.cancel() |
| 94 | + } |
| 95 | + }) |
| 96 | + } |
| 97 | +} |
0 commit comments