io.reactivex.flowables.ConnectableFlowable Java Examples

The following examples show how to use io.reactivex.flowables.ConnectableFlowable. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ConnectConnect.java    From akarnokd-misc with Apache License 2.0 6 votes vote down vote up
@Test(timeout = 5000)
public void schedulerTestRx() throws InterruptedException {
  CountDownLatch latch = new CountDownLatch(2);

  ConnectableFlowable<Integer> connectableFlux = Flowable.just(1, 2, 3, 4, 5).publish();

  connectableFlux
      .doOnEach(System.out::println)
      .subscribe(v -> {} , e -> {}, latch::countDown);

  connectableFlux
      .observeOn(Schedulers.computation())
      .doOnEach(System.out::println)
      .map(v -> v * 10)
      .observeOn(Schedulers.single())
      .doOnEach(System.out::println)
      .subscribeOn(Schedulers.io())
      .subscribe(v -> {} , e -> {}, latch::countDown);

  Thread.sleep(100);
  
  connectableFlux.connect();
  latch.await();
}
 
Example #2
Source File: EventCapturePresenterImpl.java    From dhis2-android-capture-app with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private ConnectableFlowable<List<FieldViewModel>> getFieldFlowable() {
    return showCalculationProcessor
            .startWith(true)
            .filter(newCalculation -> newCalculation)
            .flatMap(newCalculation -> Flowable.zip(
                    eventCaptureRepository.list(),
                    eventCaptureRepository.calculate(),
                    this::applyEffects)
            ).map(fields ->
                    {
                        emptyMandatoryFields = new HashMap<>();
                        for (FieldViewModel fieldViewModel : fields) {
                            if (fieldViewModel.mandatory() && DhisTextUtils.Companion.isEmpty(fieldViewModel.value()) && !sectionsToHide.contains(fieldViewModel.programStageSection()))
                                emptyMandatoryFields.put(fieldViewModel.uid(), fieldViewModel);
                        }
                        return fields;
                    }
            )
            .publish();

}
 
Example #3
Source File: InAppMessageStreamManager.java    From firebase-android-sdk with Apache License 2.0 5 votes vote down vote up
@Inject
public InAppMessageStreamManager(
    @AppForeground ConnectableFlowable<String> appForegroundEventFlowable,
    @ProgrammaticTrigger ConnectableFlowable<String> programmaticTriggerEventFlowable,
    CampaignCacheClient campaignCacheClient,
    Clock clock,
    ApiClient apiClient,
    AnalyticsEventsManager analyticsEventsManager,
    Schedulers schedulers,
    ImpressionStorageClient impressionStorageClient,
    RateLimiterClient rateLimiterClient,
    @AppForeground RateLimit appForegroundRateLimit,
    TestDeviceHelper testDeviceHelper,
    FirebaseInstallationsApi firebaseInstallations,
    DataCollectionHelper dataCollectionHelper,
    AbtIntegrationHelper abtIntegrationHelper) {
  this.appForegroundEventFlowable = appForegroundEventFlowable;
  this.programmaticTriggerEventFlowable = programmaticTriggerEventFlowable;
  this.campaignCacheClient = campaignCacheClient;
  this.clock = clock;
  this.apiClient = apiClient;
  this.analyticsEventsManager = analyticsEventsManager;
  this.schedulers = schedulers;
  this.impressionStorageClient = impressionStorageClient;
  this.rateLimiterClient = rateLimiterClient;
  this.appForegroundRateLimit = appForegroundRateLimit;
  this.testDeviceHelper = testDeviceHelper;
  this.dataCollectionHelper = dataCollectionHelper;
  this.firebaseInstallations = firebaseInstallations;
  this.abtIntegrationHelper = abtIntegrationHelper;
}
 
Example #4
Source File: RxBusDemo_Bottom3Fragment.java    From RxJava-Android-Samples with Apache License 2.0 5 votes vote down vote up
@Override
public void onStart() {
  super.onStart();
  _disposables = new CompositeDisposable();

  ConnectableFlowable<Object> tapEventEmitter = _rxBus.asFlowable().publish();

  _disposables //
      .add(
      tapEventEmitter.subscribe(
          event -> {
            if (event instanceof RxBusDemoFragment.TapEvent) {
              _showTapText();
            }
          }));

  _disposables.add(
      tapEventEmitter
          .publish(stream -> stream.buffer(stream.debounce(1, TimeUnit.SECONDS)))
          .observeOn(AndroidSchedulers.mainThread())
          .subscribe(
              taps -> {
                _showTapCount(taps.size());
              }));

  _disposables.add(tapEventEmitter.connect());
}
 
Example #5
Source File: DoOnErrorFusion.java    From akarnokd-misc with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {
    ConnectableFlowable<Integer> f = Flowable.just(1)
            .doOnNext(i -> {
                throw new IllegalArgumentException();
            })
            .doOnError(e -> {
                throw new IllegalStateException(e);
            }).publish();
    f.subscribe(
            i -> { throw new AssertionError(); }, 
            e -> e.printStackTrace());
    f.connect();
}
 
Example #6
Source File: AppsListFragment.java    From J2ME-Loader with Apache License 2.0 5 votes vote down vote up
@SuppressLint("CheckResult")
private void initDb() {
	appRepository = new AppRepository(getActivity().getApplication(), appSort.equals("date"));
	ConnectableFlowable<List<AppItem>> listConnectableFlowable = appRepository.getAll()
			.subscribeOn(Schedulers.io()).publish();
	listConnectableFlowable
			.firstElement()
			.subscribe(list -> AppUtils.updateDb(appRepository, list));
	listConnectableFlowable
			.observeOn(AndroidSchedulers.mainThread())
			.subscribe(list -> adapter.setItems(list));
	compositeDisposable.add(listConnectableFlowable.connect());
}
 
Example #7
Source File: ForegroundNotifierTest.java    From firebase-android-sdk with Apache License 2.0 5 votes vote down vote up
@Before
public void setup() {
  foregroundNotifier = new ForegroundNotifier();
  ConnectableFlowable<String> foregroundFlowable = foregroundNotifier.foregroundFlowable();
  foregroundFlowable.connect();
  subscriber = foregroundFlowable.test();
}
 
Example #8
Source File: Callbacks.java    From contentful.java with Apache License 2.0 5 votes vote down vote up
static <O, C> CDACallback<C> subscribeAsync(
    Flowable<O> flowable, CDACallback<C> callback, CDAClient client) {
  ConnectableFlowable<O> connectable = flowable.observeOn(Schedulers.io()).publish();

  callback.setSubscription(connectable.subscribe(
      new SuccessAction<>(callback, client),
      new FailureAction(callback, client)));

  connectable.connect();

  return callback;
}
 
Example #9
Source File: TestForegroundFlowableModule.java    From firebase-android-sdk with Apache License 2.0 5 votes vote down vote up
@Provides
@Singleton
@AppForeground
public ConnectableFlowable<String> providesAppForegroundEventStream(Application application) {
  ConnectableFlowable<String> foregroundFlowable = foregroundNotifier.foregroundFlowable();
  foregroundFlowable.connect();

  application.registerActivityLifecycleCallbacks(foregroundNotifier);
  return foregroundFlowable;
}
 
Example #10
Source File: RxJavaHooksUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenConnectableFlowable_whenAssembled_shouldExecuteTheHook() {

    RxJavaPlugins.setOnConnectableFlowableAssembly(connectableFlowable -> {
        hookCalled = true;
        return connectableFlowable;
    });

    ConnectableFlowable.range(1, 10)
        .publish()
        .connect();
    assertTrue(hookCalled);
}
 
Example #11
Source File: CurrentTraceContextAssemblyTrackingMatrixTest.java    From brave with Apache License 2.0 5 votes vote down vote up
@Test public void connectableFlowable_assembleInScope_subscribeNoScope() {
  ConnectableFlowable<Integer> source, errorSource;
  try (Scope scope = currentTraceContext.newScope(assemblyContext)) {
    source = Flowable.range(1, 3)
      .doOnNext(e -> assertInAssemblyContext())
      .doOnComplete(this::assertInAssemblyContext).publish();
    errorSource = Flowable.<Integer>error(new IllegalStateException())
      .doOnError(t -> assertInAssemblyContext())
      .doOnComplete(this::assertInAssemblyContext).publish();
  }

  subscribeInNoContext(
    source.autoConnect().toObservable(), errorSource.autoConnect().toObservable()
  ).assertResult(1, 2, 3);
}
 
Example #12
Source File: CurrentTraceContextAssemblyTrackingMatrixTest.java    From brave with Apache License 2.0 5 votes vote down vote up
@Test public void connectableFlowable_assembleInScope_subscribeInScope() {
  ConnectableFlowable<Integer> source, errorSource;
  try (Scope scope = currentTraceContext.newScope(assemblyContext)) {
    source = Flowable.range(1, 3)
      .doOnNext(e -> assertInAssemblyContext())
      .doOnComplete(this::assertInAssemblyContext).publish();
    errorSource = Flowable.<Integer>error(new IllegalStateException())
      .doOnError(t -> assertInAssemblyContext())
      .doOnComplete(this::assertInAssemblyContext).publish();
  }

  subscribeInDifferentContext(
    source.autoConnect().toObservable(), errorSource.autoConnect().toObservable()
  ).assertResult(1, 2, 3);
}
 
Example #13
Source File: ProgrammaticContextualTriggerFlowableModule.java    From firebase-android-sdk with Apache License 2.0 5 votes vote down vote up
@Provides
@Singleton
@ProgrammaticTrigger
public ConnectableFlowable<String> providesProgramaticContextualTriggerStream() {

  ConnectableFlowable<String> flowable =
      Flowable.<String>create(e -> triggers.setListener((trigger) -> e.onNext(trigger)), BUFFER)
          .publish();

  flowable.connect();
  // We ignore the subscription since this connected flowable is expected to last the lifetime of
  // the app.
  return flowable;
}
 
Example #14
Source File: AnalyticsEventsModule.java    From firebase-android-sdk with Apache License 2.0 5 votes vote down vote up
@Provides
@AnalyticsListener
@Singleton
ConnectableFlowable<String> providesAnalyticsConnectorEvents(
    AnalyticsEventsManager analyticsEventsManager) {
  return analyticsEventsManager.getAnalyticsEventsFlowable();
}
 
Example #15
Source File: ForegroundFlowableModule.java    From firebase-android-sdk with Apache License 2.0 5 votes vote down vote up
@Provides
@Singleton
@AppForeground
public ConnectableFlowable<String> providesAppForegroundEventStream(Application application) {
  ForegroundNotifier notifier = new ForegroundNotifier();
  ConnectableFlowable<String> foregroundFlowable = notifier.foregroundFlowable();
  foregroundFlowable.connect();

  application.registerActivityLifecycleCallbacks(notifier);
  return foregroundFlowable;
}
 
Example #16
Source File: CurrentTraceContextAssemblyTrackingMatrixTest.java    From brave with Apache License 2.0 5 votes vote down vote up
@Test public void connectableFlowable_assembleNoScope_subscribeInScope() {
  ConnectableFlowable<Integer> source = Flowable.range(1, 3)
    .doOnNext(e -> assertInSubscribeContext())
    .doOnComplete(this::assertInSubscribeContext).publish();
  ConnectableFlowable<Integer> errorSource = Flowable.<Integer>error(new IllegalStateException())
    .doOnError(t -> assertInSubscribeContext())
    .doOnComplete(this::assertInSubscribeContext).publish();

  subscribeInDifferentContext(
    source.autoConnect().toObservable(), errorSource.autoConnect().toObservable()
  ).assertResult(1, 2, 3);
}
 
Example #17
Source File: FlowableOnAssemblyConnectable.java    From RxJava2Debug with Apache License 2.0 4 votes vote down vote up
FlowableOnAssemblyConnectable(ConnectableFlowable<T> source) {
    this.source = source;
    this.assembled = new RxJavaAssemblyException();
}
 
Example #18
Source File: TraceContextConnectableFlowable.java    From brave with Apache License 2.0 4 votes vote down vote up
TraceContextConnectableFlowable(
  ConnectableFlowable<T> source, CurrentTraceContext contextScoper, TraceContext assembled) {
  this.source = source;
  this.contextScoper = contextScoper;
  this.assembled = assembled;
}
 
Example #19
Source File: Wrappers.java    From brave with Apache License 2.0 4 votes vote down vote up
public static <T> ConnectableFlowable<T> wrap(
  ConnectableFlowable<T> source, CurrentTraceContext contextScoper, TraceContext assembled) {
  return new TraceContextConnectableFlowable<>(source, contextScoper, assembled);
}
 
Example #20
Source File: StreamModelTaskRunner.java    From incubator-gobblin with Apache License 2.0 4 votes vote down vote up
protected void run() throws Exception {
  long maxWaitInMinute = taskState.getPropAsLong(ConfigurationKeys.FORK_MAX_WAIT_MININUTES, ConfigurationKeys.DEFAULT_FORK_MAX_WAIT_MININUTES);

  // Get the fork operator. By default IdentityForkOperator is used with a single branch.
  ForkOperator forkOperator = closer.register(this.taskContext.getForkOperator());

  RecordStreamWithMetadata<?, ?> stream = this.extractor.recordStream(this.shutdownRequested);
  // This prevents emitting records until a connect() call is made on the connectable stream
  ConnectableFlowable connectableStream = stream.getRecordStream().publish();

  // The cancel is not propagated to the extractor's record generator when it has been turned into a hot Flowable
  // by publish, so set the shutdownRequested flag on cancel to stop the extractor
  Flowable streamWithShutdownOnCancel = connectableStream.doOnCancel(() -> this.shutdownRequested.set(true));

  stream = stream.withRecordStream(streamWithShutdownOnCancel);

  stream = stream.mapRecords(r -> {
    this.task.onRecordExtract();
    return r;
  });
  if (this.task.isStreamingTask()) {

    // Start watermark manager and tracker
    if (this.watermarkTracker.isPresent()) {
      this.watermarkTracker.get().start();
    }
    this.watermarkManager.get().start();

    ((StreamingExtractor) this.taskContext.getRawSourceExtractor()).start(this.watermarkStorage.get());

    stream = stream.mapRecords(r -> {
      AcknowledgableWatermark ackableWatermark = new AcknowledgableWatermark(r.getWatermark());
      if (watermarkTracker.isPresent()) {
        watermarkTracker.get().track(ackableWatermark);
      }
      r.addCallBack(ackableWatermark);
      return r;
    });
  }

  // Use the recordStreamProcessor list if it is configured. This list can contain both all RecordStreamProcessor types
  if (!this.recordStreamProcessors.isEmpty()) {
    for (RecordStreamProcessor streamProcessor : this.recordStreamProcessors) {
      stream = streamProcessor.processStream(stream, this.taskState);
    }
  } else {
    if (this.converter instanceof MultiConverter) {
      // if multiconverter, unpack it
      for (Converter cverter : ((MultiConverter) this.converter).getConverters()) {
        stream = cverter.processStream(stream, this.taskState);
      }
    } else {
      stream = this.converter.processStream(stream, this.taskState);
    }
  }
  stream = this.rowChecker.processStream(stream, this.taskState);

  Forker.ForkedStream<?, ?> forkedStreams = new Forker().forkStream(stream, forkOperator, this.taskState);

  boolean isForkAsync = !this.task.areSingleBranchTasksSynchronous(this.taskContext) || forkedStreams.getForkedStreams().size() > 1;
  int bufferSize =
      this.taskState.getPropAsInt(ConfigurationKeys.FORK_RECORD_QUEUE_CAPACITY_KEY, ConfigurationKeys.DEFAULT_FORK_RECORD_QUEUE_CAPACITY);

  for (int fidx = 0; fidx < forkedStreams.getForkedStreams().size(); fidx ++) {
    RecordStreamWithMetadata<?, ?> forkedStream = forkedStreams.getForkedStreams().get(fidx);
    if (forkedStream != null) {
      if (isForkAsync) {
        forkedStream = forkedStream.mapStream(f -> f.observeOn(Schedulers.from(this.taskExecutor.getForkExecutor()), false, bufferSize));
      }
      Fork fork = new Fork(this.taskContext, forkedStream.getGlobalMetadata().getSchema(), forkedStreams.getForkedStreams().size(), fidx, this.taskMode);
      fork.consumeRecordStream(forkedStream);
      this.forks.put(Optional.of(fork), Optional.of(Futures.immediateFuture(null)));
      this.task.configureStreamingFork(fork);
    }
  }

  connectableStream.connect();

  if (!ExponentialBackoff.awaitCondition().callable(() -> this.forks.keySet().stream().map(Optional::get).allMatch(Fork::isDone)).
      initialDelay(1000L).maxDelay(1000L).maxWait(TimeUnit.MINUTES.toMillis(maxWaitInMinute)).await()) {
    throw new TimeoutException("Forks did not finish withing specified timeout.");
  }
}
 
Example #21
Source File: RequestContextConnectableFlowable.java    From armeria with Apache License 2.0 4 votes vote down vote up
RequestContextConnectableFlowable(ConnectableFlowable<T> source, RequestContext assemblyContext) {
    this.source = source;
    this.assemblyContext = assemblyContext;
}
 
Example #22
Source File: InAppMessageStreamManagerTest.java    From firebase-android-sdk with Apache License 2.0 4 votes vote down vote up
@Before
public void setup() {
  MockitoAnnotations.initMocks(this);
  ConnectableFlowable<String> appForegroundEventFlowable =
      Flowable.<String>create(e -> appForegroundEmitter = e, BUFFER).publish();
  appForegroundEventFlowable.connect();

  ConnectableFlowable<String> analyticsEventsFlowable =
      Flowable.<String>create(e -> analyticsEmitter = e, BUFFER).publish();
  analyticsEventsFlowable.connect();
  when(analyticsEventsManager.getAnalyticsEventsFlowable()).thenReturn(analyticsEventsFlowable);

  ConnectableFlowable<String> programmaticTriggerFlowable =
      Flowable.<String>create(e -> programmaticTriggerEmitter = e, BUFFER).publish();
  programmaticTriggerFlowable.connect();

  InAppMessageStreamManager streamManager =
      new InAppMessageStreamManager(
          appForegroundEventFlowable,
          programmaticTriggerFlowable,
          campaignCacheClient,
          new FakeClock(NOW),
          mockApiClient,
          analyticsEventsManager,
          schedulers,
          impressionStorageClient,
          rateLimiterClient,
          appForegroundRateLimit,
          testDeviceHelper,
          firebaseInstallations,
          dataCollectionHelper,
          abtIntegrationHelper);
  subscriber = streamManager.createFirebaseInAppMessageStream().test();
  when(application.getApplicationContext()).thenReturn(application);
  when(dataCollectionHelper.isAutomaticDataCollectionEnabled()).thenReturn(true);
  when(impressionStorageClient.clearImpressions(any(FetchEligibleCampaignsResponse.class)))
      .thenReturn(Completable.complete());
  when(rateLimiterClient.isRateLimited(appForegroundRateLimit)).thenReturn(Single.just(false));
  when(campaignCacheClient.get()).thenReturn(Maybe.empty());
  when(campaignCacheClient.put(any(FetchEligibleCampaignsResponse.class)))
      .thenReturn(Completable.complete());
  when(impressionStorageClient.isImpressed(any(ThickContent.class)))
      .thenReturn(Single.just(false));
  when(impressionStorageClient.getAllImpressions()).thenReturn(Maybe.just(CAMPAIGN_IMPRESSIONS));
  when(firebaseInstallations.getId()).thenReturn(Tasks.forResult(INSTALLATION_ID));
  when(firebaseInstallations.getToken(false))
      .thenReturn(Tasks.forResult(INSTALLATION_TOKEN_RESULT));
}
 
Example #23
Source File: TestForegroundNotifier.java    From firebase-android-sdk with Apache License 2.0 4 votes vote down vote up
@Override
public ConnectableFlowable<String> foregroundFlowable() {
  return foregroundSubject.toFlowable(BackpressureStrategy.BUFFER).publish();
}
 
Example #24
Source File: AnalyticsEventsManager.java    From firebase-android-sdk with Apache License 2.0 4 votes vote down vote up
public ConnectableFlowable<String> getAnalyticsEventsFlowable() {
  return flowable;
}
 
Example #25
Source File: UniversalComponent.java    From firebase-android-sdk with Apache License 2.0 4 votes vote down vote up
@AnalyticsListener
ConnectableFlowable<String> analyticsEventsFlowable();
 
Example #26
Source File: UniversalComponent.java    From firebase-android-sdk with Apache License 2.0 4 votes vote down vote up
@ProgrammaticTrigger
ConnectableFlowable<String> programmaticContextualTriggerFlowable();
 
Example #27
Source File: UniversalComponent.java    From firebase-android-sdk with Apache License 2.0 4 votes vote down vote up
@AppForeground
ConnectableFlowable<String> appForegroundEventFlowable();
 
Example #28
Source File: ForegroundNotifier.java    From firebase-android-sdk with Apache License 2.0 4 votes vote down vote up
/** @returns a {@link ConnectableFlowable} representing a stream of foreground events */
public ConnectableFlowable<String> foregroundFlowable() {
  return foregroundSubject.toFlowable(BackpressureStrategy.BUFFER).publish();
}