本文整理汇总了Java中io.reactivex.subjects.PublishSubject类的典型用法代码示例。如果您正苦于以下问题:Java PublishSubject类的具体用法?Java PublishSubject怎么用?Java PublishSubject使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
PublishSubject类属于io.reactivex.subjects包,在下文中一共展示了PublishSubject类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: downloadProducts
import io.reactivex.subjects.PublishSubject; //导入依赖的package包/类
@Override
public void downloadProducts(DownloadProductsRequest request, StreamObserver<Product> responseObserver) {
PublishSubject<Product> productPublishSubject = PublishSubject.create();
productPublishSubject
.doOnNext(product -> {
responseObserver.onNext(product);
counter.labels("downloadProducts", "success");
})
.doOnComplete(() -> responseObserver.onCompleted())
.doOnError(t -> {
responseObserver.onError(t);
counter.labels("downloadProducts", "failed");
})
.subscribe();
productDao.downloadProducts(request, productPublishSubject);
}
示例2: showNameInputDialog
import io.reactivex.subjects.PublishSubject; //导入依赖的package包/类
private Observable<String> showNameInputDialog(String prefix, MaterialDialog.InputCallback textWatcher) {
final PublishSubject<String> input = PublishSubject.create();
DialogUtils.showDialog(new ThemeColorMaterialDialogBuilder(mContext).title(R.string.text_name)
.inputType(InputType.TYPE_CLASS_TEXT)
.alwaysCallInputCallback()
.input(getString(R.string.text_please_input_name), prefix, false, textWatcher)
.onPositive(new MaterialDialog.SingleButtonCallback() {
@Override
public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
input.onNext(dialog.getInputEditText().getText().toString());
input.onComplete();
}
})
.build());
return input;
}
示例3: doSomeWork
import io.reactivex.subjects.PublishSubject; //导入依赖的package包/类
/**
* PublishSubject只会把在订阅发生的时间点之后来自原始Observable的数据发射给观察者。
* 需要注意的是,PublishSubject可能会一创建完成就立刻开始发射数据(除非你可以阻止它发生),
* 因此这里有一个风险:在Subject被创建后到有观察者订阅它之前这个时间段内,一个或多个数据可能会丢失。
* 如果要确保来自原始Observable的所有数据都被分发:
* 1.使用Create创建那个Observable以便手动给它引入"冷"Observable的行为(当所有观察者都已经订阅时才开始发射数据)
* 2.改用ReplaySubject。
*
* 如果原始的Observable因为发生了一个错误而终止,PublishSubject将不会发射任何数据,只是简单的向前传递这个错误通知。
*/
private void doSomeWork() {
PublishSubject<Integer> source = PublishSubject.create();
source.subscribe(getFirstObserver()); // it will get 1, 2, 3, 4 and onComplete
source.onNext(1);
source.onNext(2);
source.onNext(3);
/*
* it will emit 4 and onComplete for second observer also.
*/
source.subscribe(getSecondObserver());
source.onNext(4);
source.onComplete();
}
示例4: onRequestPermissionsResult
import io.reactivex.subjects.PublishSubject; //导入依赖的package包/类
void onRequestPermissionsResult(String permissions[], int[] grantResults, boolean[] shouldShowRequestPermissionRationale) {
for (int i = 0, size = permissions.length; i < size; i++) {
Log.i("","onRequestPermissionsResult " + permissions[i]);
// Find the corresponding subject
PublishSubject<Permission> subject = mSubjects.get(permissions[i]);
if (subject == null) {
// No subject found
Log.e(RxPermissions.TAG, "RxPermissions.onRequestPermissionsResult invoked but didn't find the corresponding permission request.");
return;
}
mSubjects.remove(permissions[i]);
boolean granted = grantResults[i] == PackageManager.PERMISSION_GRANTED;
subject.onNext(new Permission(permissions[i], granted, shouldShowRequestPermissionRationale[i]));
subject.onComplete();
}
}
示例5: monitor
import io.reactivex.subjects.PublishSubject; //导入依赖的package包/类
private void monitor(InputStream input, PublishSubject<String> subject) {
BufferedReader reader = new BufferedReader(new InputStreamReader(input, encoding));
final int BUFFERSIZE = 4096;
char[] buffer = new char[BUFFERSIZE];
while (true) {
try {
if (Thread.interrupted()) {
subject.onComplete();
return;
}
int read = reader.read(buffer, 0, BUFFERSIZE);
if (read == -1) {
subject.onComplete();
return;
}
subject.onNext(new String(buffer, 0, read));
} catch (IOException e) {
subject.onError(e);
return;
}
}
}
示例6: initView
import io.reactivex.subjects.PublishSubject; //导入依赖的package包/类
private void initView() {
subject = PublishSubject.create();
subject.debounce(0, TimeUnit.MILLISECONDS)
// .filter(new Predicate<Float>() {
// @Override
// public boolean test(Float brightness) throws Exception {
// return true;
// }
// })
.distinctUntilChanged()
.switchMap(new Function<Float, ObservableSource<ColorMatrixColorFilter>>() {
@Override
public ObservableSource<ColorMatrixColorFilter> apply(Float value) throws Exception {
return postBrightness(value);
}
})
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer<ColorMatrixColorFilter>() {
@Override
public void accept(ColorMatrixColorFilter colorMatrixColorFilter) throws Exception {
setColorFilter(colorMatrixColorFilter);
}
});
}
示例7: TranscodingServiceImpl
import io.reactivex.subjects.PublishSubject; //导入依赖的package包/类
@Inject
TranscodingServiceImpl(ExternalProcessService externalProcessService,
TranscoderSettings transcoderSettings,
MediaScanSettings mediaScanSettings,
OutputParser parser) {
this.externalProcessService = externalProcessService;
this.transcoderSettings = transcoderSettings;
this.mediaScanSettings = mediaScanSettings;
this.parser = parser;
this.publisher = PublishSubject.create().toSerialized();
publisher.ofType(TranscodeTask.class)
.skipWhile(o -> isActive())
.observeOn(Schedulers.computation())
.subscribeOn(Schedulers.io())
.subscribe(this::prepareTranscode);
}
示例8: onCreateView
import io.reactivex.subjects.PublishSubject; //导入依赖的package包/类
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_two_way_data_biding, container, false);
unbinder = ButterKnife.bind(this, view);
publishSubject = PublishSubject.create();
publishSubject.subscribe(new Consumer<String>() {
@Override
public void accept(String s) throws Exception {
mTvAddResult.setText(s);
}
});
onNumChanged();
mEtAddLeft.requestFocus();
return view;
}
示例9: onUserQueryEvent_emptyQueryString_shouldDoNothingWithView
import io.reactivex.subjects.PublishSubject; //导入依赖的package包/类
@Test
public void onUserQueryEvent_emptyQueryString_shouldDoNothingWithView() {
// given
PublishSubject<UserQueryEvent> userQuery$ = PublishSubject.create();
UserListPresenter presenter = new UserListPresenter(
userQuery$,
userService,
() -> mock(UserView.class),
() -> mock(UserPresenter.class),
DEFAULT_PAGE_SIZE,
DEFAULT_USER_SEARCH_LIMIT
);
presenter.start(view);
UserQueryEvent event = new UserQueryEvent(" "); // empty string
// when
userQuery$.onNext(event);
// then
verifyNoMoreInteractions(view, userService);
}
示例10: onRequestPermissionsResult
import io.reactivex.subjects.PublishSubject; //导入依赖的package包/类
void onRequestPermissionsResult(String permissions[], int[] grantResults, boolean[] shouldShowRequestPermissionRationale) {
for (int i = 0, size = permissions.length; i < size; i++) {
log("onRequestPermissionsResult " + permissions[i]);
// Find the corresponding subject
PublishSubject<Permission> subject = mSubjects.get(permissions[i]);
if (subject == null) {
// No subject found
Log.e(RxPermissions.TAG, "RxPermissions.onRequestPermissionsResult invoked but didn't find the corresponding permission request.");
return;
}
mSubjects.remove(permissions[i]);
boolean granted = (grantResults[i] == PackageManager.PERMISSION_GRANTED) && (PermissionChecker.checkSelfPermission(getContext(), permissions[i]) == PermissionChecker.PERMISSION_GRANTED);
subject.onNext(new Permission(permissions[i], granted, shouldShowRequestPermissionRationale[i]));
subject.onComplete();
}
}
示例11: registerNoThread
import io.reactivex.subjects.PublishSubject; //导入依赖的package包/类
/**
* 接受消息
*
* @param tag 标志
* @param callBack 回调
*/
public <T> DisposableObserver registerNoThread(@NonNull final Object tag,
@NonNull final RxBusCallBack<T> callBack) {
RxBusEvent rxBusEvent = rxBusEventArrayMap.get(tag);
if (RxUtils.isEmpty(rxBusEvent)) {
rxBusEvent = new RxBusEvent();
rxBusEvent.subject = PublishSubject.create().toSerialized();
rxBusEvent.disposable =
rxBusEvent.subject
.ofType(callBack.busOfType())
.subscribeWith(new RxBusObserver<T>() {
@Override
public void onError(@io.reactivex.annotations.NonNull Throwable e) {
super.onError(e);
callBack.onBusError(e);
}
@Override
public void onNext(@io.reactivex.annotations.NonNull T t) {
super.onNext(t);
}
});
rxBusEventArrayMap.put(tag, rxBusEvent);
}
return rxBusEvent.disposable;
}
示例12: onUserSelected_view_shouldPostUserSelectedEvent
import io.reactivex.subjects.PublishSubject; //导入依赖的package包/类
@Test
public void onUserSelected_view_shouldPostUserSelectedEvent() {
// given
TestObserver<UserSelectedEvent> userSelected$ = TestObserver.create();
User user = mock(User.class);
given(user.getLogin()).willReturn("foo");
PublishSubject<Trigger> userSelectionIntent = PublishSubject.create();
given(view.userSelection$()).willReturn(userSelectionIntent);
UserPresenter presenter = new UserPresenter(Sink.of(userSelected$));
presenter.start(user, view);
// when
fire(userSelectionIntent);
// then
userSelected$.assertValueCount(1);
UserSelectedEvent event = userSelected$.values().get(0);
assertThat(event.getUser().getLogin()).isEqualTo("foo");
}
示例13: refreshOnlineStatus
import io.reactivex.subjects.PublishSubject; //导入依赖的package包/类
public Observable<Boolean> refreshOnlineStatus() {
PublishSubject<Boolean> online = PublishSubject.create();
mRetrofit.create(UserApi.class)
.me()
.subscribeOn(Schedulers.io())
.subscribe(user -> {
setUser(user);
online.onNext(true);
online.onComplete();
}, error -> {
setUser(null);
online.onNext(false);
online.onComplete();
});
return online;
}
示例14: testGetStderrUpdatesEchoesUpdatesFromExecutorObservers
import io.reactivex.subjects.PublishSubject; //导入依赖的package包/类
@Test
public void testGetStderrUpdatesEchoesUpdatesFromExecutorObservers() throws InterruptedException, ExecutionException, TimeoutException {
final CancelablePromise<JobExecutionResult> executorPromise = new SimpleCancelablePromise<>();
final Subject<byte[]> stderrSubject = PublishSubject.create();
final JobExecutor executor =
MockJobExecutor.thatUses(executorPromise, Observable.just(TestHelpers.generateRandomBytes()), stderrSubject);
final JobManager jobManager = createManagerWith(executor);
final Pair<JobId, CancelablePromise<FinalizedJob>> ret =
jobManager.submit(STANDARD_VALID_REQUEST);
final Observable<byte[]> stderrObservable =
jobManager.stderrUpdates(ret.getLeft()).get();
final AtomicReference<byte[]> bytesFromObservable = new AtomicReference<>();
stderrObservable.subscribe(bytesFromObservable::set);
final byte[] bytesExpected = TestHelpers.generateRandomBytes();
stderrSubject.onNext(bytesExpected);
executorPromise.complete(new JobExecutionResult(FINISHED));
ret.getRight().get(DEFAULT_TIMEOUT, MILLISECONDS);
assertThat(bytesFromObservable.get()).isEqualTo(bytesExpected);
}
示例15: initView
import io.reactivex.subjects.PublishSubject; //导入依赖的package包/类
private void initView() {
subject = PublishSubject.create();
subject.debounce(0, TimeUnit.MILLISECONDS)
// .filter(new Predicate<Float>() {
// @Override
// public boolean test(Float contrast) throws Exception {
// return true;
// }
// })
.distinctUntilChanged()
.switchMap(new Function<Float, ObservableSource<ColorMatrixColorFilter>>() {
@Override
public ObservableSource<ColorMatrixColorFilter> apply(Float value) throws Exception {
return postContrast(value);
}
})
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer<ColorMatrixColorFilter>() {
@Override
public void accept(ColorMatrixColorFilter colorMatrixColorFilter) throws Exception {
setColorFilter(colorMatrixColorFilter);
}
});
}