/** * Creates and returns a new {@code PublishSubject}. * * @param <T> the value type * @return the new {@code PublishSubject} */ public static <T> PublishSubject<T> create() { final SubjectSubscriptionManager<T> state = new SubjectSubscriptionManager<T>(); state.onTerminated = new Action1<SubjectObserver<T>>() { @Override public void call(SubjectObserver<T> o) { o.emitFirst(state.get(), state.nl); } }; return new PublishSubject<T>(state, state); }
/** * Creates and returns a new {@code TestSubject}. * * @param <T> the value type * @param scheduler a {@link TestScheduler} on which to operate this Subject * @return the new {@code TestSubject} */ public static <T> TestSubject<T> create(TestScheduler scheduler) { final SubjectSubscriptionManager<T> state = new SubjectSubscriptionManager<T>(); state.onAdded = new Action1<SubjectObserver<T>>() { @Override public void call(SubjectObserver<T> o) { o.emitFirst(state.get(), state.nl); } }; state.onTerminated = state.onAdded; return new TestSubject<T>(state, state, scheduler); }
/** * Creates and returns a new {@code AsyncSubject}. * @param <T> the result value type * @return the new {@code AsyncSubject} */ public static <T> AsyncSubject<T> create() { final SubjectSubscriptionManager<T> state = new SubjectSubscriptionManager<T>(); state.onTerminated = new Action1<SubjectObserver<T>>() { @Override public void call(SubjectObserver<T> o) { Object v = state.get(); NotificationLite<T> nl = state.nl; o.accept(v, nl); if (v == null || (!nl.isCompleted(v) && !nl.isError(v))) { o.onCompleted(); } } }; return new AsyncSubject<T>(state, state); }
/** * Creates a bounded replay subject with the given state shared between the subject and the * {@link OnSubscribe} functions. * * @param <T> * the type of items observed and emitted by the Subject * @param state * the shared state * @return the created subject */ static final <T> ReplaySubject<T> createWithState(final BoundedState<T> state, Action1<SubjectObserver<T>> onStart) { SubjectSubscriptionManager<T> ssm = new SubjectSubscriptionManager<T>(); ssm.onStart = onStart; ssm.onTerminated = new Action1<SubjectObserver<T>>() { @Override public void call(SubjectObserver<T> t1) { NodeList.Node<Object> l = t1.index(); if (l == null) { l = state.head(); } state.replayObserverFromIndex(l, t1); } }; return new ReplaySubject<T>(ssm, ssm, state); }
SubjectSubscriptionManager<T> ssm = new SubjectSubscriptionManager<T>(); ssm.onStart = new Action1<SubjectObserver<T>>() { @Override
private static <T> BehaviorSubject<T> create(T defaultValue, boolean hasDefault) { final SubjectSubscriptionManager<T> state = new SubjectSubscriptionManager<T>(); if (hasDefault) { state.set(NotificationLite.instance().next(defaultValue)); } state.onAdded = new Action1<SubjectObserver<T>>() { @Override public void call(SubjectObserver<T> o) { o.emitFirst(state.get(), state.nl); } }; state.onTerminated = state.onAdded; return new BehaviorSubject<T>(state, state); }
private static <T> ClearableBehaviorSubject<T> create(T defaultValue, boolean hasDefault) { final SubjectSubscriptionManager<T> state = new SubjectSubscriptionManager<T>(); if (hasDefault) { state.set(NotificationLite.instance().next(defaultValue)); } state.onAdded = new Action1<SubjectObserver<T>>() { @Override public void call(SubjectObserver<T> o) { o.emitFirst(state.get(), state.nl); } }; state.onTerminated = state.onAdded; return new ClearableBehaviorSubject<T>(state, state); }