Exemplo n.º 1
0
  public static ConnectableObservable<Data> addCaches(final CacheObservable... ts) {
    ArrayList<Observable<Data>> list = new ArrayList<>();
    for (CacheObservable t : ts) list.add(t.observable);
    ConnectableObservable<Data> co =
        concat(from(list))
            .first(
                new Func1<Data, Boolean>() {
                  @Override
                  public Boolean call(Data data) {
                    return (data != null && data.isAvailable() && data.isLastest());
                  }
                })
            .publish();
    co.subscribe(
        new Subscriber<Data>() {
          @Override
          public void onCompleted() {}

          @Override
          public void onError(Throwable e) {}

          @Override
          public void onNext(Data data) {
            for (CacheObservable t : ts) {
              t.save(data);
            }
          }
        });
    return co;
  }
Exemplo n.º 2
0
  @Override
  public void call(Subscriber<? super TResult> child) {
    Observable<TResult> observable;
    ConnectableObservable<TIntermediate> connectable;
    try {
      connectable = new OperatorMulticast<TInput, TIntermediate>(source, subjectFactory);

      observable = resultSelector.call(connectable);
    } catch (Throwable t) {
      child.onError(t);
      return;
    }

    final SafeSubscriber<TResult> s = new SafeSubscriber<TResult>(child);

    observable.unsafeSubscribe(s);

    connectable.connect(
        new Action1<Subscription>() {
          @Override
          public void call(Subscription t1) {
            s.add(t1);
          }
        });
  }
Exemplo n.º 3
0
  public void exampleReplayWithBufferSize() throws InterruptedException {
    ConnectableObservable<Long> source =
        Observable.interval(1000, TimeUnit.MILLISECONDS).take(5).replay(2);

    source.connect();
    Thread.sleep(4500);
    source.subscribe(System.out::println);

    // 2
    // 3
    // 4
  }
Exemplo n.º 4
0
  @Test
  public void testReplayWithBufferSize() {
    TestScheduler scheduler = Schedulers.test();
    TestSubscriber<Long> tester = new TestSubscriber<>();

    ConnectableObservable<Long> source =
        Observable.interval(1000, TimeUnit.MILLISECONDS, scheduler).take(5).replay(2, scheduler);

    source.connect();
    scheduler.advanceTimeBy(4500, TimeUnit.MILLISECONDS);
    source.subscribe(tester);
    scheduler.triggerActions();
    tester.assertReceivedOnNext(Arrays.asList(2L, 3L));
    scheduler.advanceTimeBy(500, TimeUnit.MILLISECONDS);
    tester.assertReceivedOnNext(Arrays.asList(2L, 3L, 4L));
  }
  @Override
  public void onStart() {
    super.onStart();
    _subscriptions = new CompositeSubscription();

    ConnectableObservable<Object> tapEventEmitter = _rxBus.toObserverable().publish();

    _subscriptions //
        .add(
        bindSupportFragment(this, tapEventEmitter) //
            .subscribe(
                new Action1<Object>() {
                  @Override
                  public void call(Object event) {
                    if (event instanceof RxBusDemoFragment.TapEvent) {
                      _showTapText();
                    }
                  }
                }));

    _subscriptions //
        .add(
        tapEventEmitter
            .publish(
                new Func1<Observable<Object>, Observable<List<Object>>>() {
                  @Override
                  public Observable<List<Object>> call(Observable<Object> stream) {
                    return stream.buffer(stream.debounce(1, TimeUnit.SECONDS));
                  }
                })
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(
                new Action1<List<Object>>() {
                  @Override
                  public void call(List<Object> taps) {
                    _showTapCount(taps.size());
                  }
                }));

    _subscriptions.add(tapEventEmitter.connect());
  }
Exemplo n.º 6
0
  @Test
  public void testReplayWithTime() throws InterruptedException {
    TestScheduler scheduler = Schedulers.test();
    TestSubscriber<Long> tester = new TestSubscriber<>();

    ConnectableObservable<Long> source =
        Observable.interval(1000, TimeUnit.MILLISECONDS, scheduler)
            .take(5)
            .replay(2000, TimeUnit.MILLISECONDS, scheduler);

    source.connect();
    scheduler.advanceTimeBy(4500, TimeUnit.MILLISECONDS);
    source.subscribe(tester);
    tester.assertReceivedOnNext(Arrays.asList(2L, 3L));
    scheduler.advanceTimeBy(500, TimeUnit.MILLISECONDS);
    tester.assertReceivedOnNext(Arrays.asList(2L, 3L, 4L));

    //		2
    //		3
    //		4
  }
Exemplo n.º 7
0
  public void exampleReplay() throws InterruptedException {
    ConnectableObservable<Long> cold = Observable.interval(200, TimeUnit.MILLISECONDS).replay();
    cold.connect();

    System.out.println("Subscribe first");
    cold.subscribe(i -> System.out.println("First: " + i));
    Thread.sleep(700);
    System.out.println("Subscribe second");
    cold.subscribe(i -> System.out.println("Second: " + i));
    Thread.sleep(500);

    // Subscribe first
    // First: 0
    // First: 1
    // First: 2
    // Subscribe second
    // Second: 0
    // Second: 1
    // Second: 2
    // First: 3
    // Second: 3
  }
Exemplo n.º 8
0
  @Test
  public void testReplay() throws InterruptedException {
    TestScheduler scheduler = Schedulers.test();
    TestSubscriber<Long> tester1 = new TestSubscriber<>();
    TestSubscriber<Long> tester2 = new TestSubscriber<>();

    ConnectableObservable<Long> cold =
        Observable.interval(200, TimeUnit.MILLISECONDS, scheduler).replay();
    Subscription connection = cold.connect();

    cold.subscribe(tester1);
    scheduler.advanceTimeBy(700, TimeUnit.MILLISECONDS);
    tester1.assertReceivedOnNext(Arrays.asList(0L, 1L, 2L));
    tester2.assertReceivedOnNext(Arrays.asList());

    cold.subscribe(tester2);
    tester1.assertReceivedOnNext(Arrays.asList(0L, 1L, 2L));
    tester2.assertReceivedOnNext(Arrays.asList(0L, 1L, 2L));
    scheduler.advanceTimeBy(500, TimeUnit.MILLISECONDS);
    tester1.assertReceivedOnNext(Arrays.asList(0L, 1L, 2L, 3L, 4L, 5L));
    tester2.assertReceivedOnNext(Arrays.asList(0L, 1L, 2L, 3L, 4L, 5L));

    connection.unsubscribe();
  }
Exemplo n.º 9
0
  public static void main(String[] args) {

    Observable<String> lineInput =
        Observable.create(
            (Subscriber<? super String> subscriber) -> {
              if (subscriber.isUnsubscribed()) return;

              BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

              String line = null;

              try {
                while (!subscriber.isUnsubscribed() && (line = reader.readLine()) != null) {
                  subscriber.onNext(line);
                }

                if (!subscriber.isUnsubscribed()) {
                  subscriber.onCompleted();
                }
              } catch (IOException e) {
                subscriber.onError(e);
              }
            });

    ConnectableObservable<String> sourceObs = lineInput.publish();

    Function<Pattern, Observable<Integer>> lineParser =
        (pattern) ->
            sourceObs
                .filter(
                    line -> {
                      return pattern.matcher(line).matches();
                    })
                .map(
                    line -> {
                      return Integer.parseInt(line.substring(line.indexOf("=") + 1).trim());
                    });

    Subscriber<Integer> whenUnsub =
        new SafeSubscriber<Integer>(
            new Subscriber<Integer>() {

              @Override
              public void onCompleted() {}

              @Override
              public void onError(Throwable throwable) {}

              @Override
              public void onNext(Integer s) {
                System.out.println(s);
              }
            });

    whenUnsub.add(
        Subscriptions.create(
            () -> {
              System.out.println("unsubscribe callback");
            }));

    Observable.combineLatest(
            lineParser.apply(Pattern.compile("^a.*")).startWith(0),
            lineParser.apply(Pattern.compile("^b.*")).startWith(0),
            (x, y) -> x + y)
        .subscribe(whenUnsub);

    sourceObs.connect();
  }