本文整理汇总了Java中rx.subjects.BehaviorSubject类的典型用法代码示例。如果您正苦于以下问题:Java BehaviorSubject类的具体用法?Java BehaviorSubject怎么用?Java BehaviorSubject使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
BehaviorSubject类属于rx.subjects包,在下文中一共展示了BehaviorSubject类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getAutoPlaylistSongs
import rx.subjects.BehaviorSubject; //导入依赖的package包/类
private Observable<List<Song>> getAutoPlaylistSongs(AutoPlaylist playlist) {
BehaviorSubject<List<Song>> subject;
if (mPlaylistContents.containsKey(playlist)) {
subject = mPlaylistContents.get(playlist);
} else {
subject = BehaviorSubject.create();
mPlaylistContents.put(playlist, subject);
playlist.generatePlaylist(mMusicStore, this, mPlayCountStore)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(subject::onNext, subject::onError);
subject.observeOn(Schedulers.io())
.subscribe(contents -> {
MediaStoreUtil.editPlaylist(mContext, playlist, contents);
}, throwable -> {
Timber.e(throwable, "Failed to save playlist contents");
});
}
return subject.asObservable();
}
示例2: behaviourSubject
import rx.subjects.BehaviorSubject; //导入依赖的package包/类
public static void behaviourSubject() {
BehaviorSubject<Integer> subject = BehaviorSubject.create();
subject.onNext(5);
Action1<Integer> action1 = integer -> Log.i("From action1", String.valueOf(integer));
Subscription subscription1 = subject.subscribe(action1);
subject.onNext(10);
Action1<Integer> action2 = integer -> Log.i("From action2", String.valueOf(integer));
Subscription subscription2 = subject.subscribe(action2);
subject.onNext(20);
subscription1.unsubscribe();
subject.onNext(40);
subscription2.unsubscribe();
subject.onNext(80);
}
示例3: shouldEmitErrorAfterViewIsAttached
import rx.subjects.BehaviorSubject; //导入依赖的package包/类
@Test
public void shouldEmitErrorAfterViewIsAttached() {
TestScheduler testScheduler = Schedulers.test();
BehaviorSubject<Boolean> view = BehaviorSubject.create();
view.onNext(true);
WaitViewReplayTransformer<Object> transformer = new WaitViewReplayTransformer<>(
view.delay(EMIT_DELAY_IN_SECONDS, TimeUnit.SECONDS, testScheduler));
TestSubscriber<Object> testSubscriber = new TestSubscriber<>();
Observable.error(new RuntimeException())
.compose(transformer)
.subscribe(testSubscriber);
testScheduler.advanceTimeBy(EMIT_DELAY_IN_SECONDS, TimeUnit.SECONDS);
testSubscriber.awaitTerminalEvent();
testSubscriber.assertError(RuntimeException.class);
}
示例4: shouldEmitValueAfterViewIsAttached
import rx.subjects.BehaviorSubject; //导入依赖的package包/类
@Test
public void shouldEmitValueAfterViewIsAttached() {
TestScheduler testScheduler = Schedulers.test();
BehaviorSubject<Boolean> view = BehaviorSubject.create();
view.onNext(true);
WaitViewLatestTransformer<Integer> transformer =
new WaitViewLatestTransformer<>(view.delay(EMIT_DELAY_IN_SECONDS, TimeUnit.SECONDS, testScheduler));
TestSubscriber<Integer> testSubscriber = new TestSubscriber<>();
Observable.just(0)
.compose(transformer)
.subscribe(testSubscriber);
testScheduler.advanceTimeBy(EMIT_DELAY_IN_SECONDS, TimeUnit.SECONDS);
testSubscriber.awaitTerminalEvent();
testSubscriber.assertValue(0);
testSubscriber.assertCompleted();
}
示例5: testExceptionSubjectAction
import rx.subjects.BehaviorSubject; //导入依赖的package包/类
public void testExceptionSubjectAction() {
// test the behavior of surfacing exceptions from a subject
BehaviorSubject<Integer> subject = BehaviorSubject.create();
final List<Notification<Integer>> notifications = new ArrayList<Notification<Integer>>(4);
Subscription s = subject.subscribe(new Action1<Integer>() {
@Override
public void call(Integer t) {
notifications.add(Notification.createOnNext(t));
throw new RuntimeException("call " + t);
}
});
try {
subject.onNext(0);
// (unreachable) expect an exception to be thrown
fail();
} catch (RuntimeException e) {
assertEquals("call 0", e.getMessage());
}
}
示例6: getUpdates
import rx.subjects.BehaviorSubject; //导入依赖的package包/类
private BehaviorSubject<ParseChange<P>> getUpdates(I unit) {
final FileObject resource = unit.source();
final FileName name = resource.getName();
// THREADING: it is possible that two different threads asking for a subject may do the parsing twice here, as
// this is not an atomic operation. However, the chance is very low and it does not break anything (only
// duplicates some work), so it is acceptable.
BehaviorSubject<ParseChange<P>> updates = updatesPerResource.get(name);
if(updates == null) {
updates = BehaviorSubject.create();
updatesPerResource.put(name, updates);
try {
logger.trace("Parsing for {}", resource);
final P result = syntaxService.parse(unit);
updates.onNext(ParseChange.update(result));
} catch(ParseException e) {
final String message = String.format("Parsing for %s failed", name);
logger.error(message, e);
updates.onNext(ParseChange.<P>error(e));
}
}
return updates;
}
示例7: testPagingCapabilities
import rx.subjects.BehaviorSubject; //导入依赖的package包/类
@Test
public void testPagingCapabilities() {
PublishSubject<Object> view = PublishSubject.create();
BehaviorSubject<Integer> nextPageRequests = BehaviorSubject.create();
final TestObserver<Delivery<Object, String>> testObserver = new TestObserver<>();
nextPageRequests
.concatMap(new Func1<Integer, Observable<Integer>>() {
@Override
public Observable<Integer> call(Integer targetPage) {
return targetPage <= requestedPageCount ?
Observable.<Integer>never() :
Observable.range(requestedPageCount, targetPage - requestedPageCount);
}
})
.doOnNext(new Action1<Integer>() {
@Override
public void call(Integer it) {
requestedPageCount = it + 1;
}
})
.startWith(Observable.range(0, requestedPageCount))
.concatMap(new Func1<Integer, Observable<String>>() {
@Override
public Observable<String> call(final Integer page) {
return requestPage(page, PAGE_SIZE);
}
})
.compose(new DeliverReplay<Object, String>(view))
.subscribe(testObserver);
ArrayList<Delivery<Object, String>> onNext = new ArrayList<>();
testObserver.assertReceivedOnNext(onNext);
view.onNext(999);
addOnNext(onNext, 999, 0, 1, 2);
testObserver.assertReceivedOnNext(onNext);
nextPageRequests.onNext(2);
addOnNext(onNext, 999, 3, 4, 5);
testObserver.assertReceivedOnNext(onNext);
view.onNext(null);
assertEquals(0, testObserver.getOnCompletedEvents().size());
testObserver.assertReceivedOnNext(onNext);
nextPageRequests.onNext(3);
assertEquals(0, testObserver.getOnCompletedEvents().size());
testObserver.assertReceivedOnNext(onNext);
view.onNext(9999);
addOnNext(onNext, 9999, 0, 1, 2, 3, 4, 5, 6, 7, 8);
assertEquals(0, testObserver.getOnCompletedEvents().size());
testObserver.assertReceivedOnNext(onNext);
}
示例8: onStartLoading
import rx.subjects.BehaviorSubject; //导入依赖的package包/类
@Override
protected void onStartLoading() {
Log.d(getClass().getSimpleName(), "onStartLoading");
super.onStartLoading();
if (subject == null) {
subject = BehaviorSubject.create();
}
subscription = subject.observeOn(AndroidSchedulers.mainThread())
.subscribe(this::onResult, this::onError);
if (subjectSubscription == null) {
subjectSubscription = searchQuerySubject.flatMap(this::create)
.subscribeOn(Schedulers.io()).subscribe(subject);
}
}
示例9: AnalyseAppLibraryPermission
import rx.subjects.BehaviorSubject; //导入依赖的package包/类
@Inject @SuppressWarnings("checkstyle:parameternumber")
public AnalyseAppLibraryPermission(DecompileApp decompileApp,
AnalyseLibraryMethodInvocations analyseLibraryMethodInvocations,
AnalysePermissionsFromMethodInvocations analysePermissionsFromMethodInvocations,
LibraryService libraryService,
PermissionService permissionService,
StorageProvider storageProvider,
FileUtils fileUtils,
AppService appService) {
this.decompileApp = decompileApp;
this.analyseLibraryMethodInvocations = analyseLibraryMethodInvocations;
this.analysePermissionsFromMethodInvocations = analysePermissionsFromMethodInvocations;
this.libraryService = libraryService;
this.permissionService = permissionService;
this.storageProvider = storageProvider;
this.fileUtils = fileUtils;
this.appService = appService;
this.progressSubject = BehaviorSubject.create();
}
示例10: initObservable
import rx.subjects.BehaviorSubject; //导入依赖的package包/类
@Override
protected final void initObservable() {
subject = BehaviorSubject.create();
subject.onBackpressureBuffer()
.observeOn(EventThread.getScheduler(observeThread))
.subscribeOn(EventThread.getScheduler(subscribeThread))
.subscribe(event -> {
try {
if (valid) {
handleEvent(event);
}
} catch (InvocationTargetException e) {
throwRuntimeException("Could not dispatch event: " + event.getClass() + " to subscriber " + SubscriberBehaviorEvent.this, e);
}
});
}
示例11: findAll
import rx.subjects.BehaviorSubject; //导入依赖的package包/类
protected Observable<ParseObject<T>> findAll(ParseQuery.Builder<DefaultParseColumn> queryBuilder) {
ParseRestApi api = ParseRestClientFactory.masterClient();
BehaviorSubject<Date> relay = BehaviorSubject.create();
relay.onNext(new Date());
return relay.flatMap(date -> {
ParseQuery<DefaultParseColumn> queryOlders = queryBuilder.olderThan(DefaultParseColumn.createdAt, date).build();
Observable<Response<QueryResults>> response = api.query(className, queryOlders.params);
return response;
}).flatMap((r) -> flatMapResponse(r, relay));
}
示例12: flatMapResponse
import rx.subjects.BehaviorSubject; //导入依赖的package包/类
protected Observable<ParseObject<T>> flatMapResponse(Response<QueryResults> r, @Nullable BehaviorSubject<Date> relay) {
if (!r.isSuccessful()) {
if (relay!= null) { relay.onCompleted();}
throw errorFrom(r);
}
List<ParseObject<T>> objects = new ArrayList<>();
List<ParseMap> found = r.body().results;
if (found == null) {
found = Collections.emptyList();
}
for (ParseMap parseMap : found) {
ParseObject<T> o = new ParseObject<T>(parseMap);
objects.add(o);
}
if (relay != null) {
if (found.size() == QUERY_ALL_LIMIT) {
ParseObject<DefaultParseColumn> object = new ParseObject<>(found.get(QUERY_ALL_LIMIT - 1));
Date createdAt = object.createdAt();
relay.onNext(createdAt);
} else {
relay.onCompleted();
}
}
return Observable.from(objects);
}
示例13: behaviourSubject
import rx.subjects.BehaviorSubject; //导入依赖的package包/类
void behaviourSubject(){
Action1<Integer> onNextFuc= new Action1<Integer>() {
@Override
public void call(Integer integer) {
Log.i(TAG, "New Event received on behaviourSubject: " + integer);
}
};
BehaviorSubject<Integer> behaviorSubject = BehaviorSubject.create();
behaviorSubject.onNext(1);
behaviorSubject.onNext(2);
Subscription subscription = behaviorSubject.doOnSubscribe(new Action0() {
@Override
public void call() {
Log.i(TAG, "Observer subscribed to behaviorSubject");
}
}).doOnUnsubscribe(new Action0() {
@Override
public void call() {
Log.i(TAG, "Observer subscribed to behaviorSubject");
}
}).subscribe(onNextFuc);
behaviorSubject.onNext(3);
behaviorSubject.onNext(4);
subscription.unsubscribe();
behaviorSubject.onNext(5);
behaviorSubject.onCompleted();
}
示例14: behaviorRefCount
import rx.subjects.BehaviorSubject; //导入依赖的package包/类
@Nonnull
public static <T> Observable.Transformer<? super T, ? extends T> behaviorRefCount() {
return new Observable.Transformer<T, T>() {
@Override
public Observable<T> call(Observable<T> tObservable) {
return new OperatorMulticast<>(tObservable, new Func0<Subject<? super T, ? extends T>>() {
@Override
public Subject<? super T, ? extends T> call() {
return BehaviorSubject.<T>create();
}
}).refCount();
}
};
}
示例15: updateMarketTypes
import rx.subjects.BehaviorSubject; //导入依赖的package包/类
private void updateMarketTypes() {
BehaviorSubject<Map.Entry<Integer, Integer>> subject = BehaviorSubject.create();
CompositeSubscription subscriptions = new CompositeSubscription();
subscriptions.add(subject
.observeOn(AndroidSchedulers.mainThread())
.doOnNext(progress -> {
onProgressUpdate(progress.getKey(), progress.getValue(), "Storing items list...");
if (progress.getKey().equals(progress.getValue())) {
subscriptions.unsubscribe();
decrementUpdatingCount();
updateFinished();
}
}).subscribe());
Observable.defer(() -> Observable.just(getAllMarketTypes()))
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.doOnNext(orders -> MarketTypeEntry.addNewMarketTypes(orders, subject))
.subscribe();
}