@@ -864,6 +864,38 @@ trait Observable[+T]
864
864
: Observable [Observable [T ]] // SI-7818
865
865
}
866
866
867
+ /**
868
+ * Returns an Observable that emits windows of items it collects from the source Observable. The resulting
869
+ * Observable starts a new window periodically, as determined by the `timeshift` argument or a maximum
870
+ * size as specified by the `count` argument (whichever is reached first). It emits
871
+ * each window after a fixed timespan, specified by the `timespan` argument. When the source
872
+ * Observable completes or Observable completes or encounters an error, the resulting Observable emits the
873
+ * current window and propagates the notification from the source Observable.
874
+ *
875
+ * <img width="640" height="335" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/window7.s.png" alt="">
876
+ *
877
+ * ===Backpressure Support:===
878
+ * This operator does not support backpressure as it uses time to control data flow.
879
+ *
880
+ * ===Scheduler:===
881
+ * you specify which `Scheduler` this operator will use
882
+ *
883
+ * @param timespan the period of time each window collects items before it should be emitted
884
+ * @param timeshift the period of time after which a new window will be created
885
+ * @param count the maximum size of each window before it should be emitted
886
+ * @param scheduler the `Scheduler` to use when determining the end and start of a window
887
+ * @return an Observable that emits new windows periodically as a fixed timespan elapses
888
+ * @see <a href="https://github.com/Netflix/RxJava/wiki/Transforming-Observables#window">RxJava wiki: window</a>
889
+ * @see <a href="http://msdn.microsoft.com/en-us/library/system.reactive.linq.observable.window.aspx">MSDN: Observable.Window</a>
890
+ */
891
+ def sliding (timespan : Duration , timeshift : Duration , count : Int , scheduler : Scheduler ): Observable [Observable [T ]] = {
892
+ val span : Long = timespan.length
893
+ val shift : Long = timespan.unit.convert(timeshift.length, timeshift.unit)
894
+ val unit : TimeUnit = timespan.unit
895
+ Observable .jObsOfJObsToScObsOfScObs(asJavaObservable.window(span, shift, unit, count, scheduler))
896
+ : Observable [Observable [T ]] // SI-7818
897
+ }
898
+
867
899
/**
868
900
* Returns an Observable which only emits those items for which a given predicate holds.
869
901
*
@@ -1577,6 +1609,41 @@ trait Observable[+T]
1577
1609
toScalaObservable[T ](asJavaObservable.cache())
1578
1610
}
1579
1611
1612
+ /**
1613
+ * Caches emissions from the source Observable and replays them in order to any subsequent Subscribers.
1614
+ * This method has similar behavior to [[Observable.replay ]] except that this auto-subscribes to the source
1615
+ * Observable rather than returning a [[ConnectableObservable ]] for which you must call
1616
+ * `connect` to activate the subscription.
1617
+ * <p>
1618
+ * <img width="640" height="410" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/cache.png" alt="">
1619
+ * <p>
1620
+ * This is useful when you want an Observable to cache responses and you can't control the
1621
+ * `subscribe/unsubscribe` behavior of all the [[Subscriber ]]s.
1622
+ * <p>
1623
+ * When you call `cache`, it does not yet subscribe to the source Observable and so does not yet
1624
+ * begin cacheing items. This only happens when the first Subscriber calls the resulting Observable's
1625
+ * `subscribe` method.
1626
+ * <p>
1627
+ * <em>Note:</em> You sacrifice the ability to unsubscribe from the origin when you use the `cache`
1628
+ * Observer so be careful not to use this Observer on Observables that emit an infinite or very large number
1629
+ * of items that will use up memory.
1630
+ *
1631
+ * ===Backpressure Support:===
1632
+ * This operator does not support upstream backpressure as it is purposefully requesting and caching everything emitted.
1633
+ *
1634
+ * ===Scheduler:===
1635
+ * `cache` does not operate by default on a particular `Scheduler`.
1636
+ *
1637
+ * @param capacity hint for number of items to cache (for optimizing underlying data structure)
1638
+ * @return an Observable that, when first subscribed to, caches all of its items and notifications for the
1639
+ * benefit of subsequent subscribers
1640
+ * @see <a href="https://github.com/Netflix/RxJava/wiki/Observable-Utility-Operators#cache">RxJava wiki: cache</a>
1641
+ * @since 0.20
1642
+ */
1643
+ def cache (capacity : Int ): Observable [T ] = {
1644
+ toScalaObservable[T ](asJavaObservable.cache(capacity))
1645
+ }
1646
+
1580
1647
/**
1581
1648
* Returns a new [[Observable ]] that multicasts (shares) the original [[Observable ]]. As long a
1582
1649
* there is more than 1 [[Subscriber ]], this [[Observable ]] will be subscribed and emitting data.
@@ -2176,6 +2243,41 @@ trait Observable[+T]
2176
2243
toScalaObservable[(K , Observable [T ])](o1.map[(K , Observable [T ])](func))
2177
2244
}
2178
2245
2246
+ /**
2247
+ * Groups the items emitted by an [[Observable ]] according to a specified criterion, and emits these
2248
+ * grouped items as `(key, observable)` pairs.
2249
+ *
2250
+ * <img width="640" height="360" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/groupBy.png" alt="">
2251
+ *
2252
+ * Note: A `(key, observable)` will cache the items it is to emit until such time as it
2253
+ * is subscribed to. For this reason, in order to avoid memory leaks, you should not simply ignore those
2254
+ * `(key, observable)` pairs that do not concern you. Instead, you can signal to them that they may
2255
+ * discard their buffers by applying an operator like `take(0)` to them.
2256
+ *
2257
+ * ===Backpressure Support:===
2258
+ * This operator does not support backpressure as splitting a stream effectively turns it into a "hot observable"
2259
+ * and blocking any one group would block the entire parent stream. If you need backpressure on individual groups
2260
+ * then you should use operators such as `nBackpressureDrop` or `@link #onBackpressureBuffer`.</dd>
2261
+ * ===Scheduler:===
2262
+ * groupBy` does not operate by default on a particular `Scheduler`.
2263
+ *
2264
+ * @param keySelector a function that extracts the key for each item
2265
+ * @param valueSelector a function that extracts the return element for each item
2266
+ * @tparam K the key type
2267
+ * @tparam V the value type
2268
+ * @return an [[Observable ]] that emits `(key, observable)` pairs, each of which corresponds to a
2269
+ * unique key value and each of which emits those items from the source Observable that share that
2270
+ * key value
2271
+ * @see <a href="https://github.com/Netflix/RxJava/wiki/Transforming-Observables#groupby-and-groupbyuntil">RxJava wiki: groupBy</a>
2272
+ * @see <a href="http://msdn.microsoft.com/en-us/library/system.reactive.linq.observable.groupby.aspx">MSDN: Observable.GroupBy</a>
2273
+ */
2274
+ def groupBy [K , V ](keySelector : T => K , valueSelector : T => V ): Observable [(K , Observable [V ])] = {
2275
+ val jo : rx.Observable [rx.observables.GroupedObservable [K , V ]] = asJavaObservable.groupBy[K , V ](keySelector, valueSelector)
2276
+ toScalaObservable[rx.observables.GroupedObservable [K , V ]](jo).map {
2277
+ go : rx.observables.GroupedObservable [K , V ] => (go.getKey, toScalaObservable[V ](go))
2278
+ }
2279
+ }
2280
+
2179
2281
/**
2180
2282
* Groups the items emitted by this Observable according to a specified discriminator function and terminates these groups
2181
2283
* according to a function.
@@ -4298,6 +4400,75 @@ trait Observable[+T]
4298
4400
def nonEmpty : Observable [Boolean ] = {
4299
4401
isEmpty.map(! _)
4300
4402
}
4403
+
4404
+ /**
4405
+ * Transform an Observable by applying a particular Transformer function to it.
4406
+ *
4407
+ * This method operates on the Observable itself whereas [[Observable.lift ]] operates on the Observable's
4408
+ * Subscribers or Observers.
4409
+ *
4410
+ * If the operator you are creating is designed to act on the individual items emitted by a source
4411
+ * Observable, use [[Observable.lift ]]. If your operator is designed to transform the source Observable as a whole
4412
+ * (for instance, by applying a particular set of existing RxJava operators to it) use `compose`.
4413
+ *
4414
+ * ===Scheduler:===
4415
+ * `compose` does not operate by default on a particular [[Scheduler ]].
4416
+ *
4417
+ * @param transformer implements the function that transforms the source Observable
4418
+ * @return the source Observable, transformed by the transformer function
4419
+ * @see <a href="https://github.com/Netflix/RxJava/wiki/Implementing-Your-Own-Operators">RxJava wiki: Implementing Your Own Operators</a>
4420
+ */
4421
+ def compose [R ](transformer : Observable [T ] => Observable [R ]): Observable [R ] = {
4422
+ toScalaObservable[R ](asJavaObservable.compose(toJavaTransformer(transformer)))
4423
+ }
4424
+
4425
+ /**
4426
+ * Instructs an Observable that is emitting items faster than its observer can consume them to buffer these
4427
+ * items indefinitely until they can be emitted.
4428
+ *
4429
+ * <img width="640" height="300" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/bp.obp.buffer.png" alt="">
4430
+ *
4431
+ * ===Scheduler:===
4432
+ * `onBackpressureBuffer` does not operate by default on a particular `Scheduler`.
4433
+ *
4434
+ * @return the source Observable modified to buffer items to the extent system resources allow
4435
+ * @see <a href="https://github.com/Netflix/RxJava/wiki/Backpressure">RxJava wiki: Backpressure</a>
4436
+ */
4437
+ def onBackpressureBuffer : Observable [T ] = {
4438
+ toScalaObservable[T ](asJavaObservable.onBackpressureBuffer)
4439
+ }
4440
+
4441
+ /**
4442
+ * Use this operator when the upstream does not natively support backpressure and you wish to drop
4443
+ * `onNext` when unable to handle further events.
4444
+ *
4445
+ * <img width="640" height="245" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/bp.obp.drop.png" alt="">
4446
+ *
4447
+ * If the downstream request count hits 0 then `onNext` will be dropped until `request(long n)`
4448
+ * is invoked again to increase the request count.
4449
+ *
4450
+ * ===Scheduler:===
4451
+ * onBackpressureDrop` does not operate by default on a particular `Scheduler`.
4452
+ *
4453
+ * @return the source Observable modified to drop `onNext` notifications on overflow
4454
+ * @see <a href="https://github.com/Netflix/RxJava/wiki/Backpressure">RxJava wiki: Backpressure</a>
4455
+ */
4456
+ def onBackpressureDrop : Observable [T ] = {
4457
+ toScalaObservable[T ](asJavaObservable.onBackpressureDrop)
4458
+ }
4459
+
4460
+ /**
4461
+ * Return a new [[Observable ]] by applying a partial function to all elements of this [[Observable ]]
4462
+ * on which the function is defined.
4463
+ *
4464
+ * @tparam R the element type of the returned [[Observable ]].
4465
+ * @param pf the partial function which filters and maps the [[Observable ]].
4466
+ * @return a new [[Observable ]] by applying a partial function to all elements of this [[Observable ]]
4467
+ * on which the function is defined.
4468
+ */
4469
+ def collect [R ](pf : PartialFunction [T , R ]): Observable [R ] = {
4470
+ filter(pf.isDefinedAt(_)).map(pf)
4471
+ }
4301
4472
}
4302
4473
4303
4474
/**
@@ -4746,6 +4917,7 @@ object Observable {
4746
4917
* @param observableFactory the factory function to obtain an Observable
4747
4918
* @return the Observable whose lifetime controls the lifetime of the dependent resource object
4748
4919
*/
4920
+ @ deprecated(" Use `using(=> Resource)(Resource => Observable[T], Resource => Unit)` instead" , " 0.20.1" )
4749
4921
def using [T , Resource <: Subscription ](resourceFactory : () => Resource , observableFactory : Resource => Observable [T ]): Observable [T ] = {
4750
4922
class ResourceSubscription (val resource : Resource ) extends rx.Subscription {
4751
4923
def unsubscribe = resource.unsubscribe
@@ -4759,6 +4931,32 @@ object Observable {
4759
4931
))
4760
4932
}
4761
4933
4934
+ /**
4935
+ * Constructs an Observable that creates a dependent resource object.
4936
+ *
4937
+ * <img width="640" height="400" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/using.png" alt="" />
4938
+ *
4939
+ * ===Scheduler:===
4940
+ * `using` does not operate by default on a particular `Scheduler`.
4941
+ *
4942
+ * @param resourceFactory the factory function to create a resource object that depends on the Observable.
4943
+ * Note: this is a by-name parameter.
4944
+ * @param observableFactory the factory function to create an Observable
4945
+ * @param dispose the function that will dispose of the resource
4946
+ * @return the Observable whose lifetime controls the lifetime of the dependent resource object
4947
+ * @see <a href="https://github.com/Netflix/RxJava/wiki/Observable-Utility-Operators#using">RxJava wiki: using</a>
4948
+ * @see <a href="http://msdn.microsoft.com/en-us/library/hh229585.aspx">MSDN: Observable.Using</a>
4949
+ */
4950
+ def using [T , Resource ](resourceFactory : => Resource )(observableFactory : Resource => Observable [T ], dispose : Resource => Unit ): Observable [T ] = {
4951
+ val jResourceFactory = new rx.functions.Func0 [Resource ] {
4952
+ override def call : Resource = resourceFactory
4953
+ }
4954
+ val jObservableFactory = new rx.functions.Func1 [Resource , rx.Observable [_ <: T ]] {
4955
+ override def call (r : Resource ) = observableFactory(r).asJavaObservable
4956
+ }
4957
+ toScalaObservable[T ](rx.Observable .using[T , Resource ](jResourceFactory, jObservableFactory, dispose))
4958
+ }
4959
+
4762
4960
/**
4763
4961
* Mirror the one Observable in an Iterable of several Observables that first emits an item.
4764
4962
*
0 commit comments