2828import io .reactivex .internal .subscribers .completable .*;
2929import io .reactivex .plugins .RxJavaPlugins ;
3030import io .reactivex .schedulers .Schedulers ;
31+ import io .reactivex .subscribers .TestSubscriber ;
3132
3233/**
3334 * Represents a deferred computation without any value but only indication for completion or exception.
@@ -165,33 +166,44 @@ public static Completable concat(Publisher<? extends CompletableSource> sources,
165166 }
166167
167168 /**
168- * Constructs a Completable instance by wrapping the given source callback.
169+ * Provides an API (via a cold Completable) that bridges the reactive world with the callback-style world.
170+ * <p>
171+ * Example:
172+ * <pre><code>
173+ * Completable.create(emitter -> {
174+ * Callback listener = new Callback() {
175+ * @Override
176+ * public void onEvent(Event e) {
177+ * emitter.onComplete();
178+ * }
179+ *
180+ * @Override
181+ * public void onFailure(Exception e) {
182+ * emitter.onError(e);
183+ * }
184+ * };
185+ *
186+ * AutoCloseable c = api.someMethod(listener);
187+ *
188+ * emitter.setCancellable(c::close);
189+ *
190+ * });
191+ * </code></pre>
192+ * <p>
169193 * <dl>
170194 * <dt><b>Scheduler:</b></dt>
171195 * <dd>{@code create} does not operate by default on a particular {@link Scheduler}.</dd>
172196 * </dl>
173- * @param source the callback which will receive the CompletableObserver instances
174- * when the Completable is subscribed to.
175- * @return the created Completable instance
176- * @throws NullPointerException if source is null
197+ * @param source the emitter that is called when a Subscriber subscribes to the returned {@code Flowable}
198+ * @return the new Completable instance
199+ * @see FlowableOnSubscribe
200+ * @see FlowableEmitter.BackpressureMode
201+ * @see Cancellable
177202 */
178203 @ SchedulerSupport (SchedulerSupport .NONE )
179- public static Completable create (CompletableSource source ) {
204+ public static Completable create (CompletableOnSubscribe source ) {
180205 Objects .requireNonNull (source , "source is null" );
181- if (source instanceof Completable ) {
182- throw new IllegalArgumentException ("Use of create(Completable)!" );
183- }
184- try {
185- // TODO plugin wrapping source
186-
187- return RxJavaPlugins .onAssembly (new CompletableFromSource (source ));
188- } catch (NullPointerException ex ) { // NOPMD
189- throw ex ;
190- } catch (Throwable ex ) {
191- Exceptions .throwIfFatal (ex );
192- RxJavaPlugins .onError (ex );
193- throw toNpe (ex );
194- }
206+ return RxJavaPlugins .onAssembly (new CompletableCreate (source ));
195207 }
196208
197209 /**
@@ -214,8 +226,6 @@ public static Completable unsafeCreate(CompletableSource source) {
214226 throw new IllegalArgumentException ("Use of unsafeCreate(Completable)!" );
215227 }
216228 try {
217- // TODO plugin wrapping source
218-
219229 return RxJavaPlugins .onAssembly (new CompletableFromUnsafeSource (source ));
220230 } catch (NullPointerException ex ) { // NOPMD
221231 throw ex ;
@@ -1696,4 +1706,33 @@ public final Completable unsubscribeOn(final Scheduler scheduler) {
16961706 Objects .requireNonNull (scheduler , "scheduler is null" );
16971707 return new CompletableUnsubscribeOn (this , scheduler );
16981708 }
1709+ // -------------------------------------------------------------------------
1710+ // Fluent test support, super handy and reduces test preparation boilerplate
1711+ // -------------------------------------------------------------------------
1712+
1713+ /**
1714+ * Creates a TestSubscriber and subscribes
1715+ * it to this Completable.
1716+ * @return the new TestSubscriber instance
1717+ * @since 2.0
1718+ */
1719+ public final TestSubscriber <Void > test () {
1720+ TestSubscriber <Void > ts = new TestSubscriber <Void >();
1721+ subscribe (ts );
1722+ return ts ;
1723+ }
1724+
1725+ /**
1726+ * Creates a TestSubscriber optionally in cancelled state, then subscribes it to this Completable.
1727+ * @param cancelled if true, the TestSubscriber will be cancelled before subscribing to this
1728+ * Completable.
1729+ * @return the new TestSubscriber instance
1730+ * @since 2.0
1731+ */
1732+ public final TestSubscriber <Void > test (boolean cancelled ) {
1733+ TestSubscriber <Void > ts = new TestSubscriber <Void >();
1734+ ts .dispose ();
1735+ subscribe (ts );
1736+ return ts ;
1737+ }
16991738}
0 commit comments