當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。