17
17
18
18
package rx .lang .scala
19
19
20
- import org .scalatest .junit .JUnitSuite
21
- import scala .collection .Seq
22
- import rx .lang .scala .observables .BlockingObservable
23
-
24
-
25
20
/**
26
21
* The Observable interface that implements the Reactive Pattern.
27
22
*/
@@ -38,6 +33,7 @@ class Observable[+T](val asJava: rx.Observable[_ <: T])
38
33
import rx .lang .scala .{Notification , Subscription , Scheduler , Observer }
39
34
import rx .lang .scala .util ._
40
35
import rx .lang .scala .subjects .Subject
36
+ import rx .lang .scala .observables .BlockingObservable
41
37
import rx .lang .scala .ImplicitFunctionConversions ._
42
38
43
39
/**
@@ -1149,17 +1145,10 @@ class Observable[+T](val asJava: rx.Observable[_ <: T])
1149
1145
* @return an Observable that emits items from the source Observable so long as the predicate
1150
1146
* continues to return <code>true</code> for each item, then completes
1151
1147
*/
1152
- // TODO: if we have zipWithIndex, takeWhileWithIndex is not needed any more
1153
1148
def takeWhileWithIndex (predicate : (T , Integer ) => Boolean ): Observable [T ] = {
1154
1149
Observable [T ](asJava.takeWhileWithIndex(predicate))
1155
1150
}
1156
1151
1157
- /* TODO zipWithIndex once it's in RxJava
1158
- def zipWithIndex: Observable[(T, Int)] = {
1159
- ???
1160
- }
1161
- */
1162
-
1163
1152
/**
1164
1153
* Returns an Observable that emits only the last <code>count</code> items emitted by the source
1165
1154
* Observable.
@@ -1339,6 +1328,20 @@ class Observable[+T](val asJava: rx.Observable[_ <: T])
1339
1328
Observable [U ](o5)
1340
1329
}
1341
1330
1331
+ /**
1332
+ * Combines two observables, emitting a pair of the latest values of each of
1333
+ * the source observables each time an event is received from one of the source observables, where the
1334
+ * aggregation is defined by the given function.
1335
+ *
1336
+ * @param that
1337
+ * The second source observable.
1338
+ * @return An Observable that combines the source Observables
1339
+ */
1340
+ def combineLatest [U ](that : Observable [U ]): Observable [(T , U )] = {
1341
+ val f : Func2 [_ >: T , _ >: U , _ <: (T , U )] = (t : T , u : U ) => (t, u)
1342
+ Observable [(T , U )](rx.Observable .combineLatest[T , U , (T , U )](this .asJava, that.asJava, f))
1343
+ }
1344
+
1342
1345
/**
1343
1346
* Debounces by dropping all values that are followed by newer values before the timeout value expires. The timer resets on each `onNext` call.
1344
1347
* <p>
@@ -1761,7 +1764,6 @@ object Observable {
1761
1764
import scala .collection .JavaConverters ._
1762
1765
import scala .collection .immutable .Range
1763
1766
import scala .concurrent .duration .Duration
1764
- import scala .concurrent .Future
1765
1767
import rx .{Observable => JObservable }
1766
1768
import rx .lang .scala .{Notification , Subscription , Scheduler , Observer }
1767
1769
import rx .lang .scala .util ._
@@ -1896,75 +1898,6 @@ object Observable {
1896
1898
def just [T ](value : T ): Observable [T ] = {
1897
1899
Observable [T ](JObservable .just(value))
1898
1900
}
1899
-
1900
- /**
1901
- * This behaves like {@link #merge(java.util.List)} except that if any of the merged Observables
1902
- * notify of an error via {@link Observer#onError onError}, {@code mergeDelayError} will
1903
- * refrain from propagating that error notification until all of the merged Observables have
1904
- * finished emitting items.
1905
- * <p>
1906
- * <img width="640" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/mergeDelayError.png">
1907
- * <p>
1908
- * Even if multiple merged Observables send {@code onError} notifications, {@code mergeDelayError} will only invoke the {@code onError} method of its
1909
- * Observers once.
1910
- * <p>
1911
- * This method allows an Observer to receive all successfully emitted items from all of the
1912
- * source Observables without being interrupted by an error notification from one of them.
1913
- *
1914
- * @param source
1915
- * a list of Observables
1916
- * @return an Observable that emits items that are the result of flattening the items emitted by
1917
- * the {@code source} list of Observables
1918
- * @see <a href="http://msdn.microsoft.com/en-us/library/hh229099(v=vs.103).aspx">MSDN: Observable.Merge Method</a>
1919
- */
1920
- // public static <T> Observable<T> mergeDelayError(List<? extends Observable<? extends T>> source)
1921
- // TODO decide if instance method mergeWithDelayError (?)
1922
-
1923
- /**
1924
- * This behaves like {@link #merge(Observable)} except that if any of the merged Observables
1925
- * notify of an error via {@link Observer#onError onError}, {@code mergeDelayError} will
1926
- * refrain from propagating that error notification until all of the merged Observables have
1927
- * finished emitting items.
1928
- * <p>
1929
- * <img width="640" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/mergeDelayError.png">
1930
- * <p>
1931
- * Even if multiple merged Observables send {@code onError} notifications, {@code mergeDelayError} will only invoke the {@code onError} method of its
1932
- * Observers once.
1933
- * <p>
1934
- * This method allows an Observer to receive all successfully emitted items from all of the
1935
- * source Observables without being interrupted by an error notification from one of them.
1936
- *
1937
- * @param source
1938
- * an Observable that emits Observables
1939
- * @return an Observable that emits items that are the result of flattening the items emitted by
1940
- * the Observables emitted by the {@code source} Observable
1941
- * @see <a href="http://msdn.microsoft.com/en-us/library/hh229099(v=vs.103).aspx">MSDN: Observable.Merge Method</a>
1942
- */
1943
- // public static <T> Observable<T> mergeDelayError(Observable<? extends Observable<? extends T>> source)
1944
- // TODO decide if instance method mergeWithDelayError (?)
1945
-
1946
- /**
1947
- * This behaves like {@link #merge(Observable...)} except that if any of the merged Observables
1948
- * notify of an error via {@link Observer#onError onError}, {@code mergeDelayError} will
1949
- * refrain from propagating that error notification until all of the merged Observables have
1950
- * finished emitting items.
1951
- * <p>
1952
- * <img width="640" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/mergeDelayError.png">
1953
- * <p>
1954
- * Even if multiple merged Observables send {@code onError} notifications, {@code mergeDelayError} will only invoke the {@code onError} method of its
1955
- * Observers once.
1956
- * <p>
1957
- * This method allows an Observer to receive all successfully emitted items from all of the
1958
- * source Observables without being interrupted by an error notification from one of them.
1959
- *
1960
- * @param source
1961
- * a series of Observables
1962
- * @return an Observable that emits items that are the result of flattening the items emitted by
1963
- * the {@code source} Observables
1964
- * @see <a href="http://msdn.microsoft.com/en-us/library/hh229099(v=vs.103).aspx">MSDN: Observable.Merge Method</a>
1965
- */
1966
- // public static <T> Observable<T> mergeDelayError(Observable<? extends T>... source)
1967
- // TODO decide if instance method mergeWithDelayError (?)
1968
1901
1969
1902
/**
1970
1903
* Returns an Observable that never sends any items or notifications to an {@link Observer}.
@@ -1979,45 +1912,23 @@ object Observable {
1979
1912
Observable [Nothing ](JObservable .never())
1980
1913
}
1981
1914
1982
- /*
1915
+ // TODO also support Scala Futures, but think well before. Do we want to Future and Observable
1916
+ // to share a common base interface?
1917
+
1918
+ // private because it's not RxScala's responsability to provide this alias
1919
+ private type Future [+ T ] = java.util.concurrent.Future [_ <: T ]
1920
+
1983
1921
def apply [T ](f : Future [T ]): Observable [T ] = {
1984
- ??? // TODO convert Scala Future to Java Future
1922
+ Observable [ T ](rx. Observable .from(f))
1985
1923
}
1986
- */
1987
1924
1988
- /*
1989
1925
def apply [T ](f : Future [T ], scheduler : Scheduler ): Observable [T ] = {
1990
- ??? // TODO convert Scala Future to Java Future
1926
+ Observable [ T ](rx. Observable .from(f, scheduler))
1991
1927
}
1992
- */
1993
1928
1994
- /*
1995
1929
def apply [T ](f : Future [T ], duration : Duration ): Observable [T ] = {
1996
- ??? // TODO convert Scala Future to Java Future
1930
+ Observable [ T ](rx. Observable .from(f, duration.length, duration.unit))
1997
1931
}
1998
- */
1999
-
2000
- /**
2001
- * Combines the given observables, emitting an event containing an aggregation of the latest values of each of the source observables
2002
- * each time an event is received from one of the source observables, where the aggregation is defined by the given function.
2003
- * <p>
2004
- * <img width="640" src="https://github.com/Netflix/RxJava/wiki/images/rx-operators/combineLatest.png">
2005
- *
2006
- * @param o1
2007
- * The first source observable.
2008
- * @param o2
2009
- * The second source observable.
2010
- * @param combineFunction
2011
- * The aggregation function used to combine the source observable values.
2012
- * @return An Observable that combines the source Observables with the given combine function
2013
- */
2014
- // public static <T1, T2, R> Observable<R> combineLatest(Observable<? extends T1> o1, Observable<? extends T2> o2, Func2<? super T1, ? super T2, ? extends R> combineFunction)
2015
- // TODO do we want this as an instance method?
2016
- // TODO then decide about combineLatest with > 2 Observables
2017
-
2018
- // TODO what about these two?
2019
- // public static <R> Observable<R> zip(Observable<? extends Observable<?>> ws, final FuncN<? extends R> zipFunction)
2020
- // public static <R> Observable<R> zip(Collection<? extends Observable<?>> ws, FuncN<? extends R> zipFunction)
2021
1932
2022
1933
/**
2023
1934
* Given a Seq of N observables, returns an observable that emits Seqs of N elements each.
@@ -2088,7 +1999,7 @@ class WithFilter[+T] private[scala] (p: T => Boolean, asJava: rx.Observable[_ <:
2088
1999
// there is no foreach here, that's only available on BlockingObservable
2089
2000
}
2090
2001
2091
- class UnitTestSuite extends JUnitSuite {
2002
+ class UnitTestSuite extends org.scalatest.junit. JUnitSuite {
2092
2003
import scala .concurrent .duration ._
2093
2004
import org .junit .{Before , Test , Ignore }
2094
2005
import org .junit .Assert ._
0 commit comments