io.reactivex.CompletableSource Java Examples

The following examples show how to use io.reactivex.CompletableSource. 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: Transformers.java    From mobius with Apache License 2.0 6 votes vote down vote up
/**
 * Creates an {@link ObservableTransformer} that will flatten the provided {@link Consumer} into
 * the stream as a {@link Completable} every time it receives an effect from the upstream effects
 * observable. This will result in calling the consumer on the specified scheduler, and passing it
 * the requested effect object.
 *
 * @param doEffect the {@link Consumer} to be run every time the effect is requested
 * @param scheduler the {@link Scheduler} to be used when invoking the consumer
 * @param <F> the type of Effect this transformer handles
 * @param <E> these transformers are for effects that do not result in any events; however, they
 *     still need to share the same Event type
 * @return an {@link ObservableTransformer} that can be used with a {@link
 *     SubtypeEffectHandlerBuilder}.
 */
static <F, E> ObservableTransformer<F, E> fromConsumer(
    final Consumer<F> doEffect, @Nullable final Scheduler scheduler) {
  return new ObservableTransformer<F, E>() {
    @Override
    public ObservableSource<E> apply(Observable<F> effectStream) {
      return effectStream
          .flatMapCompletable(
              new Function<F, CompletableSource>() {
                @Override
                public CompletableSource apply(final F effect) throws Exception {
                  Completable completable =
                      Completable.fromAction(
                          new Action() {
                            @Override
                            public void run() throws Exception {
                              doEffect.accept(effect);
                            }
                          });
                  return scheduler == null ? completable : completable.subscribeOn(scheduler);
                }
              })
          .toObservable();
    }
  };
}
 
Example #2
Source File: Transformers.java    From mobius with Apache License 2.0 6 votes vote down vote up
/**
 * Creates an {@link ObservableTransformer} that will flatten the provided {@link Action} into the
 * stream as a {@link Completable} every time it receives an effect from the upstream effects
 * observable. This Completable will be subscribed on the specified {@link Scheduler}. This will
 * result in calling the provided Action on the specified scheduler every time an effect is
 * dispatched to the created effect transformer.
 *
 * @param doEffect the {@link Action} to be run every time the effect is requested
 * @param scheduler the {@link Scheduler} that the action should be run on
 * @param <F> the type of Effect this transformer handles
 * @param <E> these transformers are for effects that do not result in any events; however, they
 *     still need to share the same Event type
 * @return an {@link ObservableTransformer} that can be used with a {@link
 *     SubtypeEffectHandlerBuilder}.
 */
static <F, E> ObservableTransformer<F, E> fromAction(
    final Action doEffect, @Nullable final Scheduler scheduler) {
  return new ObservableTransformer<F, E>() {
    @Override
    public ObservableSource<E> apply(Observable<F> effectStream) {
      return effectStream
          .flatMapCompletable(
              new Function<F, CompletableSource>() {
                @Override
                public CompletableSource apply(F f) throws Exception {
                  return scheduler == null
                      ? Completable.fromAction(doEffect)
                      : Completable.fromAction(doEffect).subscribeOn(scheduler);
                }
              })
          .toObservable();
    }
  };
}
 
Example #3
Source File: SchedulerTransformer.java    From Collection-Android with MIT License 6 votes vote down vote up
@Override
public CompletableSource apply(Completable upstream) {
    switch (mSchedulerType) {
        case _main:
            return upstream.observeOn(AndroidSchedulers.mainThread());
        case _io:
            return upstream.observeOn(RxSchedulerUtils.io(mIOExecutor));
        case _io_main:
            return upstream
                    .subscribeOn(RxSchedulerUtils.io(mIOExecutor))
                    .unsubscribeOn(RxSchedulerUtils.io(mIOExecutor))
                    .observeOn(AndroidSchedulers.mainThread());
        case _io_io:
            return upstream
                    .subscribeOn(RxSchedulerUtils.io(mIOExecutor))
                    .unsubscribeOn(RxSchedulerUtils.io(mIOExecutor))
                    .observeOn(RxSchedulerUtils.io(mIOExecutor));
        default:
            break;
    }
    return upstream;
}
 
Example #4
Source File: RxBleConnectionImpl.java    From RxAndroidBle with Apache License 2.0 6 votes vote down vote up
@Override
public Completable writeDescriptor(
        @NonNull final UUID serviceUuid, @NonNull final UUID characteristicUuid, @NonNull final UUID descriptorUuid,
        @NonNull final byte[] data
) {
    return discoverServices()
            .flatMap(new Function<RxBleDeviceServices, SingleSource<BluetoothGattDescriptor>>() {
                @Override
                public SingleSource<BluetoothGattDescriptor> apply(RxBleDeviceServices rxBleDeviceServices) {
                    return rxBleDeviceServices.getDescriptor(serviceUuid, characteristicUuid, descriptorUuid);
                }
            })
            .flatMapCompletable(new Function<BluetoothGattDescriptor, CompletableSource>() {
                @Override
                public CompletableSource apply(BluetoothGattDescriptor bluetoothGattDescriptor) {
                    return writeDescriptor(bluetoothGattDescriptor, data);
                }
            });
}
 
Example #5
Source File: Wrappers.java    From brave with Apache License 2.0 5 votes vote down vote up
public static Completable wrap(
  CompletableSource source, CurrentTraceContext contextScoper, TraceContext assembled) {
  if (source instanceof Callable) {
    return new TraceContextCallableCompletable<>(source, contextScoper, assembled);
  }
  return new TraceContextCompletable(source, contextScoper, assembled);
}
 
Example #6
Source File: InTransactionCompletable.java    From vertx-rx with Apache License 2.0 5 votes vote down vote up
@Override
public CompletableSource apply(Completable upstream) {
  return sqlConnection.rxSetAutoCommit(false)
    .andThen(upstream)
    .andThen(sqlConnection.rxCommit())
    .onErrorResumeNext(throwable -> {
      return sqlConnection.rxRollback().onErrorComplete()
        .andThen(sqlConnection.rxSetAutoCommit(true).onErrorComplete())
        .andThen(Completable.error(throwable));
    }).andThen(sqlConnection.rxSetAutoCommit(true));
}
 
Example #7
Source File: RxFragment.java    From SimpleCropView with MIT License 5 votes vote down vote up
private Disposable loadImage(final Uri uri) {
  mSourceUri = uri;
  return new RxPermissions(getActivity()).request(Manifest.permission.WRITE_EXTERNAL_STORAGE)
      .filter(new Predicate<Boolean>() {
        @Override public boolean test(@io.reactivex.annotations.NonNull Boolean granted)
            throws Exception {
          return granted;
        }
      })
      .flatMapCompletable(new Function<Boolean, CompletableSource>() {
        @Override
        public CompletableSource apply(@io.reactivex.annotations.NonNull Boolean aBoolean)
            throws Exception {
          return mCropView.load(uri)
              .useThumbnail(true)
              .initialFrameRect(mFrameRect)
              .executeAsCompletable();
        }
      })
      .subscribeOn(Schedulers.newThread())
      .observeOn(AndroidSchedulers.mainThread())
      .subscribe(new Action() {
        @Override public void run() throws Exception {
        }
      }, new Consumer<Throwable>() {
        @Override public void accept(@NonNull Throwable throwable) throws Exception {
        }
      });
}
 
Example #8
Source File: LifecycleTransformer.java    From recyclerview-binder with Apache License 2.0 5 votes vote down vote up
@Override
public CompletableSource apply(Completable upstream) {
    return Completable.ambArray(upstream, observable.flatMapCompletable(new Function<Object, Completable>() {
        @Override
        public Completable apply(Object ignore) throws Exception {
            return Completable.error(new CancellationException());
        }
    }));
}
 
Example #9
Source File: StompClient.java    From StompProtocolAndroid with MIT License 5 votes vote down vote up
@SuppressLint("CheckResult")
private void sendHeartBeat(@NonNull String pingMessage) {
    Completable completable = connectionProvider.send(pingMessage);
    CompletableSource connectionComplete = getConnectionStream()
            .filter(isConnected -> isConnected)
            .firstElement().ignoreElement();
    completable.startWith(connectionComplete)
            .onErrorComplete()
            .subscribe();
}
 
Example #10
Source File: StompClient.java    From StompProtocolAndroid with MIT License 5 votes vote down vote up
public Completable send(@NonNull StompMessage stompMessage) {
    Completable completable = connectionProvider.send(stompMessage.compile(legacyWhitespace));
    CompletableSource connectionComplete = getConnectionStream()
            .filter(isConnected -> isConnected)
            .firstElement().ignoreElement();
    return completable
            .startWith(connectionComplete);
}
 
Example #11
Source File: SimpleRequestTransformer.java    From My-MVP with Apache License 2.0 5 votes vote down vote up
@Override
public CompletableSource apply(Completable upstream) {
    if (mLifecycleTransformer != null) {
        upstream = upstream.compose(mLifecycleTransformer);
    }
    return upstream.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread());
}
 
Example #12
Source File: SimpleRequestResponseTransformer.java    From My-MVP with Apache License 2.0 5 votes vote down vote up
@Override
public CompletableSource apply(Completable upstream) {
    if (mLifecycleTransformer != null) {
        upstream = upstream.compose(mLifecycleTransformer);
    }
    return upstream.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread());
}
 
Example #13
Source File: RxLifecycleTransformer.java    From pandroid with Apache License 2.0 5 votes vote down vote up
/**
 * TakeUntil n'existe pas sur un completable. Amb permet de la premère émission
 *
 * @param upstream
 * @return
 */
@Override
public CompletableSource apply(Completable upstream) {
    return Completable.ambArray(upstream, mObservable.flatMapCompletable(new Function<Object, CompletableSource>() {
        @Override
        public CompletableSource apply(@NonNull Object o) throws Exception {
            return Completable.error(new CancellationException());
        }
    }));
}
 
Example #14
Source File: MainObserverTransformer.java    From pandroid with Apache License 2.0 5 votes vote down vote up
@Override
public CompletableSource apply(Completable upstream) {
    Completable tObservable = upstream
            .observeOn(AndroidSchedulers.mainThread());
    if (provider == null) {
        return tObservable;
    }
    return tObservable.compose(RxLifecycleDelegate.<T>bindLifecycle(provider));
}
 
Example #15
Source File: LifecycleTransformer.java    From Tangram-Android with MIT License 5 votes vote down vote up
@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();
            }
        }));
}
 
Example #16
Source File: EventStatsVerticle.java    From vertx-in-action with MIT License 5 votes vote down vote up
private CompletableSource publishThroughput(List<KafkaConsumerRecord<String, JsonObject>> records) {
  KafkaProducerRecord<String, JsonObject> record = KafkaProducerRecord.create("event-stats.throughput", new JsonObject()
    .put("seconds", 5)
    .put("count", records.size())
    .put("throughput", (((double) records.size()) / 5.0d)));
  return producer.rxWrite(record);
}
 
Example #17
Source File: AbstractSubscriberAndProducerTest.java    From reactive-grpc with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private static void racePauseResuming(final TestCallStreamObserver<?> downstream, int times) {
    Observable.range(0, times)
              .concatMapCompletable(new Function<Integer, CompletableSource>() {
                  @Override
                  public CompletableSource apply(Integer i) {
                      return Completable
                          .fromAction(new Action() {
                              @Override
                              public void run() {
                                  downstream.resume();
                              }
                          })
                          .subscribeOn(Schedulers.computation())
                          .andThen(Completable
                              .fromAction(
                                  new Action() {
                                      @Override
                                      public void run() {
                                          downstream.pause();
                                      }
                                  }
                              )
                              .subscribeOn(Schedulers.computation())
                          );
                  }
              })
              .blockingAwait();

    downstream.pause();
    executorService.execute(new Runnable() {
        @Override
        public void run() {
            downstream.resume();
        }
    });
}
 
Example #18
Source File: NotificationAndIndicationManager.java    From RxAndroidBle with Apache License 2.0 5 votes vote down vote up
@NonNull
static Completable writeClientCharacteristicConfig(
        final BluetoothGattCharacteristic bluetoothGattCharacteristic,
        final DescriptorWriter descriptorWriter,
        final byte[] value
) {
    final BluetoothGattDescriptor descriptor = bluetoothGattCharacteristic.getDescriptor(CLIENT_CHARACTERISTIC_CONFIG_UUID);
    if (descriptor == null) {
        return Completable.error(new BleCannotSetCharacteristicNotificationException(
                bluetoothGattCharacteristic,
                BleCannotSetCharacteristicNotificationException.CANNOT_FIND_CLIENT_CHARACTERISTIC_CONFIG_DESCRIPTOR,
                null
        ));
    }

    return descriptorWriter.writeDescriptor(descriptor, value)
            .onErrorResumeNext(new Function<Throwable, CompletableSource>() {
                @Override
                public CompletableSource apply(Throwable throwable) {
                    return Completable.error(new BleCannotSetCharacteristicNotificationException(
                            bluetoothGattCharacteristic,
                            BleCannotSetCharacteristicNotificationException.CANNOT_WRITE_CLIENT_CHARACTERISTIC_CONFIG_DESCRIPTOR,
                            throwable
                    ));
                }
            });
}
 
Example #19
Source File: LifecycleTransformer.java    From Tangram-Android with MIT License 5 votes vote down vote up
@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();
            }
        }));
}
 
Example #20
Source File: LifecycleTransformer.java    From Tangram-Android with MIT License 5 votes vote down vote up
@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();
            }
        }));
}
 
Example #21
Source File: Interactor.java    From RIBs with Apache License 2.0 4 votes vote down vote up
@Override
public final CompletableSource requestScope() {
  return LifecycleScopes.resolveScopeFromLifecycle(this);
}
 
Example #22
Source File: WorkerScopeProvider.java    From RIBs with Apache License 2.0 4 votes vote down vote up
@Override
public CompletableSource requestScope() {
  return workerLifecycleObservable.skip(1).firstElement().ignoreElement();
}
 
Example #23
Source File: Presenter.java    From RIBs with Apache License 2.0 4 votes vote down vote up
@Override
public CompletableSource requestScope() {
  return lifecycleRelay.skip(1).firstElement().ignoreElement();
}
 
Example #24
Source File: RibActivity.java    From RIBs with Apache License 2.0 4 votes vote down vote up
@Override
public CompletableSource requestScope() {
  return LifecycleScopes.resolveScopeFromLifecycle(this);
}
 
Example #25
Source File: RequestContextScalarCallableCompletable.java    From armeria with Apache License 2.0 4 votes vote down vote up
RequestContextScalarCallableCompletable(CompletableSource source, RequestContext assemblyContext) {
    this.source = source;
    this.assemblyContext = assemblyContext;
}
 
Example #26
Source File: RequestContextCompletable.java    From armeria with Apache License 2.0 4 votes vote down vote up
RequestContextCompletable(CompletableSource source, RequestContext assemblyContext) {
    this.source = source;
    this.assemblyContext = assemblyContext;
}
 
Example #27
Source File: RequestContextCallableCompletable.java    From armeria with Apache License 2.0 4 votes vote down vote up
RequestContextCallableCompletable(CompletableSource source, RequestContext assemblyContext) {
    this.source = source;
    this.assemblyContext = assemblyContext;
}
 
Example #28
Source File: EventStatsVerticle.java    From vertx-in-action with MIT License 4 votes vote down vote up
private CompletableSource publishUserActivityUpdate(JsonObject data) {
  return producer.rxWrite(
    KafkaProducerRecord.create("event-stats.user-activity.updates", data.getString("username"), data));
}
 
Example #29
Source File: TraceContextCompletable.java    From brave with Apache License 2.0 4 votes vote down vote up
TraceContextCompletable(
  CompletableSource source, CurrentTraceContext contextScoper, TraceContext assembled) {
  this.source = source;
  this.contextScoper = contextScoper;
  this.assembled = assembled;
}
 
Example #30
Source File: TraceContextCallableCompletable.java    From brave with Apache License 2.0 4 votes vote down vote up
TraceContextCallableCompletable(
  CompletableSource source, CurrentTraceContext contextScoper, TraceContext assembled) {
  this.source = source;
  this.contextScoper = contextScoper;
  this.assembled = assembled;
}