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

The following examples show how to use io.reactivex.FlowableEmitter#onNext() . 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: SimpleRemoteSourceMapper.java    From mvvm-template with GNU General Public License v3.0 6 votes vote down vote up
public SimpleRemoteSourceMapper(FlowableEmitter<Resource<T>> emitter) {
    emitter.onNext(Resource.loading(null));
    // since realm instance was created on Main Thread, so if we need to touch on realm database after calling
    // api (such as save response data to local database, we must make request on main thread
    // by setting shouldUpdateUi params = true
    Disposable disposable = RestHelper.makeRequest(getRemote(), true, response -> {
        Log.d(TAG, "SimpleRemoteSourceMapper: call API success!");
        saveCallResult(response);
        emitter.onNext(Resource.success(response));
    }, errorEntity -> {
        Log.d(TAG, "SimpleRemoteSourceMapper: call API error: " + errorEntity.getMessage());
        emitter.onNext(Resource.error(errorEntity.getMessage(), null));
    });

    // set emitter disposable to ensure that when it is going to be disposed, our api request should be disposed as well
    emitter.setDisposable(disposable);
}
 
Example 2
Source File: ListDataNetworkBounceResource.java    From mvvm-template with GNU General Public License v3.0 6 votes vote down vote up
public ListDataNetworkBounceResource(FlowableEmitter<Resource<LocalType>> emitter) {
    emitter.onNext(Resource.success(getLocal()));

    emitter.onNext(Resource.loading(null));

    // since realm instance was created on Main Thread, so if we need to touch on realm database after calling
    // api (such as save response data to local database, we must make request on main thread
    // by setting shouldUpdateUi params = true
    Disposable disposable = RestHelper.makeRequest(getRemote(), true, response -> {
        Single.just(response)
                .map(mapper())
                .subscribe(localData -> {
                    Log.d(TAG, "SimpleRemoteSourceMapper: call API success!");
                    saveCallResult(localData);
                    emitter.onNext(Resource.success(localData));
                });
    }, errorEntity -> {
        Log.d(TAG, "SimpleRemoteSourceMapper: call API error: " + errorEntity.getMessage());
        emitter.onNext(Resource.error(errorEntity.getMessage(), null));
    });

    // set emitter disposable to ensure that when it is going to be disposed, our api request should be disposed as well
    emitter.setDisposable(disposable);
}
 
Example 3
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 4
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 5
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 6
Source File: TextSearchIndex.java    From jadx with Apache License 2.0 6 votes vote down vote up
private int searchNext(FlowableEmitter<CodeNode> emitter, String text, JavaNode javaClass, String code,
		int startPos, boolean ignoreCase) {
	int pos;
	if (ignoreCase) {
		pos = StringUtils.indexOfIgnoreCase(code, text, startPos);
	} else {
		pos = code.indexOf(text, startPos);
	}
	if (pos == -1) {
		return -1;
	}
	int lineStart = 1 + code.lastIndexOf(CodeWriter.NL, pos);
	int lineEnd = code.indexOf(CodeWriter.NL, pos + text.length());
	StringRef line = StringRef.subString(code, lineStart, lineEnd == -1 ? code.length() : lineEnd);
	emitter.onNext(new CodeNode(nodeCache.makeFrom(javaClass), -pos, line.trim()));
	return lineEnd;
}
 
Example 7
Source File: RxJavaUtils.java    From Collection-Android with MIT License 5 votes vote down vote up
@NonNull
private static <T, R> RxTaskOnSubscribe<RxAsyncTask<T, R>> getRxAsyncTaskOnSubscribe(@NonNull final RxAsyncTask<T, R> rxTask) {
    return new RxTaskOnSubscribe<RxAsyncTask<T, R>>(rxTask) {
        @Override
        public void subscribe(FlowableEmitter<RxAsyncTask<T, R>> emitter) throws Exception {
            RxAsyncTask<T, R> task = getTask();
            task.setOutData(task.doInIOThread(task.getInData()));  //在io线程工作
            emitter.onNext(task);
            emitter.onComplete();
        }
    };
}
 
Example 8
Source File: TestConnector.java    From microprofile-reactive-messaging with Apache License 2.0 5 votes vote down vote up
public void send(String channel, Message<String> message) {
    FlowableEmitter<Message<String>> emitter = incomingEmitters.get(channel);
    
    if (emitter == null) {
        throw new RuntimeException("No such incoming channel registered: " + channel);
    }
    
    emitter.onNext(message);
}
 
Example 9
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 10
Source File: FlowableOnSubscribeExecuteAsBlocking.java    From storio with Apache License 2.0 5 votes vote down vote up
@Override
public void subscribe(@NonNull FlowableEmitter<Result> emitter) throws Exception {
    try {
        emitter.onNext(preparedOperation.executeAsBlocking());
        emitter.onComplete();
    } catch (Exception e) {
        emitter.onError(e);
    }
}
 
Example 11
Source File: FlowableOnSubscribeExecuteAsBlockingOptional.java    From storio with Apache License 2.0 5 votes vote down vote up
@Override
public void subscribe(@NonNull FlowableEmitter<Optional<Result>> emitter) throws Exception {
    try {
        final Result value = preparedOperation.executeAsBlocking();
        emitter.onNext(Optional.of(value));
        emitter.onComplete();
    } catch (Exception e) {
        emitter.onError(e);
    }
}
 
Example 12
Source File: GoogleApiClientFlowable.java    From RxGps with Apache License 2.0 4 votes vote down vote up
@Override
protected void onGoogleApiClientReady(GoogleApiClient apiClient, FlowableEmitter<GoogleApiClient> emitter) {
    emitter.onNext(apiClient);
}
 
Example 13
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);
}