The Defer operator waits until an observer subscribes to it, and then it generates an Observable, typically with an Observable factory function. It does this afresh for each subscriber, so although each subscriber may think it is subscribing to the same Observable, in fact each subscriber gets its own individual sequence.
In some circumstances, waiting until the last minute (that is, until subscription time) to generate the Observable can ensure that this Observable contains the freshest data.
TBD
TBD
RxGroovy implements this operator as defer
. This operator takes as its sole
parameter an Observable factory function of your choosing. This function takes no parameters
and returns an Observable.
defer
does not by default operate on a particular Scheduler.
defer()
There is a somewhat similar operator in the optional rxjava-computation-expressions
package (it is not part of the standard RxGroovy set of operators). The switchCase
operator
conditionally creates and returns one of a set of possible Observables.
An even simpler operator in the optional rxjava-computation-expressions
package (also not
part of the standard RxGroovy set of operators) is ifThen
. This operator checks a condition
and then either mirrors the source Observable or an empty Observable depending on the result.
RxJava implements this operator as defer
. This operator takes as its sole
parameter an Observable factory function of your choosing. This function takes no parameters
and returns an Observable.
defer
does not by default operate on a particular Scheduler.
defer()
There is a somewhat similar operator in the optional rxjava-computation-expressions
package (it is not part of the standard RxJava set of operators). The switchCase
operator
conditionally creates and returns one of a set of possible Observables.
An even simpler operator in the optional rxjava-computation-expressions
package (also not
part of the standard RxGroovy set of operators) is ifThen
. This operator checks a condition
and then either mirrors the source Observable or an empty Observable depending on the result.
RxJS implements this operator as defer
. This operator takes as its sole parameter
an Observable factory function of your choosing. This function takes no parameters and returns
an Observable or a Promise.
/* Using an observable sequence */ var source = Rx.Observable.defer(function () { return Rx.Observable.return(42); }); var subscription = source.subscribe( function (x) { console.log('Next: ' + x); }, function (err) { console.log('Error: ' + err); }, function () { console.log('Completed'); } );
Next: 42 Completed
var source = Rx.Observable.defer(function () { return RSVP.Promise.resolve(42); }); var subscription = source.subscribe( function (x) { console.log('Next: ' + x); }, function (err) { console.log('Error: ' + err); }, function () { console.log('Completed'); } );
Next: 42 Completed
defer
is found in the following distributions:
rx.js
rx.all.js
rx.all.compat.js
rx.compat.js
rx.lite.js
rx.lite.compat.js
RxJS also implements the if
operator. It takes as parameters a function that returns a
boolean, an Observable to mirror if that function returns a true value, and optionally a second
Observable to mirror if that function returns a false value (if you omit this parameter, if
will mirror an empty Observable in such a case).
var shouldRun = false; var source = Rx.Observable.if( function () { return shouldRun; }, Rx.Observable.return(42), Rx.Observable.return(56) ); var subscription = source.subscribe( function (x) { console.log('Next: ' + x); }, function (err) { console.log('Error: ' + err); }, function () { console.log('Completed'); });
Next: 56 Completed
if
is found in the following distributions:
rx.all.js
rx.experimental.js
(requires rx.js
, rx.compat.js
, rx.lite.js
, or rx.lite.compat.js
)
RxJS implements a somewhat similar operator called case
(or
“switchCase
”). This operator conditionally creates and returns one of a set of
possible Observables. It takes the following parameters:
var sources = { 'foo': Rx.Observable.return(42), 'bar': Rx.Observable.return(56) }; var defaultSource = Rx.Observable.empty(); var source = Rx.Observable.case( function () { return 'foo'; }, sources, defaultSource); var subscription = source.subscribe( function (x) { console.log('Next: ' + x); }, function (err) { console.log('Error: ' + err); }, function () { console.log('Completed'); } );
Next: 42 Completed
case
/switchCase
is found in the following distributions:
rx.all.js
rx.all.compat.js
rx.experimental.js
(requires rx.js
, rx.compat.js
, rx.lite.js
, or rx.lite.compat.js
)TBD
RxPHP implements this operator as defer
.
Returns an observable sequence that invokes the specified factory function whenever a new observer subscribes.
//from https://github.com/ReactiveX/RxPHP/blob/master/demo/defer/defer.php $source = \Rx\Observable::defer(function () { return \Rx\Observable::just(42); }); $subscription = $source->subscribe($stdoutObserver);
Next value: 42 Complete!
TBD
TBD
TBD