Java Code Examples for io.reactivex.FlowableEmitter#isCancelled()

The following examples show how to use io.reactivex.FlowableEmitter#isCancelled() . 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: RxEasyHttpManager.java    From EasyHttp with Apache License 2.0 6 votes vote down vote up
@Override
public void subscribe(FlowableEmitter<T> e) throws Exception {
	try {
		Response response = call.execute();

		if (!e.isCancelled()) {
			if (response.isSuccessful()) {
				e.onNext(rxEasyConverter.convert(response.body().string()));
			} else {
				e.onError(new Throwable("response is unsuccessful"));
			}
		}
	} catch (Throwable t) {
		if (!e.isCancelled()) {
			e.onError(t);
		}
	} finally {
		e.onComplete();
	}
}
 
Example 2
Source File: ViewClickOnSubscribe.java    From RxComboDetector with MIT License 6 votes vote down vote up
@Override
public void subscribe(final FlowableEmitter<Integer> emitter) throws Exception {
    checkUiThread();

    View.OnClickListener listener = v -> {
        if (!emitter.isCancelled()) {
            emitter.onNext(1);
        }
    };
    view.setOnClickListener(listener);

    emitter.setDisposable(new MainThreadDisposable() {
        @Override
        protected void onDispose() {
            view.setOnClickListener(null);
        }
    });
}
 
Example 3
Source File: ObserveQuery.java    From vault with Apache License 2.0 6 votes vote down vote up
@Override public void subscribe(FlowableEmitter<T> flowableEmitter) throws Exception {
  try {
    FetchQuery<T> fetchQuery = query.vault().fetch(query.type());
    fetchQuery.setParams(query.params());
    List<T> items = fetchQuery.all(locale);
    for (T item : items) {
      if (flowableEmitter.isCancelled()) {
        return;
      }
      flowableEmitter.onNext(item);
    }
  } catch (Throwable t) {
    if (!flowableEmitter.isCancelled()) {
      flowableEmitter.onError(t);
    }
    return;
  }
  if (!flowableEmitter.isCancelled()) {
    flowableEmitter.onComplete();
  }
}
 
Example 4
Source File: ViewClickOnSubscribe.java    From dapp-wallet-demo with Apache License 2.0 5 votes vote down vote up
@Override
public void subscribe(FlowableEmitter<View> emitter) throws Exception {
    verifyMainThread();
    View.OnClickListener listener = v -> {
        if (!emitter.isCancelled()){
            emitter.onNext(v);
        }
    };
    view.setOnClickListener(listener);
    emitter.setCancellable(() -> view.setOnClickListener(null));
}
 
Example 5
Source File: TransformerDecode.java    From rxjava2-extras with Apache License 2.0 4 votes vote down vote up
public static Result process(byte[] next, ByteBuffer last, boolean endOfInput, CharsetDecoder decoder,
        FlowableEmitter<String> emitter) {
    if (emitter.isCancelled())
        return new Result(null, false);

    ByteBuffer bb;
    if (last != null) {
        if (next != null) {
            // merge leftover in front of the next bytes
            bb = ByteBuffer.allocate(last.remaining() + next.length);
            bb.put(last);
            bb.put(next);
            bb.flip();
        } else { // next == null
            bb = last;
        }
    } else { // last == null
        if (next != null) {
            bb = ByteBuffer.wrap(next);
        } else { // next == null
            return new Result(null, true);
        }
    }

    CharBuffer cb = CharBuffer.allocate((int) (bb.limit() * decoder.averageCharsPerByte()));
    CoderResult cr = decoder.decode(bb, cb, endOfInput);
    cb.flip();

    if (cr.isError()) {
        try {
            cr.throwException();
        } catch (CharacterCodingException e) {
            emitter.onError(e);
            return new Result(null, false);
        }
    }

    ByteBuffer leftOver;
    if (bb.remaining() > 0) {
        leftOver = bb;
    } else {
        leftOver = null;
    }

    String string = cb.toString();
    if (!string.isEmpty())
        emitter.onNext(string);

    return new Result(leftOver, true);
}
 
Example 6
Source File: GoogleSuggestionSource.java    From rxSuggestions with Apache License 2.0 2 votes vote down vote up
/**
 * Method to check if XML iteration can be still performed.
 * <p>
 * Checks if {@link Flowable} is still valid based on:
 * <ul>
 * <li>Downstream is actively requesting further elements.</li>
 * <li>XML document end has not reached.</li>
 * </ul>
 *
 * @param emitter   The emitter to currently handling events.
 * @param eventType XML document event type.
 * @return {@code true} if Flowable emission is valid.
 */
private boolean isFlowableEmissionValid(FlowableEmitter<SimpleSuggestionItem> emitter, int eventType) {
    return eventType != END_DOCUMENT && !emitter.isCancelled() && emitter.requested() > 0;
}