You can use the generateWithRelativeTime
operator to create simple Observables that can
generate their next emissions, and can determine when to terminate, based on the value of the previous
emission. The basic form of generateWithRelativeTime
takes five parameters:
- the first item to emit
- a function to test an item to determine whether to emit it (
true
) or terminate the
Observable (false
)
- a function to generate the next item to test and emit based on the value of the previous item
- a function to transform items before emitting them
- a function to indicate how long, in milliseconds, the generator should wait after the emission of
the previous item before emitting this item
You can also pass in as an optional sixth parameter a Scheduler that
generate
will use to create and emit its sequence (it uses currentThread
by
default).
Sample Code
var source = Rx.Observable.generateWithRelativeTime(
1,
function (x) { return x < 4; },
function (x) { return x + 1; },
function (x) { return x; },
function (x) { return 100 * x; }
).timeInterval();
var subscription = source.subscribe(
function (x) { console.log('Next: ' + x); },
function (err) { console.log('Error: ' + err); },
function () { console.log('Completed'); });
Next: {value: 1, interval: 100}
Next: {value: 2, interval: 200}
Next: {value: 3, interval: 300}
Completed
generateWithRelativeTime
is found in the following distributions:
rx.lite.js
rx.lite.compat.js
rx.time.js
(requires rx.js
or rx.compat.js
)