io.reactivex.CompletableEmitter Java Examples

The following examples show how to use io.reactivex.CompletableEmitter. 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: RealValueStore.java    From RxStore with Apache License 2.0 6 votes vote down vote up
@Override @NonNull public Completable observeClear() {
  return Completable.create(new CompletableOnSubscribe() {
    @Override public void subscribe(final CompletableEmitter emitter) throws Exception {
      runInWriteLock(readWriteLock, new ThrowingRunnable() {
        @Override public void run() throws Exception {
          if (file.exists() && !file.delete()) {
            throw new IOException("Clear operation on store failed.");
          } else {
            emitter.onComplete();
          }

          updateSubject.onNext(ValueUpdate.<T>empty());
        }
      });
    }
  });
}
 
Example #2
Source File: RxActionDelegate.java    From pandroid with Apache License 2.0 6 votes vote down vote up
public static Completable completable(final OnSubscribeAction<Void> subscribe){
    return Completable.create(new CompletableOnSubscribe() {
        @Override
        public void subscribe(final CompletableEmitter emitter) throws Exception {
            RxActionDelegate<Void> delegate = new RxActionDelegate<>(new ActionDelegate<Void>() {
                @Override
                public void onSuccess(Void result) {
                    emitter.onComplete();
                }

                @Override
                public void onError(Exception e) {
                    emitter.onError(e);
                }
            });
            emitter.setDisposable(delegate);
            subscribe.subscribe(delegate);
        }
    });
}
 
Example #3
Source File: MaybeConsumers.java    From science-journal with Apache License 2.0 6 votes vote down vote up
/**
 * Given an operation that takes a {@link MaybeConsumer<Success>}, create a JavaRX {@link
 * Completable} that succeeds iff the operation does.
 *
 * <p>Example:
 *
 * <pre>
 *     // update the experiment, and then log that it was successful
 *     DataController dc = getDataController();
 *     MaybeConsumers.buildCompleteable(mc -> dc.updateExperiment(e.getExperimentId(), mc))
 *                   .subscribe(() -> log("successfully updated!"));
 * </pre>
 */
public static Completable buildCompleteable(
    io.reactivex.functions.Consumer<MaybeConsumer<Success>> c) {
  return Completable.create(
      new CompletableOnSubscribe() {
        @Override
        public void subscribe(CompletableEmitter emitter) throws Exception {
          c.accept(
              new MaybeConsumer<Success>() {
                @Override
                public void success(Success value) {
                  emitter.onComplete();
                }

                @Override
                public void fail(Exception e) {
                  emitter.onError(e);
                }
              });
        }
      });
}
 
Example #4
Source File: Rx2Apollo.java    From apollo-android with MIT License 6 votes vote down vote up
/**
 * Converts an {@link ApolloPrefetch} to a synchronous Completable
 *
 * @param prefetch the ApolloPrefetch to convert
 * @return the converted Completable
 * @throws NullPointerException if prefetch == null
 */
@NotNull
@CheckReturnValue
public static Completable from(@NotNull final ApolloPrefetch prefetch) {
  checkNotNull(prefetch, "prefetch == null");

  return Completable.create(new CompletableOnSubscribe() {
    @Override public void subscribe(final CompletableEmitter emitter) {
      cancelOnCompletableDisposed(emitter, prefetch);
      prefetch.enqueue(new ApolloPrefetch.Callback() {
        @Override public void onSuccess() {
          if (!emitter.isDisposed()) {
            emitter.onComplete();
          }
        }

        @Override public void onFailure(@NotNull ApolloException e) {
          Exceptions.throwIfFatal(e);
          if (!emitter.isDisposed()) {
            emitter.onError(e);
          }
        }
      });
    }
  });
}
 
Example #5
Source File: CompletableEmitterMqttActionListenerTest.java    From rxmqtt with Apache License 2.0 6 votes vote down vote up
@Test
public void whenTheConstructorIsCalledWithAValidEmitterThenGetOnErrorReturnsTheEmitter() {

    // Given
    final CompletableEmitter emitter = Mockito
            .mock(CompletableEmitter.class);
    final Throwable ex = Mockito.mock(Throwable.class);
    final CompletableEmitterMqttActionListener listener = new CompletableEmitterMqttActionListener(
            emitter) {

        @Override
        public void onSuccess(final IMqttToken asyncActionToken) {
            // Not invoked
        }
    };

    // When
    final OnError onError = listener.getOnError();
    onError.onError(ex);

    // Then
    Mockito.verify(emitter).onError(ex);
}
 
Example #6
Source File: ConnectFactoryTest.java    From rxmqtt with Apache License 2.0 5 votes vote down vote up
@Test
public void whenOnSuccessIsCalledThenObserverOnNextAndOnCompletedAreCalled()
        throws Exception {
    final CompletableEmitter observer = Mockito
            .mock(CompletableEmitter.class);
    final ConnectActionListener listener = new ConnectFactory.ConnectActionListener(
            observer);
    final IMqttToken asyncActionToken = Mockito.mock(IMqttToken.class);
    listener.onSuccess(asyncActionToken);
    Mockito.verify(observer).onComplete();
}
 
Example #7
Source File: CompletableOnSubscribeExecuteAsBlocking.java    From storio with Apache License 2.0 5 votes vote down vote up
@Override
public void subscribe(@NonNull CompletableEmitter emitter) throws Exception {
    try {
        preparedOperation.executeAsBlocking();
        emitter.onComplete();
    } catch (Exception e) {
        emitter.onError(e);
    }
}
 
Example #8
Source File: CropImageView.java    From SimpleCropView with MIT License 5 votes vote down vote up
/**
 * Load image from Uri with RxJava2
 *
 * @param sourceUri Image Uri
 *
 * @see #load(Uri)
 *
 * @return Completable of loading image
 */
public Completable loadAsCompletable(final Uri sourceUri, final boolean useThumbnail,
    final RectF initialFrameRect) {
  return Completable.create(new CompletableOnSubscribe() {

    @Override public void subscribe(@NonNull final CompletableEmitter emitter) throws Exception {

      mInitialFrameRect = initialFrameRect;
      mSourceUri = sourceUri;

      if (useThumbnail) {
        applyThumbnail(sourceUri);
      }

      final Bitmap sampled = getImage(sourceUri);

      mHandler.post(new Runnable() {
        @Override public void run() {
          mAngle = mExifRotation;
          setImageDrawableInternal(new BitmapDrawable(getResources(), sampled));
          emitter.onComplete();
        }
      });
    }
  }).doOnSubscribe(new Consumer<Disposable>() {
    @Override public void accept(@NonNull Disposable disposable) throws Exception {
      mIsLoading.set(true);
    }
  }).doFinally(new Action() {
    @Override public void run() throws Exception {
      mIsLoading.set(false);
    }
  });
}
 
Example #9
Source File: SignOutOnSubscribe.java    From rxfirebase with Apache License 2.0 5 votes vote down vote up
@Override
public void subscribe(CompletableEmitter emitter) {
    if (!emitter.isDisposed()) {
        instance.signOut();
        emitter.onComplete();
    }
}
 
Example #10
Source File: DisconnectFactoryTest.java    From rxmqtt with Apache License 2.0 5 votes vote down vote up
@Test
public void whenOnSuccessIsCalledThenObserverOnNextAndOnCompletedAreCalled()
        throws Exception {
    final CompletableEmitter observer = Mockito
            .mock(CompletableEmitter.class);
    final DisconnectActionListener listener = new DisconnectFactory.DisconnectActionListener(
            observer);
    final IMqttToken asyncActionToken = Mockito.mock(IMqttToken.class);
    listener.onSuccess(asyncActionToken);
    Mockito.verify(observer).onComplete();
}
 
Example #11
Source File: UnsubscribeFactoryTest.java    From rxmqtt with Apache License 2.0 5 votes vote down vote up
@Test
public void whenOnSuccessIsCalledThenObserverOnNextAndOnCompletedAreCalled()
        throws Exception {
    final CompletableEmitter observer = Mockito
            .mock(CompletableEmitter.class);
    final UnsubscribeActionListener listener = new UnsubscribeFactory.UnsubscribeActionListener(
            observer);
    final IMqttToken asyncActionToken = Mockito.mock(IMqttToken.class);
    listener.onSuccess(asyncActionToken);
    Mockito.verify(observer).onComplete();
}
 
Example #12
Source File: ExampleUnitTest.java    From RxAndroid-Sample with Apache License 2.0 5 votes vote down vote up
@Test
public void testCompletableObservable() {

    Completable.create(new CompletableOnSubscribe() {
        @Override
        public void subscribe(CompletableEmitter emitter) {
            emitter.onComplete();
        }
    })
            .subscribeOn(Schedulers.io())
            .subscribe(new CompletableObserver() {
                @Override
                public void onSubscribe(Disposable d) {

                }

                @Override
                public void onComplete() {
                    System.out.println("onComplete is called");
                }

                @Override
                public void onError(Throwable e) {
                    System.out.println("onError is called" + e.getMessage());
                }
            });
}
 
Example #13
Source File: DisconnectFactory.java    From rxmqtt with Apache License 2.0 4 votes vote down vote up
public DisconnectActionListener(final CompletableEmitter emitter) {
    super(emitter);
}
 
Example #14
Source File: CompletableEmitterMqttActionListener.java    From rxmqtt with Apache License 2.0 4 votes vote down vote up
public CompletableEmitterMqttActionListener(
        final CompletableEmitter emitter) {
    this.emitter = Objects.requireNonNull(emitter);
}
 
Example #15
Source File: Rx2Apollo.java    From apollo-android with MIT License 4 votes vote down vote up
private static void cancelOnCompletableDisposed(CompletableEmitter emitter, final Cancelable cancelable) {
  emitter.setDisposable(getRx2Disposable(cancelable));
}
 
Example #16
Source File: UnsubscribeFactory.java    From rxmqtt with Apache License 2.0 4 votes vote down vote up
public UnsubscribeActionListener(final CompletableEmitter emitter) {
    super(emitter);
}
 
Example #17
Source File: ConnectFactory.java    From rxmqtt with Apache License 2.0 4 votes vote down vote up
public ConnectActionListener(final CompletableEmitter emitter) {
    super(emitter);
}
 
Example #18
Source File: AppInitHelperBase.java    From edslite with GNU General Public License v2.0 4 votes vote down vote up
AppInitHelperBase(RxActivity activity, CompletableEmitter emitter)
{
    _activity = activity;
    _settings = UserSettings.getSettings(activity);
    _initFinished = emitter;
}
 
Example #19
Source File: AppInitHelper.java    From edslite with GNU General Public License v2.0 4 votes vote down vote up
AppInitHelper(RxActivity activity, CompletableEmitter emitter)
{
    super(activity, emitter);
}