Many implementations of ReactiveX use “Scheduler
s”
to govern an Observable’s transitions between threads in a multi-threaded environment. You can instruct
an Observable to do its work on a particular Scheduler by calling the Observable’s
SubscribeOn operator.
The ObserveOn operator is similar, but more limited. It instructs the Observable to send notifications to observers on a specified Scheduler.
In some implementations there is also an UnsubscribeOn operator.
By default, an Observable and the chain of operators that you apply to it will do its work, and will notify
its observers, on the same thread on which its Subscribe
method is called. The
SubscribeOn operator changes this behavior by specifying a different Scheduler
on which the Observable should operate. The ObserveOn operator specifies a
different Scheduler that the Observable will use to send notifications to its observers.
As shown in this illustration, the SubscribeOn operator designates which thread the Observable will begin operating on, no matter at what point in the chain of operators that operator is called. ObserveOn, on the other hand, affects the thread that the Observable will use below where that operator appears. For this reason, you may call ObserveOn multiple times at various points during the chain of Observable operators in order to change on which threads certain of those operators operate.
Scheduler
TBD
To specify on which Scheduler the Observable should operate, use the subscribeOn
operator,
passing it the appropriate Scheduler
.
subscribeOn(Scheduler)
To specify which Scheduler observers will use to unsubscribe from an Observable, use the
unsubscribeOn
operator, passing it the appropriate Scheduler
.
unsubscribeOn(Scheduler)
To specify on which Scheduler the Observable should operate, use the subscribeOn
operator,
passing it the appropriate Scheduler
.
subscribeOn(Scheduler)
To specify which Scheduler observers will use to unsubscribe from an Observable, use the
unsubscribeOn
operator, passing it the appropriate Scheduler
.
unsubscribeOn(Scheduler)
To specify on which Scheduler the Observable should operate, use the subscribeOn
operator,
passing it the appropriate Scheduler
.
/* Change from immediate scheduler to timeout */ var source = Rx.Observable.return(42, Rx.Scheduler.immediate) .subscribeOn(Rx.Scheduler.timeout); var subscription = source.subscribe( function (x) { console.log('Next: ' + x); }, function (err) { console.log('Error: ' + err); }, function () { console.log('Completed'); });
Next: 42 Completed
subscribeOn
is found in each of the following distributions:
rx.js
rx.all.js
rx.all.compat.js
rx.compat.js
rx.lite.js
rx.lite.compat.js