Skip to content

Commit 354c959

Browse files
committed
Add using to RxScala
1 parent ec2f6d6 commit 354c959

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

language-adaptors/rxjava-scala/src/main/scala/rx/lang/scala/Observable.scala

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3335,6 +3335,28 @@ object Observable {
33353335
toScalaObservable[java.lang.Long](rx.Observable.timer(initialDelay.toNanos, period.toNanos, duration.NANOSECONDS, scheduler)).map(_.longValue())
33363336
}
33373337

3338+
/**
3339+
* Constructs an Observable that creates a dependent resource object.
3340+
* <p>
3341+
* <img width="640" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/using.png">
3342+
*
3343+
* @param resourceFactory the factory function to create a resource object that depends on the Observable
3344+
* @param observableFactory the factory function to obtain an Observable
3345+
* @return the Observable whose lifetime controls the lifetime of the dependent resource object
3346+
*/
3347+
def using[T, Resource <: Subscription](resourceFactory: () => Resource, observableFactory: Resource => Observable[T]): Observable[T] = {
3348+
class ResourceSubscription(val resource: Resource) extends rx.Subscription {
3349+
def unsubscribe = resource.unsubscribe
3350+
3351+
def isUnsubscribed: Boolean = resource.isUnsubscribed
3352+
}
3353+
3354+
toScalaObservable(rx.Observable.using[T, ResourceSubscription](
3355+
() => new ResourceSubscription(resourceFactory()),
3356+
(s: ResourceSubscription) => observableFactory(s.resource).asJavaObservable
3357+
))
3358+
}
3359+
33383360
}
33393361

33403362

0 commit comments

Comments
 (0)