content: /*
   * MIT License
   *
   * Copyright (c) 2018 Alibaba Group
   *
   * Permission is hereby granted, free of charge, to any person obtaining a copy
   * of this software and associated documentation files (the "Software"), to deal
   * in the Software without restriction, including without limitation the rights
   * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
   * copies of the Software, and to permit persons to whom the Software is
   * furnished to do so, subject to the following conditions:
   *
   * The above copyright notice and this permission notice shall be included in all
   * copies or substantial portions of the Software.
   *
   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
   * SOFTWARE.
   */
  
  package com.tmall.wireless.tangram3.util;
  
  import io.reactivex.Completable;
  import io.reactivex.CompletableSource;
  import io.reactivex.CompletableTransformer;
  import io.reactivex.Maybe;
  import io.reactivex.MaybeSource;
  import io.reactivex.MaybeTransformer;
  import io.reactivex.Observable;
  import io.reactivex.ObservableSource;
  import io.reactivex.ObservableTransformer;
  import io.reactivex.Single;
  import io.reactivex.SingleSource;
  import io.reactivex.SingleTransformer;
  import io.reactivex.functions.Function;
  
  /**
   * Created by longerian on 2018/4/9.
   *
   * @author longerian
   * @date 2018/04/09
   */
  
  public class LifecycleTransformer<T> implements ObservableTransformer<T, T>,
      SingleTransformer<T, T>, MaybeTransformer<T, T>, CompletableTransformer {
  
      final Observable<?> mObservable;
  
      public LifecycleTransformer(Observable<?> observable) {
          mObservable = observable;
      }
  
      @Override
      public ObservableSource<T> apply(Observable<T> upstream) {
          return upstream.takeUntil(mObservable);
      }
  
      @Override
      public MaybeSource<T> apply(Maybe<T> upstream) {
          return upstream.takeUntil(mObservable.firstElement());
      }
  
      @Override
      public SingleSource<T> apply(Single<T> upstream) {
          return upstream.takeUntil(mObservable.firstOrError());
      }
  
      @Override
      public CompletableSource apply(Completable upstream) {
          return Completable.ambArray(upstream, mObservable.flatMapCompletable(
              new Function<Object, CompletableSource>() {
                  @Override
                  public CompletableSource apply(Object t) throws Exception {
                      return Completable.complete();
                  }
              }));
      }
  }
  
  