use of io.reactivex.rxjava3.core.ObservableSource in project RxJava by ReactiveX.
the class ObservableSwitchTest method switchWhenInnerCompleteBeforeOuter.
@Test
public void switchWhenInnerCompleteBeforeOuter() {
Observable<Observable<String>> source = Observable.unsafeCreate(new ObservableSource<Observable<String>>() {
@Override
public void subscribe(Observer<? super Observable<String>> outerObserver) {
outerObserver.onSubscribe(Disposable.empty());
publishNext(outerObserver, 10, Observable.unsafeCreate(new ObservableSource<String>() {
@Override
public void subscribe(Observer<? super String> innerObserver) {
innerObserver.onSubscribe(Disposable.empty());
publishNext(innerObserver, 0, "one");
publishNext(innerObserver, 10, "two");
publishCompleted(innerObserver, 20);
}
}));
publishNext(outerObserver, 100, Observable.unsafeCreate(new ObservableSource<String>() {
@Override
public void subscribe(Observer<? super String> innerObserver) {
innerObserver.onSubscribe(Disposable.empty());
publishNext(innerObserver, 0, "three");
publishNext(innerObserver, 10, "four");
publishCompleted(innerObserver, 20);
}
}));
publishCompleted(outerObserver, 200);
}
});
Observable<String> sampled = Observable.switchOnNext(source);
sampled.subscribe(observer);
InOrder inOrder = inOrder(observer);
scheduler.advanceTimeTo(150, TimeUnit.MILLISECONDS);
inOrder.verify(observer, never()).onComplete();
inOrder.verify(observer, times(1)).onNext("one");
inOrder.verify(observer, times(1)).onNext("two");
inOrder.verify(observer, times(1)).onNext("three");
inOrder.verify(observer, times(1)).onNext("four");
scheduler.advanceTimeTo(250, TimeUnit.MILLISECONDS);
inOrder.verify(observer, never()).onNext(anyString());
inOrder.verify(observer, times(1)).onComplete();
}
use of io.reactivex.rxjava3.core.ObservableSource in project RxJava by ReactiveX.
the class ObservableWindowWithStartEndObservableTest method endError.
@Test
@SuppressUndeliverable
public void endError() {
PublishSubject<Integer> source = PublishSubject.create();
PublishSubject<Integer> start = PublishSubject.create();
final PublishSubject<Integer> end = PublishSubject.create();
TestObserver<Integer> to = source.window(start, new Function<Integer, ObservableSource<Integer>>() {
@Override
public ObservableSource<Integer> apply(Integer v) throws Exception {
return end;
}
}).flatMap(Functions.<Observable<Integer>>identity()).test();
start.onNext(1);
end.onError(new TestException());
to.assertFailure(TestException.class);
assertFalse("Source has observers!", source.hasObservers());
assertFalse("Start has observers!", start.hasObservers());
assertFalse("End has observers!", end.hasObservers());
}
use of io.reactivex.rxjava3.core.ObservableSource in project xxf_android by NBXXF.
the class StringFileService method writeFileString.
/**
* 写
*
* @param file
* @param content
* @param append
* @return
*/
default Observable<File> writeFileString(File file, String content, boolean append) {
return Observable.defer(new Supplier<ObservableSource<? extends File>>() {
@Override
public ObservableSource<? extends File> get() throws Throwable {
FileUtils.createOrExistsFile(file);
try (FileWriter fw = new FileWriter(file, append)) {
fw.write(content);
fw.flush();
}
return Observable.just(file);
}
}).subscribeOn(Schedulers.io());
}
use of io.reactivex.rxjava3.core.ObservableSource in project xxf_android by NBXXF.
the class StringFileService method readFileString.
/**
* 读取
*
* @return
*/
default Observable<String> readFileString(File file) {
return Observable.defer(new Supplier<ObservableSource<? extends String>>() {
@Override
public ObservableSource<? extends String> get() throws Throwable {
if (FileUtils.isFileExists(ApplicationProvider.applicationContext, file)) {
try (FileReader fr = new FileReader(file)) {
char[] bt = new char[1024];
StringBuffer sb = new StringBuffer();
while (fr.read(bt) != -1) {
sb.append(bt);
java.util.Arrays.fill(bt, (char) 0);
}
return Observable.just(sb.toString());
}
}
return Observable.empty();
}
}).subscribeOn(Schedulers.io());
}
use of io.reactivex.rxjava3.core.ObservableSource in project xxf_android by NBXXF.
the class SystemUtils method selectFileUri.
/**
* 获取文件的uri 但是不是真实的文件路径
* 建议 UriUtils.getPath(activity, uri); 但是对于android 10 大文件拷贝较慢 2G==3s拷贝时间
*
* @param activity
* @param mimeTypes
* @return
*/
public static Observable<Uri> selectFileUri(final FragmentActivity activity, String[] mimeTypes) {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("*/*");
intent.putExtra(Intent.EXTRA_MIME_TYPES, mimeTypes);
return XXF.requestPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE).compose(new RxPermissionTransformer(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE)).flatMap(new Function<Boolean, ObservableSource<Uri>>() {
@Override
public ObservableSource<Uri> apply(Boolean aBoolean) throws Throwable {
return XXF.startActivityForResult(activity, intent, REQUEST_CODE_DOCUMENT).flatMap(new Function<ActivityResult, ObservableSource<Uri>>() {
@Override
public ObservableSource<Uri> apply(ActivityResult activityResult) throws Throwable {
if (!activityResult.isOk()) {
return Observable.empty();
}
return Observable.just(activityResult.getData().getData());
}
}).observeOn(AndroidSchedulers.mainThread());
}
});
}
Aggregations