当前位置: 首页>>代码示例>>Java>>正文


Java PublishSubject类代码示例

本文整理汇总了Java中rx.subjects.PublishSubject的典型用法代码示例。如果您正苦于以下问题:Java PublishSubject类的具体用法?Java PublishSubject怎么用?Java PublishSubject使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


PublishSubject类属于rx.subjects包,在下文中一共展示了PublishSubject类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: toSubscribe

import rx.subjects.PublishSubject; //导入依赖的package包/类
/**
 * 添加线程管理并订阅
 * @param ob
 * @param subscriber
 * @param cacheKey 缓存kay
 * @param event Activity 生命周期
 * @param lifecycleSubject
 * @param isSave 是否缓存
 * @param forceRefresh 是否强制刷新
 */
public void toSubscribe(Observable ob,
                        final ProgressSubscriber subscriber,
                        String cacheKey,
                        final ActivityLifeCycleEvent event,
                        final PublishSubject<ActivityLifeCycleEvent> lifecycleSubject,
                        boolean isSave,
                        boolean forceRefresh) {
    //数据预处理
    Observable.Transformer<HttpResult<Object>, Object> result = RxHelper.handleResult(event,lifecycleSubject);
    Observable observable = ob.compose(result)
            .doOnSubscribe(new Action0() {
                @Override
                public void call() {
                    //显示Dialog和一些其他操作
                    subscriber.showProgressDialog();
                }
            });
    RetrofitCache.load(cacheKey,observable,isSave,forceRefresh).subscribe(subscriber);
}
 
开发者ID:Xianicai,项目名称:TMvpSimple-master,代码行数:30,代码来源:HttpUtil.java

示例2: bindUntilEvent

import rx.subjects.PublishSubject; //导入依赖的package包/类
/**
 * 利用Observable.takeUntil()停止网络请求
 *
 * @param event
 * @param lifecycleSubject
 * @param <T>
 * @return
 */
@NonNull
public <T> Observable.Transformer<T, T> bindUntilEvent(@NonNull final ActivityLifeCycleEvent
                                                                   event, final
PublishSubject<ActivityLifeCycleEvent> lifecycleSubject) {
    return new Observable.Transformer<T, T>() {
        @Override
        public Observable<T> call(Observable<T> sourceObservable) {
            Observable<ActivityLifeCycleEvent> compareLifecycleObservable =
                    lifecycleSubject.takeFirst(new Func1<ActivityLifeCycleEvent, Boolean>() {
                        @Override
                        public Boolean call(ActivityLifeCycleEvent activityLifeCycleEvent) {
                            return activityLifeCycleEvent.equals(event);
                        }
                    });
            return sourceObservable.takeUntil(compareLifecycleObservable);
        }
    };
}
 
开发者ID:Xianicai,项目名称:TMvpSimple-master,代码行数:27,代码来源:RxHelper.java

示例3: testErrorsDoNotEmit

import rx.subjects.PublishSubject; //导入依赖的package包/类
@Test
public void testErrorsDoNotEmit() {
  final PublishSubject<Integer> source = PublishSubject.create();
  final Observable<Integer> result = source
    .materialize()
    .compose(Transformers.values());

  final TestSubscriber<Integer> resultTest = new TestSubscriber<>();
  result.subscribe(resultTest);

  //
  source.onNext(1);
  resultTest.assertValues(1);

  // An error in the source stream should not emit values.
  source.onError(new Throwable());

  resultTest.assertValues(1);
}
 
开发者ID:kickstarter,项目名称:android-oss,代码行数:20,代码来源:ValuesTransformerTest.java

示例4: basic

import rx.subjects.PublishSubject; //导入依赖的package包/类
@Test
public void basic() {
  TestScheduler scheduler = new TestScheduler();
  PublishSubject<Integer> subject = PublishSubject.create();
  RecordingObserver<Integer> o = new RecordingObserver<>();
  subject
      .lift(new OperatorNormalize<Integer>(1, TimeUnit.SECONDS, scheduler))
      .subscribe(o);

  subject.onNext(0);
  o.takeNext();

  subject.onNext(1);
  o.assertNoMoreEvents();

  scheduler.advanceTimeBy(1, TimeUnit.SECONDS);
  o.takeNext();

  subject.onCompleted();
  o.assertOnCompleted();
}
 
开发者ID:hzsweers,项目名称:RxNormalize,代码行数:22,代码来源:OperatorNormalizeTest.java

示例5: completion

import rx.subjects.PublishSubject; //导入依赖的package包/类
@Test
public void completion() {
  TestScheduler scheduler = new TestScheduler();
  PublishSubject<Integer> subject = PublishSubject.create();
  RecordingObserver<Integer> o = new RecordingObserver<>();
  subject
      .lift(new OperatorNormalize<Integer>(1, TimeUnit.SECONDS, scheduler))
      .subscribe(o);

  // First emits immediately
  subject.onNext(0);
  o.takeNext();

  subject.onNext(1);
  subject.onCompleted();
  o.assertNoMoreEvents();

  scheduler.advanceTimeBy(1, TimeUnit.SECONDS);
  o.takeNext();
  o.assertOnCompleted();
}
 
开发者ID:hzsweers,项目名称:RxNormalize,代码行数:22,代码来源:OperatorNormalizeTest.java

示例6: unsubscription

import rx.subjects.PublishSubject; //导入依赖的package包/类
@Test
public void unsubscription() {
  TestScheduler scheduler = new TestScheduler();
  PublishSubject<Integer> subject = PublishSubject.create();
  RecordingObserver<Integer> o = new RecordingObserver<>();
  Subscription sub = subject
      .lift(new OperatorNormalize<Integer>(1, TimeUnit.SECONDS, scheduler))
      .subscribe(o);

  // First emits immediately
  subject.onNext(0);
  o.takeNext();

  subject.onNext(1);
  o.assertNoMoreEvents();

  sub.unsubscribe();

  scheduler.advanceTimeBy(1, TimeUnit.SECONDS);
  o.assertNoMoreEvents();
}
 
开发者ID:hzsweers,项目名称:RxNormalize,代码行数:22,代码来源:OperatorNormalizeTest.java

示例7: overDelay_shouldEmitImmediately

import rx.subjects.PublishSubject; //导入依赖的package包/类
@Test
public void overDelay_shouldEmitImmediately() {
  TestScheduler scheduler = new TestScheduler();
  PublishSubject<Integer> subject = PublishSubject.create();
  RecordingObserver<Integer> o = new RecordingObserver<>();
  subject
      .lift(new OperatorNormalize<Integer>(1, TimeUnit.SECONDS, scheduler))
      .subscribe(o);

  // First emits immediately
  subject.onNext(0);
  o.takeNext();

  scheduler.advanceTimeBy(2, TimeUnit.SECONDS);
  subject.onNext(1);
  o.takeNext();
}
 
开发者ID:hzsweers,项目名称:RxNormalize,代码行数:18,代码来源:OperatorNormalizeTest.java

示例8: should_fail_on_second_subscription

import rx.subjects.PublishSubject; //导入依赖的package包/类
@Test
public void should_fail_on_second_subscription() {
    // given
    PublishSubject<Integer> subject = PublishSubject.create();
    Observable<Integer> limitedObservable = SubscriptionLimiter.limitSubscriptions(1, subject);
    TestSubscriber<Integer> subscriber = new TestSubscriber<>();
    TestSubscriber<Integer> subscriber2 = new TestSubscriber<>();
    limitedObservable.subscribe(subscriber);
    // when
    limitedObservable.subscribe(subscriber2);
    subject.onNext(123);
    // then
    assertThat(subscriber2.getOnNextEvents()).isEmpty();
    assertThat(subscriber2.getOnErrorEvents()).hasSize(1);

}
 
开发者ID:RxHandsOn,项目名称:MarketData,代码行数:17,代码来源:SubscriptionLimiterTest.java

示例9: refresh

import rx.subjects.PublishSubject; //导入依赖的package包/类
@Test
public void refresh() throws Exception {
    final Reddit reddit = new Reddit();
    PublishSubject<Reddit> subject = PublishSubject.create();
    Mockito.doReturn(subject.asObservable().toList())
            .when(mRepository)
            .getReddits(Mockito.anyString());
    mViewModel.refresh();
    Mockito.verify(mRepository).getReddits("test");
    Assert.assertThat(mViewModel.errorText.get(), IsNull.nullValue());
    Assert.assertThat(mViewModel.isLoading.get(), Is.is(true));
    subject.onNext(reddit);
    subject.onCompleted();
    Assert.assertThat(mViewModel.isLoading.get(), Is.is(false));
    Assert.assertThat(mViewModel.reddits, IsCollectionContaining.hasItems(reddit));
}
 
开发者ID:DanielSerdyukov,项目名称:droidcon2016,代码行数:17,代码来源:RedditListViewModelTest.java

示例10: searchQueryChange

import rx.subjects.PublishSubject; //导入依赖的package包/类
@Test
public void searchQueryChange() throws Exception {
    final Subreddit subreddit = new Subreddit();
    PublishSubject<Subreddit> subject = PublishSubject.create();
    Mockito.doReturn(subject.asObservable().toList())
            .when(mRepository)
            .searchSubreddits(Mockito.anyString());
    mViewModel.subscribeOnSearchQueryChange();
    mViewModel.mSearchQuery.onNext("test");
    Mockito.verify(mRepository).searchSubreddits("test");
    Assert.assertThat(mViewModel.isLoading.get(), Is.is(true));
    subject.onNext(subreddit);
    subject.onCompleted();
    Assert.assertThat(mViewModel.isLoading.get(), Is.is(false));
    Assert.assertThat(mViewModel.subreddits, IsCollectionContaining.hasItems(subreddit));
}
 
开发者ID:DanielSerdyukov,项目名称:droidcon2016,代码行数:17,代码来源:MainViewModelTest.java

示例11: observeIndication

import rx.subjects.PublishSubject; //导入依赖的package包/类
@Override public Observable<GattObserveData> observeIndication(
        final BluetoothGattCharacteristic characteristicToIndication) {
    return Observable.merge(indicationSubject = PublishSubject.create(),
            Observable.create((Observable.OnSubscribe<GattObserveData>) subscriber -> {
                if (isConnected()) {
                    if (characteristicToIndication != null) {
                        bluetoothGatt.setCharacteristicNotification(characteristicToIndication, true);
                        BluetoothGattDescriptor indicationDescriptor = characteristicToIndication.getDescriptor(BluetoothGatts.CLIENT_CHARACTERISTIC_CONFIG);
                        indicationDescriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
                        bluetoothGatt.writeDescriptor(indicationDescriptor);
                    } else {
                        subscriber.onError(UUID_NOT_FOUND);
                    }
                } else {
                    subscriber.onError(GATT_NOT_CONNECTED);
                }
            })).doOnSubscribe(() -> currentIndicationCharacteristic = characteristicToIndication);
}
 
开发者ID:kam6512,项目名称:RxBleGattManager,代码行数:19,代码来源:GattManager.java

示例12: testPagingCapabilities

import rx.subjects.PublishSubject; //导入依赖的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);
}
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:62,代码来源:DeliverReplayTest.java

示例13: testActivityObservableBindLifecycle

import rx.subjects.PublishSubject; //导入依赖的package包/类
@Test public void testActivityObservableBindLifecycle() throws Exception {
  final Observable<Object> observable = PublishSubject.create().asObservable();

  TestSubscriber<Object> testSubscriber = TestSubscriber.create();

  ActivityController controller = Robolectric.buildActivity(TestActivity.class).create().start();
  NaviActivity activity = (NaviActivity) controller.get();
  this.presenter.bind(activity);

  this.presenter.bindLifecycle(observable, testSubscriber);

  controller.create();
  assertFalse(testSubscriber.isUnsubscribed());
  controller.start();
  assertFalse(testSubscriber.isUnsubscribed());
  controller.resume();
  assertFalse(testSubscriber.isUnsubscribed());
  controller.pause();
  assertFalse(testSubscriber.isUnsubscribed());
  controller.stop();
  assertFalse(testSubscriber.isUnsubscribed());
  controller.destroy();
  testSubscriber.assertCompleted();
  testSubscriber.assertUnsubscribed();
}
 
开发者ID:ezhome,项目名称:Android-RxPresenter,代码行数:26,代码来源:RxPresenterTest.java

示例14: initObservable

import rx.subjects.PublishSubject; //导入依赖的package包/类
protected void initObservable() {
    subject = PublishSubject.create();

    subscribe = 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 " + SubscriberEvent.this, e);
                }
            });
}
 
开发者ID:analizer1,项目名称:RxBusLib,代码行数:17,代码来源:SubscriberEvent.java

示例15: handleResult

import rx.subjects.PublishSubject; //导入依赖的package包/类
/**
 * @param <T>
 * @return
 */
public static <T> Observable.Transformer<HttpResult<T>, T> handleResult(final
                                                                        ActivityLifeCycleEvent event, final PublishSubject<ActivityLifeCycleEvent> lifecycleSubject) {
    return new Observable.Transformer<HttpResult<T>, T>() {
        @Override
        public Observable<T> call(Observable<HttpResult<T>> tObservable) {
            Observable<ActivityLifeCycleEvent> compareLifecycleObservable =
                    lifecycleSubject.takeFirst(new Func1<ActivityLifeCycleEvent, Boolean>() {
                        @Override
                        public Boolean call(ActivityLifeCycleEvent activityLifeCycleEvent) {
                            return activityLifeCycleEvent.equals(event);
                        }
                    });
            return tObservable.flatMap(new Func1<HttpResult<T>, Observable<T>>() {
                @Override
                public Observable<T> call(HttpResult<T> result) {
                    if (result.getCount() != 0) {
                        return createData(result.getSubjects());
                    } else {
                        return Observable.error(new ApiException(result.getCount()));
                    }
                }
            }).takeUntil(compareLifecycleObservable).subscribeOn(Schedulers.io())
                    .unsubscribeOn(Schedulers.io())
                    .subscribeOn(AndroidSchedulers.mainThread())
                    .observeOn(AndroidSchedulers.mainThread());
        }
    };
}
 
开发者ID:TBoyLi,项目名称:TMvpSimple,代码行数:33,代码来源:RxHelper.java


注:本文中的rx.subjects.PublishSubject类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。